Facebook ログインを Android アプリケーションに統合しようとしています。今のところ、ログに記録されたユーザーに関する新しい行を解析バックエンドで取得します。これには、彼の本当の名前、userName のいくつかの奇妙な値 (例:SnvvKEsIv6tX7...)、および Json であり、次の値を含む authData が含まれます: access_token、有効期限日付、ID。
デバッグ後、json 応答の URL を取得しました。名前と ID の 2 つの値しか含まれていませんが、次のようにリストを宣言しました。 List permissions = Arrays.asList("public_profile", "email");
したがって、今のところ、グラフは2つの値のみを返します。これは権限によるものだと思いましたが、宣言したと言いました。これが私のコードです:
public class LoginActivity extends Activity {
private Dialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FacebookSdk.sdkInitialize(getApplicationContext());
ParseFacebookUtils.initialize(this);
// Check if there is a currently logged in user
// and it's linked to a Facebook account.
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && ParseFacebookUtils.isLinked(currentUser)) {
// Go to the user info activity
showUserDetailsActivity();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}
public void onLoginClick(View v) {
progressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true);
List<String> permissions = Arrays.asList("public_profile", "email");
// NOTE: for extended permissions, like "user_about_me", your app must be reviewed by the Facebook team
// (https://developers.facebook.com/docs/facebook-login/permissions/)
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
progressDialog.dismiss();
if (user == null) {
Log.d("TAG", "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject fbUser,
GraphResponse response) {
/*
* If we were able to successfully retrieve the
* Facebook user's name, let's set it on the
* fullName field.
*/
Log.e("facebook User", fbUser.toString());
final ParseUser parseUser = ParseUser
.getCurrentUser();
if (fbUser != null
&& parseUser != null
&& fbUser.optString("name").length() > 0) {
parseUser.put("name",
fbUser.optString("name"));
parseUser.put("email",
fbUser.optString("email"));
Log.v("isnew, values: ","values : "+fbUser.optString("name")+","+fbUser.optString("email")+","+fbUser.optString("age_range")+","+fbUser.optString("locale"));
parseUser
.saveInBackground(new SaveCallback() {
@Override
public void done(
ParseException e) {
if (e != null) {
Log.v("", (getString(R.string.com_parse_ui_login_warning_facebook_login_user_update_failed)
+ e.toString()));
}
ParseInstallation installation = ParseInstallation
.getCurrentInstallation();
installation
.put(ParseConstants.KEY_USER_ID,
parseUser
.getUsername());
installation
.saveInBackground();
showUserDetailsActivity();
}
});
}
}
}).executeAsync();
} else {
Log.d("TAG", "User not new logged in through Facebook!");
showUserDetailsActivity();
}
}
});
}
private void showUserDetailsActivity() {
Intent intent = new Intent(this, UserDetailsActivity.class);
startActivity(intent);
}
}
public class UserDetailsActivity extends Activity {
private ProfilePictureView userProfilePictureView;
private TextView userNameView;
private TextView userGenderView;
private TextView userEmailView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userdetails);
userProfilePictureView = (ProfilePictureView) findViewById(R.id.userProfilePicture);
userNameView = (TextView) findViewById(R.id.userName);
userGenderView = (TextView) findViewById(R.id.userGender);
userEmailView = (TextView) findViewById(R.id.userEmail);
//Fetch Facebook user info if it is logged
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && currentUser.isAuthenticated()) {
makeMeRequest();
}
}
@Override
public void onResume() {
super.onResume();
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
// Check if the user is currently logged
// and show any cached content
updateViewsWithProfileInfo();
} else {
// If the user is not logged in, go to the
// activity showing the login view.
startLoginActivity();
}
}
private void makeMeRequest() {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
if (jsonObject != null) {
JSONObject userProfile = new JSONObject();
try {
userProfile.put("facebookId", jsonObject.getLong("id"));
userProfile.put("name", jsonObject.getString("name"));
if (jsonObject.getString("gender") != null)
userProfile.put("gender", jsonObject.getString("gender"));
if (jsonObject.getString("email") != null)
userProfile.put("email", jsonObject.getString("email"));
// Save the user profile info in a user property
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("profile", userProfile);
currentUser.saveInBackground();
// Show the user info
updateViewsWithProfileInfo();
} catch (JSONException e) {
Log.d("TAG",
"Error parsing returned user data. " + e);
}
} else if (graphResponse.getError() != null) {
switch (graphResponse.getError().getCategory()) {
case LOGIN_RECOVERABLE:
Log.d("TAG",
"Authentication error: " + graphResponse.getError());
break;
case TRANSIENT:
Log.d("TAG",
"Transient error. Try again. " + graphResponse.getError());
break;
case OTHER:
Log.d("TAG",
"Some other error: " + graphResponse.getError());
break;
}
}
}
});
request.executeAsync();
}
private void updateViewsWithProfileInfo() {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.has("profile")) {
JSONObject userProfile = currentUser.getJSONObject("profile");
try {
if (userProfile.has("facebookId")) {
userProfilePictureView.setProfileId(userProfile.getString("facebookId"));
} else {
// Show the default, blank user profile picture
userProfilePictureView.setProfileId(null);
}
if (userProfile.has("name")) {
userNameView.setText(userProfile.getString("name"));
} else {
userNameView.setText("");
}
if (userProfile.has("gender")) {
userGenderView.setText(userProfile.getString("gender"));
} else {
userGenderView.setText("");
}
if (userProfile.has("email")) {
userEmailView.setText(userProfile.getString("email"));
} else {
userEmailView.setText("");
}
} catch (JSONException e) {
Log.d("TAG", "Error parsing saved user data.");
}
}
}
public void onLogoutClick(View v) {
logout();
}
private void logout() {
// Log the user out
ParseUser.logOut();
// Go to the login view
startLoginActivity();
}
private void startLoginActivity() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
私は Parse 1.9.4 と ParseFacebookUtils 1.9.4 を使用しています。