如何获取电子邮件 ID 推特集成

How to get email id twitter integration

我正在使用 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();
                }


            }

        });
    }

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

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);
    }
});

所以你必须在授权成功Result<TwitterSession>后调用TwitterAuthClient.requestEmail()

请注意,您必须联系 Twitter 支持才能为您的应用启用对用户电子邮件的访问权限。将显示一条错误消息。

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

这是我从 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());
                }
            });



        }

对于那些想用 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)
            }

        })