如何从Twitter Api获取用户信息

问题描述 投票:2回答:2

我无法从Twitter Api获取信息,没有提供任何信息,我需要用户个人资料图片,电子邮件和用户个人帐户信息,我几乎尝试了网上发现的所有东西,

ConfigurationBuilder builder = new ConfigurationBuilder();
         builder.setOAuthConsumerKey(CONSUMER_KEY);
         builder.setOAuthConsumerSecret(CONSUMER_SECRET);
         Configuration configuration = builder.build();

         TwitterFactory factory = new TwitterFactory(configuration);
         twitter = factory.getInstance();

            try {
                User user = twitter.showUser(mTwitter.getUsername());
            } catch (TwitterException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

但是,它说auth错误没有提供任何信息。

android
2个回答
3
投票

使用此功能,您可以获取用户信息

1.

    twitter_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            client.authorize(SignUpToConnectActivity.this, new Callback<TwitterSession>() {
                @Override
                public void success(Result<TwitterSession> result) {
                    String oauth_token_secret = result.data.getAuthToken().secret;
                    String oauth_token = result.data.getAuthToken().token;
                    TwitterSession user = result.data;
                    getUserDetails(user);
                }
                @Override
                public void failure(TwitterException exception) {
                    //
                }
            });
        }
    })

2.

    public void getUserDetails(TwitterSession twitterSession) {
        Twitter.getApiClient(twitterSession).getAccountService().verifyCredentials(true, false).enqueue(new Callback<User>() {
            @Override
            public void success(Result<User> userResult) {
                try {
                    User user = userResult.data;
                    String fullname = user.name;
                    String twitterID = user.getId();
                    String userSocialProfile = user.profileImageUrl;
                    String userEmail = user.email;
                    String userFirstNmae = fullname.substring(0, fullname.lastIndexOf(" "));
                    String userLastNmae = fullname.substring(fullname.lastIndexOf(" "));
                    String userScreenName = user.screenName;

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void failure(TwitterException e) {
            }
        });
    }

使用此功能,您可以获取用户信息。


1
投票

这适用于Twitter 3+ api调用更改

twitterLoginButton.setCallback(new Callback<TwitterSession>() {
    @Override
    public void success(Result<TwitterSession> result) {
        // Do something with result, which provides a TwitterSession for making API calls                
        TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
        AccountService accountService = twitterApiClient.getAccountService();
        Call<com.twitter.sdk.android.core.models.User> call = accountService.verifyCredentials(true, true, true);
        call.enqueue(new Callback<com.twitter.sdk.android.core.models.User>() {
            @Override
            public void success(Result<com.twitter.sdk.android.core.models.User> result) {
               //here we go User details
            }

            @Override
            public void failure(TwitterException exception) {
            }
        });
    }

    @Override
    public void failure(TwitterException exception) {
        // Do something on failure
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.