如何获得电子邮件ID twitter集成

时间:2017-03-14 10:04:40

标签: android twitter twitter-fabric

我正在使用面料进行Twitter集成,现在问题是我能够获得除电子邮件地址以外的所有用户详细信息。以下是我的代码,任何人都可以帮助我

public void login(Result<TwitterSession> result) {

        //Creating a twitter session with result's data
        TwitterSession session = result.data;

        //Getting the username from session
        final String username = session.getUserName();

        //This code will fetch the profile image URL
        //Getting the account service of the user logged in
        Call<User> userResult = Twitter.getApiClient(session).getAccountService().verifyCredentials(true, false);
        userResult.enqueue(new Callback<User>() {

            @Override
            public void failure(TwitterException e) {

            }

            @Override
            public void success(Result<User> userResult) {

                User user = userResult.data;
                String twitterImage = user.profileImageUrl;

                try {
                    Log.d("imageurl", user.profileImageUrl);
                    Log.d("name", user.name);
                    System.out.println("Twitter Email"+user.email);
                    //Log.d("email", user.email);
                    Log.d("des", user.description);
                    Log.d("followers ", String.valueOf(user.followersCount));
                    Log.d("createdAt", user.createdAt);
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

        });
    }

4 个答案:

答案 0 :(得分:6)

这是我获取用户的用户电子邮件的方式:

final TwitterSession twitterSession = result.data;
twitterAuthClient.requestEmail(twitterSession, new com.twitter.sdk.android.core.Callback<String>() {
    @Override
    public void success(Result<String> emailResult) {
        String email = emailResult.data;
        // ...
    }

    @Override
    public void failure(TwitterException e) {
        callback.onTwitterSignInFailed(e);
    }
});

因此,您必须在获得授权后成功TwitterAuthClient.requestEmail()时致电Result<TwitterSession>

请注意,您必须与Twitter支持小组联系才能访问用户&#39;您的应用的电子邮件。将显示带有此错误消息。

答案 1 :(得分:3)

以下是我从twitter获取详细信息的代码:

   private void intializeTwitterUI() {
      loginButton = (TwitterLoginButton)             
      findViewById(R.id.twitter_login_button);
      loginButton.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            // The TwitterSession is also available through:
            // TWITTER.getInstance().core.getSessionManager().getActiveSession()
            TwitterSession session = result.data;
            // TODO: Remove toast and use the TwitterSession's userID
            // with your app's user model
            String msg = "Twitter: @" + session.getUserName() + " logged in! (#" + session.getUserId() + ")";
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            /**
             *
             */
            AccountService _AccountService = Twitter.getApiClient(result.data).getAccountService();
            _AccountService.verifyCredentials(true, true).enqueue(new retrofit2.Callback<User>() {
                @Override
                public void onResponse(Call<User> call, retrofit2.Response<User> response) {
                    Log.d(TAG, "Twitter user is: " + response.toString());
                    Log.d(TAG, "Twitter-Email" + response.body().email);
                    Log.d(TAG, "Twitter-profileImage" + response.body().profileImageUrl);
                    Log.d(TAG, "Twitter-ID" + response.body().id);
                    twitterDetails = response.body().email + "," + response.body().profileImageUrl + "," + response.body().id;

                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {
                    Log.e(TAG, "verifyCredentials failed! " + t.getLocalizedMessage());
                }
            });



        }

答案 2 :(得分:1)

对于那些想要用 Kotlin 做的人,可以尝试这种方式:

    val session = TwitterCore.getInstance().sessionManager.activeSession as TwitterSession
    val authClient = TwitterAuthClient()
    authClient.requestEmail(session, object : Callback<String>(){
            override fun failure(exception: TwitterException?) {
                email.setText("Welcome to Twitter")
            }

            override fun success(result: Result<String>?) {
                email.setText("Welcome " + result?.data)
            }

        })

答案 3 :(得分:0)

可以向用户请求电子邮件地址,但需要将您的应用列入白名单。 点击这里Is there a way to get an user's email ID after verifying his/her Twitter identity using OAuth?