我試圖從Facebook和我正在開發的應用程序獲得Facebook用戶的電子郵件。問題是我可以得到名字和其他所有的東西,但不是電子郵件。我已經在這裏查找了所有常見問題,並且我看到了很多種方法可以實現,但沒有人在我的應用上工作。我希望你能幫我一把。Android - 如何獲取用戶電子郵件與Facebook的SDK 4+
LoginActivity.java(上創建)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
// Set up the login form.
// AppEventsLogger.activateApp(this);
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
populateAutoComplete();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList(
"public_profile", "email", "user_birthday", "user_friends"));
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject me, GraphResponse response) {
if (response.getError() != null) {
// handle error
} else {
String email = me.optString("email");
String id = me.optString("id");
String name = me.optString("name");
Toast.makeText(getApplicationContext(),
email+"/"+name,
Toast.LENGTH_LONG).show();
// send email and id to your web server
}
}
}).executeAsync();
}
@Override
public void onCancel() {
// App code
Toast.makeText(getApplicationContext(),
"Cancelado",
Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(getApplicationContext(),
"Error",
Toast.LENGTH_LONG).show();
}
});
// loginButton.setReadPermissions("user_friends");
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == R.id.login || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
mEmailSignInButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
attemptLogin();
}
});
mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);
}
OnActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
if (callbackManager.onActivityResult(requestCode, resultCode, data)) {
return;
}
}
@Override
public void onDestroy() {
super.onDestroy();
profileTracker.stopTracking();
}
檢查了這一點:http://stackoverflow.com/questions/29396265/i-need-users-email-address-after-successful-facebook-login-in-android- using-sdk – KishuDroid
您正在測試的帳戶是否有關聯的電子郵件,並且該電子郵件是否真正被激活? – user3268305
我已經試過了,它給了我一個錯誤FBuser,當然是電子郵件被激活 –