2015-05-27 64 views
0
  1. 我試圖從登錄後的用戶那裏獲取電子郵件,因爲我想檢查用戶是否已經在我的數據庫上收到了他的電子郵件。Facebook android sdk 4. * - 如何獲取登錄的用戶電子郵件?

  2. 但我不知道如何獲得電子郵件。我使用getName從用戶那裏獲得名稱,我想用類似的方法獲得用戶的電子郵件。

我的活動代碼:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     FacebookSdk.sdkInitialize(getApplicationContext()); 
     callbackManager = CallbackManager.Factory.create(); 

     setContentView(R.layout.activity_main); 

     info = (TextView) findViewById(R.id.info); 
     loginButton = (LoginButton) findViewById(R.id.login_button); 

     loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       Profile profile = Profile.getCurrentProfile(); 
       info.setText(message(profile)); 

       String userId = loginResult.getAccessToken().getUserId(); 

      } 

      @Override 
      public void onCancel() { 
       info.setText("Login attempt cancelled."); 
      } 

      @Override 
      public void onError(FacebookException e) { 
       e.printStackTrace(); 
       info.setText("Login attempt failed."); 
      } 
     }); 
    } 

在恢復

public void onResume() { 
    super.onResume(); 
    Profile profile = Profile.getCurrentProfile(); 
    info.setText(message(profile)); 
} 

上的活動結果

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     callbackManager.onActivityResult(requestCode, resultCode, data); 
    } 

    private String message(Profile profile) { 
     StringBuilder stringBuffer = new StringBuilder(); 
     if (profile != null) { 
      stringBuffer.append("Welcome ").append(profile.getName()); 
     } 
     return stringBuffer.toString(); 
    } 
} 

謝謝。

回答

0

我認爲您需要獲得訪問用戶電子郵件的權限。嘗試這樣的事情。

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

然後看看你是否可以訪問電子郵件之後。

String email = profile.getProperty("email").toString(); 

一切順利。

0

要訪問「電子郵件」,您必須設置它的權限。

//FB Login 
loginButton = (LoginButton) findViewById(R.id.login_button); 
loginButton.setReadPermissions(Arrays.asList("public_profile", "email")); 

然後你可以在onSuccess中訪問它。

0

這是你需要在4.2.0版本做什麼,如果Facebook的SDK爲Android:

GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), 
      new GraphRequest.GraphJSONObjectCallback() { 
       @Override 
       public void onCompleted(JSONObject object, GraphResponse response) { 
        Log.d("onCompleted",object.toString()); 
       } 
      }); 
    Bundle parameters = new Bundle(); 
    parameters.putString("fields", "email"); 
    request.setParameters(parameters); 
    request.executeAsync(); 

此代碼應被放入onSucess()回調方法。

相關問題