2013-06-13 70 views
1

在這段代碼中GraphUser沒有返回電子郵件ID和聯繫人號碼。圖形api沒有返回用戶電子郵件ID

@Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     uiHelper.onSaveInstanceState(outState); 
    } 

    private void onSessionStateChange(Session session, SessionState state, Exception exception) { 
     if (state.isOpened()) { 
      // userInfoTextView.setVisibility(View.VISIBLE); 

       // Request user data and show the results 
       Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { 

        @Override 
        public void onCompleted(GraphUser user, Response response) { 
         if (user != null) { 
          // Display the parsed user info 

         String email= (String) response.getGraphObject().getProperty("email"); 

          System.out.println("yahooooooooooo "+email); 
          buildUserInfoDisplay(user); 
         } 
        } 
       }); 
     } else if (state.isClosed()) { 
      Log.i(TAG, "Logged out..."); 
     } 
    } 

    private String buildUserInfoDisplay(GraphUser user) { 
     StringBuilder userInfo = new StringBuilder(""); 



     // Example: typed access (name) 
     // - no special permissions required 
     userInfo.append(String.format("Name: %s\n\n", 
      user.getName())); 

     // Example: typed access (birthday) 
     // - requires user_birthday permission 
     userInfo.append(String.format("Birthday: %s\n\n", 
      user.getBirthday())); 


     userInfo.append(String.format("Birthday: %s\n\n", 
       user.getBirthday())); 


     userInfo.append(String.format("Gender: %s\n\n", 
       user.getProperty("gender"))); 

     userInfo.append(String.format("Iddddddd: %s\n\n", 
       user.getId())); 
     // Example: partially typed access, to location field, 
     // name key (location) 
     // - requires user_location permission 
     userInfo.append(String.format("Location: %s\n\n", 
      user.getLocation().getProperty("name"))); 

     // Example: access via property name (locale) 
     // - no special permissions required 
     userInfo.append(String.format("Locale: %s\n\n", 
      user.getProperty("locale"))); 
     return userInfo.toString(); 
    } 
+1

你有'email'權限嗎? –

+0

對不起如何添加權限 –

回答

9

默認情況下,你不會得到的電子郵件,電子郵件必須指定權限

Session.StatusCallback statusCallback = new Session.StatusCallback() 
     { 

      @Override 
      public void call(Session session, SessionState state, Exception exception) 
      { 
       if(session.isOpened()) 
       { 
        Request.executeMeRequestAsync(session, new Request.GraphUserCallback() 
        { 

         @Override 
         public void onCompleted(GraphUser user, Response response) 
         { 

          if(user != null) 
          { 
           try 
           { 
            System.out.println("Graph Inner Json"+user.getInnerJSONObject()); 
            String email = user.getInnerJSONObject().getString("email"); 
           } 
          } 
         } 
        } 
       } 
      } 

YourActivityName.openActiveSession(this, true, statusCallback); 

這是我們的openActiveSession()我們指定的權限

private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback statusCallback) 
    { 
     OpenRequest openRequest = new OpenRequest(activity); 
     openRequest.setPermissions(Arrays.asList("user_birthday", "email")); 
     openRequest.setCallback(statusCallback); 

     Session session = new Session.Builder(activity).build(); 

     if(SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) 
     { 
      Session.setActiveSession(session); 
      session.openForRead(openRequest); 

      return session; 
     } 

     return null; 
    } 

和內部onActivityResult()唐別忘了

if(Session.getActiveSession() != null) 
     { 
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
     } 
+1

感謝您的回答,它完美的作品! – VansFannel

1

我得到了另一種情況,我通過了所有的權限,但仍然出現錯誤「沒有價值的電子郵件」。其原因是沒有爲該Facebook帳戶驗證主要電子郵件(要查看Facebook中的主要電子郵件,請轉到帳戶設置 - >電子郵件)。 因此,如果沒有主要的電子郵件驗證的Facebook帳戶,那麼你將永遠得到這個錯誤。請確保您正在訪問的Facebook帳戶有一個經過驗證的主要電子郵件。

相關問題