1

我正在尝试将 facebook 登录集成到我的 android 应用程序中。现在我在 Parse Backend 中得到一个关于已登录用户的新行,其中包含他的真实姓名、他的用户名中的一些奇怪的值(例如:SnvvKEsIv6tX7 ...),以及一个 authData,它是 Json 并包含以下值:access_token、到期日期,身份证。

调试后我得到了 json 响应的 url,它只包含两个值,名称和 id,但是我这样声明列表: List permissions = Arrays.asList("public_profile", "email");

所以现在图表只返回两个值,我认为这是由于权限,但正如我所说我确实声明了。这是我的代码:

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。

4

0 回答 0