How to get user information from twitter in android app?

懵懂的女人 提交于 2019-12-17 09:52:40

问题


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.


回答1:


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.




回答2:


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




回答3:


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)
            {

            }

        });



回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/6395945/how-to-get-user-information-from-twitter-in-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!