【发布时间】:2023-03-03 12:46:01
【问题描述】:
自从 Facebook 在提供用户数据方面变得更加安全以来,情况发生了翻天覆地的变化。在使用 Bundles 和 Graph Request 之前,我可以使用 Graph 2.12 在我的管理员帐户中获取性别和年龄范围。
//Log in Facebook
private void signInFacebook() {
//Request a read permission of user's info from Facebook
//Data provided by Facebook will be used for Firebase FireStore
LoginManager.getInstance().logInWithReadPermissions(LogIn.this, Arrays.asList("email", "public_profile"));
LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
mStateOfSuccess = false;
//Dismiss any snackbar first before showing a new one
mSnackBar.dismiss();
mSnackBar.show();
Log.d(TAG, "facebook:onSuccess:" + loginResult);
//Bundle is use for passing data as K/V pair like a Map
Bundle bundle=new Bundle();
//Fields is the key of bundle with values that matched the proper Permissions Reference provided by Facebook
bundle.putString("fields","id, email, first_name, last_name, gender, age_range");
//Graph API to access the data of user's facebook account
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("Login Success", response.toString());
//For safety measure enclose the request with try and catch
try {
//The get() or getString() key should be included in Bundle otherwise it won't work properly
//If not then error dialog will be called
//First re-initialize jSON object to a new Contructor with parameter that is equal to a jSON format age range
JSONObject ageRange = new JSONObject(object.getString("age_range"));
//Log in using Facebook with Firebase
loginToFirebaseUsingFacebook(loginResult.getAccessToken()
,object.getString("first_name")
,object.getString("last_name")
//Then get again get a string from object itself for the minimum age range
//The idea is that we need to get minimum age only written im string format
//not the whole age range data that is written in jSOM format
,ageRange.getString("min")
,object.getString("gender")
,object.getString("email")
);
}
//If no data has been retrieve throw some error
catch (JSONException e) {
ErrorDialog(e.getMessage(),"facebookAuth");
}
}
});
//Set the bundle's data as Graph's object data
request.setParameters(bundle);
//Execute this Graph request asynchronously
request.executeAsync();
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
ErrorDialog("Request has canceled.","facebookAuth");
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
ErrorDialog(String.valueOf(error),"facebookAuth");
}
});
}
但现在一切都发生了变化,即使访问我自己的帐户(管理员)也无法提供我需要的数据。根据他们的文档enter image description here
【问题讨论】:
-
专门为这两个字段添加了两个新权限,您需要先询问用户,然后您的应用才能立即读取此数据。
标签: android facebook-graph-api