Android - Fabric.io Twitter REST API profile image / picture
Asked Answered
Z

3

6

Does anyone know if there is a way to pull a signed in users profile picture to be placed through the app, to maybe place it on the ActionBar as they navigate around?

hints, tips, examples, downloads all welcome :)

If you can help me, please assume I very little knowledge regarding anything outside basic Java!

Again, thanks people x

Zingg answered 13/2, 2015 at 16:50 Comment(0)
T
9

You can get a user's profile image by using /1.1/users/show.json. You can refer to REST API URLs for Twitter data.

By extending TwitterApiClient we can retrieve Twitter data from the URL.

class MyTwitterApiClient extends TwitterApiClient {
    public MyTwitterApiClient(TwitterSession session) {
        super(session);
    }

    public UsersService getUsersService() {
        return getService(UsersService.class);
    }
}

interface UsersService {
    @GET("/1.1/users/show.json")
    void show(@Query("user_id") Long userId,
            @Query("screen_name") String screenName,
            @Query("include_entities") Boolean includeEntities,
            Callback<User> cb);
}

Next, get the UsersService and call its show method, passing in the defined query parameters. I defined the query parameters based on the ones that are documented.

new MyTwitterApiClient(session).getUsersService().show(12L, null, true,
new Callback<User>() {
    @Override
    public void success(Result<User> result) {
        Log.d("twittercommunity", "user's profile url is "
                + result.data.profileImageUrlHttps);
    }

    @Override
    public void failure(TwitterException exception) {
        Log.d("twittercommunity", "exception is " + exception);
    }
});

Courtesy: https://twittercommunity.com/t/android-get-user-profile-image/30579/2

Teresita answered 20/3, 2015 at 13:6 Comment(4)
I had to define the interface inside of MyTwitterApiClient and make it public for it to workExcurrent
Thnx dude.I was surfing for it .! :)Encyst
What should be the User in new Callback<User>() ? Should it be twitter4j.User or something else?Malfunction
com.twitter.sdk.android.core.models.UserTeresita
L
5

This is how I got mine to work:

            TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
            twitterApiClient.getAccountService().verifyCredentials(false,false, new Callback<User>() {
                @Override
                public void success(Result<User> userResult) {
                    String name = userResult.data.name;
                    String profilebannerurl = userResult.data.profileBannerUrl;
                    String profileurl = userResult.data.profileImageUrl;

                }

                @Override
                public void failure(TwitterException e) {

                }
            });

I have place this piece of code within my LoginButton callback method:

    loginButton.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) { <insert here> }
Lexy answered 3/5, 2015 at 18:29 Comment(5)
I did exactly the same, however, i still face the callback null issue. Any idea what can be the issue? I've also added button.onActivityResult(requestCode, resultCode, data);Linkoski
I think maybe check whether u had logged onto twitter before you implement any twitter functionality like fetching the profile pic etc. That is the usual why it returns null. I had struggled with this as when I started, I would log off and then still try fetch profile pic etc and it would return null as I would not have the authorisation to do so.Lexy
What should be the User in new Callback<User>() ? Should it be twitter4j.User or something else?Malfunction
Thats a callback method that returns an object called Result<User> userResult. from userResults you can query it for all the user fields, by saying userResult.data.name.Lexy
I tried the same way, but neither success, nor failure getting calledMalfunction
S
1

I did it with a custom button and this is the code that is executed by it's onClick listener :

TwitterAuthConfig authConfig =  new TwitterAuthConfig(TWITTER_API_KEY, TWITTER_API_SECRET);
Fabric.with(activity, new Twitter(authConfig));

TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(false, false, new com.twitter.sdk.android.core.Callback<User>() {
    @Override
    public void success(Result<User> result) {
        Log.d(TAG, "Twitter log in success");

        String userName = result.data.screenName;
        int userId = result.data.id;
        String pictureUrl = result.data.profileImageUrl;
        String coverUrl = result.data.profileBannerUrl;
    }

    @Override
    public void failure(TwitterException e) {
        Log.d(TAG, "Twitter log in error : " + e.getMessage());
    }
});

I should ask the user to authorize access to your app and log him in if he accepts.

Saguaro answered 16/2, 2016 at 1:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.