如何从Facebook登录获取电子邮件ID
添加的权限是
mFBLoginButton.setReadPermissions("user_friends");
mFBLoginButton.setReadPermissions("public_profile");
mFBLoginButton.setReadPermissions("email");
mFBLoginButton.setReadPermissions("user_birthday");
我正在通过Facebook教程进行登录并访问我正在使用GraphRequest
代码的电子邮件,如下所示
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me?fields=email",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
if (response != null)
Log.d(TAG, " response " + response.toString());
}
}
).executeAsync();
但我没有在回复中收到电子邮件。 请帮助我一些人
答案 0 :(得分:0)
正如'Md Abdul Gafur'所提到的,请确保您的Facebook帐户已正确设置。
将依赖项添加到build.gradle:
编译'com.facebook.android:facebook-android-sdk:4.2.0'
将facebook活动添加到AndroidManifest.xml:
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
然后在你的actvitiy覆盖onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
使用以下网址登录您的活动:
FacebookSdk.sdkInitialize(mContext.getApplicationContext());
FacebookSdk.setApplicationId(mApiKey);
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject user, GraphResponse response) {
if (response.getError() != null) {
// handle error
} else {
if (user != null) {
// Get the data you need
String email = user.optString("email", "");
} else {
//handle null user
}
}
}
});
request.executeAsync();
}
@Override
public void onCancel() {
//handle cancel
}
@Override
public void onError(FacebookException exception) {
// handle exception
}
});
LoginManager.getInstance().logInWithReadPermissions(mContext, Collections.singletonList("public_profile"));
答案 1 :(得分:0)
您需要在登录获取权限时使用参数询问permissions`,您还必须考虑这样一个事实:并非所有登录用户都有分配给其帐户的电子邮件,也有访问权限的权限。
可以使用手机号码打开并验证自己的Facebook帐户,因此您帐户中不存在电子邮件的可能性。
希望此信息有所帮助!
答案 2 :(得分:0)
使用此代码。它的成功运作。您可以通过此获取所有用户数据。
loginButton = (LoginButton) findViewById(R.id.login_button);
List < String > permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {@Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity", response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
name = object.getString("name");
email = object.getString("email");
gender = object.getString("gender");
birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});