使用Fabric SDK获取Twitter用户电子邮件

时间:2015-04-12 14:41:53

标签: android twitter twitter-fabric

我正在使用Fabric使用twitter登录。

这是我获取用户数据的方式:

loginButton.setCallback(new Callback<TwitterSession>() {
                @Override
                public void success(Result<TwitterSession> result) {
                    // Do something with result, which provides a TwitterSession for making API calls
                    AccountService ac = Twitter.getApiClient(result.data).getAccountService();
                    ac.verifyCredentials(true, true, new Callback<com.twitter.sdk.android.core.models.User>() {
                        @Override
                        public void success(Result<com.twitter.sdk.android.core.models.User> result) {
                            String imageUrl = result.data.profileImageUrl;
                            String email = result.data.email;
                            String userName = result.data.name;
                            System.out.println(imageUrl);
                            System.out.println(email);
                            System.out.println(userName);
                        }

                        @Override
                        public void failure(TwitterException e) {

                        }
                    });

                }

这是正常工作,但我打印到日志时email变量为null除外。是否有其他方式来获取用户电子邮件?

2 个答案:

答案 0 :(得分:2)

- 这是解决方案!

twitauthobj.requestEmail(twitsessionobj,new Callback<String>() {
                    @Override
                    public void success(Result<String> stringResult) {
                        'You code here'

                    }

                    @Override
                    public void failure(TwitterException e) {

                    }
                });

- 谢谢告诉我是否有效!

答案 1 :(得分:0)

为了绕过Twitter无用的请求电子邮件活动并修复漏洞,我挖掘了源代码并将其解决了:

new Retrofit.Builder()
        .client(getClient(sessionResult))
        .baseUrl(new TwitterApi().getBaseHostUrl())
        .addConverterFactory(getFactory())
        .build()
        .create(EmailService.class)
        .getEmail()
        .enqueue(new Callback<User>() {
            @Override
            public void success(Result<User> result) {
                String email = result.data.email;
                // Handle the result
                if (email == null) {
                    TwitterProvider.this.failure(
                            new TwitterException("Your application may not have access to"
                                                         + " email addresses or the user may not have an email address. To request"
                                                         + " access, please visit https://support.twitter.com/forms/platform."));
                } else if (email.equals("")) {
                    TwitterProvider.this.failure(
                            new TwitterException("This user does not have an email address."));
                } else {
                    mCallbackObject.onSuccess(createIdpResponse(sessionResult.data, email));
                }
            }

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

private OkHttpClient getClient(Result<TwitterSession> sessionResult) {
    return OkHttpClientHelper.getOkHttpClient(
            sessionResult.data,
            TwitterCore.getInstance().getAuthConfig(),
            TwitterCore.getInstance().getSSLSocketFactory());
}

private GsonConverterFactory getFactory() {
    return GsonConverterFactory.create(
            new GsonBuilder()
                    .registerTypeAdapterFactory(new SafeListAdapter())
                    .registerTypeAdapterFactory(new SafeMapAdapter())
                    .registerTypeAdapter(BindingValues.class, new BindingValuesAdapter())
                    .create());
}

EmailService

interface EmailService {
    @GET("/1.1/account/verify_credentials.json?include_email=true?include_entities=true?skip_status=true")
    Call<User> getEmail();
}