2015-07-28 42 views
3

我有一個按此步驟創建的Twitter登錄http://docs.fabric.io/android/twitter/authentication.html,它顯示用戶名,除了用戶的全名。Android如何在通過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 
       TwitterSession session = Twitter.getSessionManager().getActiveSession(); 
       TwitterAuthToken authToken = session.getAuthToken(); 
       String token = authToken.token; 
       String secret = authToken.secret; 

       long userID = result.data.getUserId(); 

       //This throws an Error Cannot find symbol showUser() 
       User user = twitter.showUser(userID); 

       //This throws error as well 
       String fullname= user.getName(); 


       Log.d("login:", "twitter:success"); 
       Log.d("username", result.data.getUserName()); 
       Log.d("full name", fullname); 

      } 

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

請我究竟能得到用戶的全名?

+0

使用TwitterApiClient:https://docs.fabric.io/android/twitter /access-rest-api.html –

回答

0

您可以使用此功能讓用戶名:

public void processToken(String callbackUrl) { 
     mProgressDlg.setMessage("Finalizing ..."); 
     mProgressDlg.show(); 
     final String verifier = getVerifier(callbackUrl); 
     new Thread() { 
      @Override 
      public void run() { 
       int what = 1; 

       try { 
        mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, 
          verifier); 

        mAccessToken = new AccessToken(
          mHttpOauthConsumer.getToken(), 
          mHttpOauthConsumer.getTokenSecret()); 

        configureToken(); 

        User user = mTwitter.verifyCredentials(); 

        Log.i("user.getName()="+user.getName(), "786"); 
        try{ 
         firstName=user.getName().split(" ")[0]; 
         lastName=user.getName().split(" ")[1]; 
        }catch(Exception e){ 
         e.printStackTrace(); 
         firstName = user.getName(); 
         lastName = ""; 
        } 
        Log.i("firstName="+firstName+" lastName="+lastName, "786"); 
        UserId=user.getId(); 

        what = 0; 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0)); 
      } 
     }.start(); 
    } 
+0

感謝@Ketan,但沒有一個更簡單的方法來做到這一點? –

3

使用TwitterApiClient和用戶是這樣的:

Twitter 
    .getApiClient(session) 
    .getAccountService() 
    .verifyCredentials(true, false, new Callback<User>() { 

     @Override 
     public void failure(TwitterException e) { 
     } 

     @Override 
     public void success(Result<User> userResult) { 
      User user = userResult.data; 
      .... access User fields .... 
     } 

    }); 
+0

它顯示沒有這樣的verifyCredentials方法。請你能告訴我這裏有什麼問題嗎? –

+0

我們需要查看您的代碼。你可能想發佈你自己的問題,並從這裏鏈接到它。 –

相關問題