问题
I am implementing facebook's SDK for an android app. I have a ProfilePictureView
, that requires, to be filled:
profilePictureView.setProfileId(id)
Yesterday, if I used the App-scoped Id of the profile (the one returned by Graph API) it worked: that's the only id I have, because apparently is against Facebook policy to get the "real" id.
But today, without any modification, it returns a null image. I am guessing they changed the backend, but if so, what is the purpose of ProfilePictureView
if it requires an id that is against Facebook's policy to get?
回答1:
From 26 Mar 2018, all solutions related to manual link don't work anymore
Use the code below
private static String FACEBOOK_FIELD_PROFILE_IMAGE = "picture.type(large)";
private static String FACEBOOK_FIELDS = "fields";
private void getFacebookData() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
(object, response) -> {
updateAvatar(getImageUrl(response));
});
Bundle parameters = new Bundle();
parameters.putString(FACEBOOK_FIELDS, FACEBOOK_FIELD_PROFILE_IMAGE);
request.setParameters(parameters);
request.executeAsync();
}
private static String FACEBOOK_FIELD_PICTURE = "picture";
private static String FACEBOOK_FIELD_DATA = "data";
private static String FACEBOOK_FIELD_URL = "url";
private String getImageUrl(GraphResponse response) {
String url = null;
try {
url = response.getJSONObject()
.getJSONObject(FACEBOOK_FIELD_PICTURE)
.getJSONObject(FACEBOOK_FIELD_DATA)
.getString(FACEBOOK_FIELD_URL);
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
回答2:
I will answer myself, since this question specifically asks for "getting a profile picture given the id", and the enlightening answer of Levon works only for the current user.
private static String FACEBOOK_FIELD_PROFILE_IMAGE = "picture.type(large)";
private static String FACEBOOK_FIELDS = "fields";
private static String FACEBOOK_FIELD_PICTURE = "picture";
private static String FACEBOOK_FIELD_DATA = "data";
private static String FACEBOOK_FIELD_URL = "url";
public static void getFacebookProfileUrl(String id, final Response<String> response) {
GraphRequest request = GraphRequest.newGraphPathRequest(
AccessToken.getCurrentAccessToken(),
"/" + id,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graph) {
String imgUrl = getImageUrl(graph);
// imgUrl is the url of the profilepic of the user with given id.
// Works both for "real" id and "app-scoped" id
}
});
Bundle parameters = new Bundle();
parameters.putString(FACEBOOK_FIELDS, FACEBOOK_FIELD_PROFILE_IMAGE);
request.setParameters(parameters);
request.executeAsync();
}
private static String getImageUrl(GraphResponse response) {
String url = null;
try {
url = response.getJSONObject()
.getJSONObject(FACEBOOK_FIELD_PICTURE)
.getJSONObject(FACEBOOK_FIELD_DATA)
.getString(FACEBOOK_FIELD_URL);
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
来源:https://stackoverflow.com/questions/49516674/use-facebook-app-scoped-id-for-profilepictureview