2017-03-14 65 views
2

我正在使用微博集成使用面料,現在問題是我能夠獲得除電子郵件地址外的所有用戶的詳細信息。以下是我的代碼,任何一個可以幫助我與如何獲得電子郵件ID微博整合

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

這是我如何獲取一個用戶的用戶電子郵件地址:

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支持以便爲您的應用程序訪問用戶的電子郵件。這將顯示一條錯誤消息。

+0

應用程序崩潰它給 java.lang.RuntimeException:失敗的結果ResultInfo – chris

+0

什麼是twitterAuthClient? – chris

+0

'twitterAuthClient'是一個'TwitterAuthClient'。我想你已經使用了一個用於初始授權。你能發佈整個堆棧跟蹤嗎?只有第一行不多說。 –

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



     } 
+0

這麼多錯誤得到 – chris

+0

你需要相應地修改你的代碼。並請通過此鏈接https://docs.fabric.io/android/twitter/log-in-with-twitter.html –

+0

感謝您的幫助 – chris

0

對於那些想與科特林做可以這樣嘗試:

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

     }) 
相關問題