onSuccess()
方法でグラフAPI呼び出し:
facebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.i("LoginActivity", response.toString());
// Get facebook data from login
Bundle bFacebookData = getFacebookData(object);
emailStr = bFacebookData.get("email").toString();
fnameStr = bFacebookData.get("first_name").toString();
lnameStr = bFacebookData.get("last_name").toString();
facebookIdStr = bFacebookData.get("idFacebook").toString();
genderStr = bFacebookData.get("gender").toString();
dp_url = bFacebookData.get("profile_pic").toString();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook
request.setParameters(parameters);
request.executeAsync();
}
をそしてgetFacebookData(object)
方法は次のようにする必要があります:これは私の作品
private Bundle getFacebookData(JSONObject object) {
try {
bundle = new Bundle();
String id = object.getString("id");
try {
URL profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");
Log.i("profile_pic", profile_pic + "");
bundle.putString("profile_pic", profile_pic.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
bundle.putString("idFacebook", id);
if (object.has("first_name"))
bundle.putString("first_name", object.getString("first_name"));
if (object.has("last_name"))
bundle.putString("last_name", object.getString("last_name"));
if (object.has("email"))
bundle.putString("email", object.getString("email"));
if (object.has("gender"))
bundle.putString("gender", object.getString("gender"));
if (object.has("birthday"))
bundle.putString("birthday", object.getString("birthday"));
if (object.has("location"))
bundle.putString("location", object.getJSONObject("location").getString("name"));
System.out.println("BUNDLEEE: "+bundle);
return bundle;
} catch (JSONException e) {
e.printStackTrace();
}
return bundle;
}
。 ProfileTrackerも使用することができますが、これはより信頼性の高いものです。 ありがとうございました