从Twitter(Android)获取用户的电子邮件地址

时间:2015-03-19 05:38:34

标签: android twitter twitter4j

如何通过Twitter API获取用户的电子邮件地址?我使用Twitter4j登录twitter

5 个答案:

答案 0 :(得分:0)

我希望这会有效 请在请求回电结果中使用以下代码

isEqual:

答案 1 :(得分:0)

检查这些链接

https://dev.twitter.com/twitterkit/android/installation

https://dev.twitter.com/twitterkit/android/log-in-with-twitter

成功登录后写下面的代码

TwitterAuthClient authClient = new TwitterAuthClient();
authClient.requestEmail(session, new Callback<String>() {
   @Override
    public void success(Result<String> result) {
    // Do something with the result, which provides the email address
}

@Override
public void failure(TwitterException exception) {
  // Do something on failure
}
});

答案 2 :(得分:0)

首先请确保您在Twitter应用Request email addresses from users部分

中激活Permissions

enter image description here

然后按照Twitter文档步骤

1。 Installation process 只是导入 twitter的sdk包

  

我只使用核心包

compile 'com.twitter.sdk.android:twitter-core:3.1.1'

在您的活动onCreate方法

上初始化Twitter工具包
Twitter.initialize(this);

将您的应用ID和密码添加到strings.xml文件

<string android:name="com.twitter.sdk.android.CONSUMER_KEY">XXXXXXXXXXX</string>
<string android:name="com.twitter.sdk.android.CONSUMER_SECRET">XXXXXXXXXXX</string>

2。 Add the Button

<com.twitter.sdk.android.core.identity.TwitterLoginButton
     android:id="@+id/twitter_connect"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
  提示:您可以使用自定义按钮follow this simple answer
private TwitterLoginButton twitterBtn;

...

twitterBtn = (TwitterLoginButton) findViewById(R.id.twitter_connect);
twitterBtn.setCallback(new Callback<TwitterSession>() {
    @Override
    public void success(Result<TwitterSession> result) {
        // Do something with result, which provides a TwitterSession for making API calls
        // which is do the callback from twitter to get the Email
        TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
        TwitterAuthToken authToken = session.getAuthToken();
        String token = authToken.token;
        String secret = authToken.secret;

        TwitterAuthClient authClient = new TwitterAuthClient();
        authClient.requestEmail(session, new Callback<String>() {
            @Override
            public void success(Result<String> result) {
                // Do something with the result, which provides the email address
                // the email is saved in the result variable 'result.data'
                Toast.makeText(getBaseContext(), "Email" + result.data, Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(TwitterException exception) {
                // Do something on failure
            }
        });
    }

    @Override
    public void failure(TwitterException exception) {
        // Do something on failure
    }
});

接下来,将身份验证活动的结果传递回按钮:

// Pass the activity result to the login button.
twitterBtn.onActivityResult(requestCode, resultCode, data);

喜欢这样:

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

    // Pass the activity result to the login button.
    twitterBtn.onActivityResult(requestCode, resultCode, data);
}

答案 3 :(得分:0)

Twitter sdk可以帮助您提供电子邮件,您只需提供请求电子邮件的权限即可。 Twitter sdk将检查您的Twitter帐户上的电子邮件的可见性,如果它有权限,它将为您提供电子邮件,否则我们需要处理它,如果它不发送电子邮件。

client.requestEmail(twitterSession, new Callback<String>() {
        @Override
        public void success(Result<String> result) {
            Log.d(TAG, "Twitter success email : " + result.data);
            email = result.data;
            if (email == null){

            }else {
            Log.d(TAG,"Email"+ email);  
            }

        }

        @Override
        public void failure(TwitterException exception) {
            Log.d(TAG, "Twitter failed  email : " + exception);
        }
    });

答案 4 :(得分:0)

要获取Twitter电子邮件和个人资料图片的URL,用户名和ID,以调用requestEmail方法。

您还允许您在Twitter的开发人员帐户中请求电子邮件。参见下面的图像。

Retrieve Email id on twitter login

使用以下代码获取用户详细信息。

    btnTwitterLogin.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
            TwitterAuthToken authToken = session.getAuthToken();
            TwitterAuthClient authClient = new TwitterAuthClient();
            authClient.requestEmail(session, new Callback<String>() {
                @Override
                public void success(Result<String> result) {
                    String email =result.data;
                    String token = authToken.token;
                    String secret = authToken.secret;
                    String username = session.getUserName();
                    long userId = session.getUserId();
                    Log.e("neem", "success: \n Token =>  " + token + "\n Secret: " + secret + " \n Username: " + username + "\n Userid: " + userId+" \n Email: "+email);

                }

                @Override
                public void failure(TwitterException exception) {
                    Log.e("neem", "failure: "+exception.getMessage() );
                }
            });
            TwitterCore.getInstance().getApiClient(result.data).getAccountService().verifyCredentials(false,true,false).enqueue(new Callback<User>() {
                @Override
                public void success(Result<User> userResult) {
                    try {

                        String imageUrl = userResult.data.profileImageUrl;
                        String username = userResult.data.screenName;
                        Log.e("neem", "acount services: imageurl: "+imageUrl+"\n username: "+username );

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

        }

        @Override
        public void failure(TwitterException exception) {
            ShowToast(mcontext, "Login failed due to: \n "+exception.getMessage());
        }
    });