How to get user information from twitter in androi

2019-01-09 11:01发布

I am integrating twitter in my android app. I am able to authorize the app for the user. Now, I am looking for the API which gives me logged users information like first name, last name, email, etc. I had done this for facebook with

facebook.request("me");

Now how to get user info from twitter? I am using twitter4j-core-android2.2.3.jar. Plz let me know is there a way to get user info.

5条回答
做自己的国王
2楼-- · 2019-01-09 11:38
Twitter.getApiClient(session).getAccountService().verifyCredentials(true, false).enqueue(new Callback<User>()
        {
            @Override
            public void success(Result<User> userResult)
            {
                try
                {
                    User user = userResult.data;
//                         twitterImage = user.profileImageUrl;
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void failure(TwitterException e)
            {

            }

        });
查看更多
beautiful°
3楼-- · 2019-01-09 11:43

You can check bellow code: To get user info you can use Twitter Fabric SDK. Its documentation is here and here

 twitterButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                // Do something with result, which provides a TwitterSession for making API calls
                AccountService ac = Twitter.getApiClient(result.data).getAccountService();
                ac.verifyCredentials(true, true, new Callback<com.twitter.sdk.android.core.models.User>() {
                    @Override
                    public void success(Result<com.twitter.sdk.android.core.models.User> result) {
                        String imageUrl = result.data.profileImageUrl;
                        String email = result.data.email;
                        String userName = result.data.name;
                        System.out.println(imageUrl);
                        System.out.println(email);
                        System.out.println(userName);
                    }

                    @Override
                    public void failure(TwitterException e) {
                        Log.d("ls",e.getMessage());
                    }
                });

            }

            @Override
            public void failure(TwitterException exception) {
                Toast.makeText(getApplicationContext(),
                        getResources().getString(R.string.app_name),
                        Toast.LENGTH_SHORT).show();
            }
        });

Here twitterButton is import com.twitter.sdk.android.core.identity.TwitterLoginButton; In this response you can get All credential without user Email.

查看更多
别忘想泡老子
4楼-- · 2019-01-09 11:49

You cannot get Email from the twitter OAuth unless or untill your app is whitelisted. For more Info Email ID from Twitter

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-09 11:50

Finally I got user information.

use the access token you get after

accessToken = twitterConnection.getOAuthAccessToken
    (requestToken,editPinCode.getText().toString());

oHelper.storeAccessToken(accessToken);

Log.i("Access Token:", accessToken.getToken());

Log.i("Access Secret:", accessToken.getTokenSecret());

long userID = accessToken.getUserId();

User user = twitterConnection.showUser(userID);

user.getName();

Thanks.

查看更多
Rolldiameter
6楼-- · 2019-01-09 11:51

There are a few tutorials here that can help you get an app running with twitter..

if you just need to retrieve info for a specific user, you can look here (includes source code):

Basic Adroid Twitter Integration

If you want to interact with twitter (e.g. post updates etc) then you will need to setup OAuth connection:

Android and Twitter integratin using OAuth

查看更多
登录 后发表回答