我正在尝试获取个人资料照片,来自Twitter整合的电子邮件

时间:2016-06-28 06:21:15

标签: android twitter

public class AndroidTwitterExample extends Activity {
    private TwitterLoginButton twitterButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        setUpViews();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        twitterButton.onActivityResult(requestCode, resultCode, data);
    }

    private void setUpViews() {
        setUpTwitterButton();
    }

    private void setUpTwitterButton() {
        twitterButton = (TwitterLoginButton) findViewById(R.id.twitter_button);
        twitterButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                String username =result.data.getUserName();
                Toast.makeText(getApplicationContext(),
                        username,
                        Toast.LENGTH_SHORT).show();
                Log.d("response",result.response.getBody().toString());

                setUpViewsForTweetComposer();
            }

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

    private void setUpViewsForTweetComposer() {
        TweetComposer.Builder builder = new TweetComposer.Builder(this)
                .text("Just setting up my Fabric!");
        builder.show();
    }
}

我尝试了上面的代码,但结果对象我只获得用户名和userId如何获取个人资料图片和电子邮件。

1 个答案:

答案 0 :(得分:1)

为了获得个人资料照片,请使用这个回调成功:

   Twitter.getApiClient(result.data).getAccountService()
            .verifyCredentials(true, false, new Callback<User>() {
                @Override
                public void failure(TwitterException e) {
                    //If any error occurs handle it here
                }

                @Override
                public void success(Result<User> userResult) {
                    //If it succeeds creating a User object from userResult.data
                    User user = userResult.data;

                    //Getting the profile image url
                    String profileImage = user.profileImageUrl.replace("_normal", "");       

                    Log.i("profile Image",""+profileImage);

                }
            });

和电子邮件ID在这里提取是参考网址:     https://docs.fabric.io/android/twitter/log-in-with-twitter.html#request-user-email-address