2013-07-12 102 views

回答

34

在致電Session.openActiveSession這樣做是爲了獲得許可補充一點:

List<String> permissions = new ArrayList<String>(); 
permissions.add("email"); 

Session.openActiveSession()最後一個參數應該是permissions。 現在您可以訪問user.getProperty("email").toString()

編輯:

這是我做的Facebook授權方式:

List<String> permissions = new ArrayList<String>(); 
permissions.add("email"); 

loginProgress.setVisibility(View.VISIBLE); 

//start Facebook session 
openActiveSession(this, true, new Session.StatusCallback() { 
    @Override 
    public void call(Session session, SessionState state, Exception exception) { 
     if (session.isOpened()) { 
      //make request to the /me API 
      Log.e("sessionopened", "true"); 
      Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { 
       @Override 
       public void onCompleted(GraphUser user, Response response) { 
        if (user != null) { 
         String firstName = user.getFirstName(); 
         String lastName = user.getLastName(); 
         String id = user.getId(); 
         String email = user.getProperty("email").toString(); 

         Log.e("facebookid", id); 
         Log.e("firstName", firstName); 
         Log.e("lastName", lastName); 
         Log.e("email", email); 
        } 
       } 
      }); 
     } 
    } 
}, permissions); 

添加這個方法將你的活動:

private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback callback, List<String> permissions) { 
    Session.OpenRequest openRequest = new Session.OpenRequest(activity).setPermissions(permissions).setCallback(callback); 
    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; 
} 
+0

不能在logcat中工作我得到空指針異常 – user2541633

+0

向我們顯示您的代碼和導致NullPointerException的確切行,所以我們可以。 –

+0

在方法openActiveSession(Activity,boolean,Session.StatusCallback)類型Session不適用於參數(Login,boolean,new Session.StatusCallback(){},列表)。這是在eclipse中顯示的錯誤 – user2541633

-1

嘗試這篇文章。希望你的問題將得到解決Click Here

順便說一句,你需要使用user.asMap().get("email").toString());接收用戶的電子郵件ID。 此外,您還需要分配到這一些標籤像lblEmail.setText其中lblEmail是一個TextView。

+0

我用簡單的Android按鈕。所以我怎麼可以把權限放在那裏。現在我只需要在那裏准許郵件。但我怎麼可以把權限和地點? – user2541633

+1

@ user2541633'LoginButton authButton =(LoginButton)findViewById(R.id.authButton); authButton.setOnErrorListener(new OnErrorListener(){ authButton.setReadPermissions(Arrays.asList(「basic_info」,「email」));' –

+0

這件事情在模擬器上工作,但是當我在手機上安裝我的應用程序時,它被導向到facebook android應用程序但沒有給我任何迴應 – user2541633

3

繼葉戈爾n個建議,我改變我的舊代碼

Session.openActiveSession(this, true, statusCallback); 

蒙山這個新:

List<String> permissions = new ArrayList<String>(); 
permissions.add("email"); 
Session.openActiveSession(this, true, permissions, statusCallback); 

現在FB詢問郵件的權限的用戶,我可以在響應讀它。

相關問題