Parse-Server Facebook登录

时间:2017-08-10 13:54:10

标签: facebook-login parse-server

我在使用Facebook时将用户注册到Parse-Server时遇到了问题。

当用户点击使用Facebook注册图标时,此代码将运行..

ParseFacebookUtils.logInWithReadPermissionsInBackground(LoginRegister.this, permissions, new LogInCallback() {
    @Override
    public void done(ParseUser user, ParseException err) {
        if (user == null) {
            MethodContants.showLog(TAG, "Uh oh. The user cancelled the Facebook login.", true);
        } else if (user.isNew()) {
            MethodContants.showLog(TAG, "User logged in through Facebook", false);
            getUserDetailsFromFacebook();
        } else {
            MethodContants.showLog(TAG, "User logged in through Facebook", false);
            Intent intent = new Intent(LoginRegister.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
        }
    }
});

我的getUserDetailsFromFacebook()方法看起来像这样

private void getUserDetailsFromFacebook() {
    GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject jsonObject, GraphResponse response) {

            try {
                facebookUser = jsonObject.getString("name");
                MethodContants.showLog(TAG, "json name object: " + jsonObject.getString("name"), false);
            } catch (JSONException e) {
                MethodContants.showLog(TAG, "Error when getting facebook name: " + e.getMessage(), true);
                showToast("Error saving Facebook user.");
            }
            try {
                facebookEmail = jsonObject.getString("email");
                MethodContants.showLog(TAG, "json email object: " + jsonObject.getString("email"), false);
            } catch (JSONException e) {
                MethodContants.showLog(TAG, "Error when getting facebook email: " + e.getMessage(), true);
                showToast("Error saving Facebook email.");
            }
            saveNewFacebookUser();
        }
    });

    Bundle parameters = new Bundle();
    parameters.putString("fields", "name,email");
    graphRequest.setParameters(parameters);
    graphRequest.executeAsync();

}

我的saveNewFacebookUser()看起来像这样......

private void saveNewFacebookUser() {
    final ParseUser newFacebookUser = new ParseUser();
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.profile_picture);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] image = stream.toByteArray();
    ParseFile file = new ParseFile(AppConstants.PARSEUSER_IMAGE_FILE_NAME, image);
    newFacebookUser.setUsername(facebookUser);
    newFacebookUser.setEmail(facebookEmail);
    newFacebookUser.put(AppConstants.PARSEUSER_FULLNAME, facebookUser);
    newFacebookUser.put(AppConstants.PARSEUSER_FIRST_TIME_LOGGED_IN, "true");
    newFacebookUser.put(AppConstants.PARSEUSER_PROFILE_IMAGE, file);

    file.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                newFacebookUser.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            // USER CREATED!
                            // TODO SEND AN EMAIL TO THE USER WITH USERNAME AND PASSWORD
                            Intent intent = new Intent(LoginRegister.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                            startActivity(intent);
                        } else {
                            MethodContants.showLog(TAG, "Facebook Error:" + e.getMessage(), true);
                            showToast("Facebook Error: " + e.getMessage());
                        }
                    }
                });
            } else {
                MethodContants.showLog(TAG, "Facebook Error:" + e.getMessage(), true);
                showToast("Facebook Error: " + e.getMessage());
            }
        }
    });
}

错误告诉我必须使用signUpInBackground而不是saveInBackground。但是,当我这样做时,我收到另一个错误,说我需要为用户保存密码 - >这打败了facebook登录的整个目的。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。

在saveNewFacebookUser()方法中,我将其设置为全新用户。

ParseUser new = new ParseUser();

这应该是

ParseUser new = ParseUser.getCurrentUser();

如果有人遇到问题,我会留下这个。