2017-08-10 2 views
0

Ich habe ein Problem mit der Anmeldung eines Benutzers in Parse-Server bei der Verwendung von Facebook.Parse-Server Facebook Login

Wenn der Benutzer klickt auf die Anmeldung mit Facebook-Symbol oben wird dieser Code ..

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); 
     } 
    } 
}); 

Mein getUserDetailsFromFacebook() -Methode sieht laufen wie folgt

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(); 

} 

meine saveNewFacebookUser() sieht wie folgt aus ...

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()); 
      } 
     } 
    }); 
} 

Der Fehler sagt mir, dass ich Sig verwenden muss nUpInBackground und nicht saveInBackground. Wenn ich das mache, bekomme ich einen weiteren Fehler, der besagt, dass ich ein Passwort für den Benutzer speichern muss -> was den gesamten Zweck des Facebook-Logins zunichte macht.

Jede Hilfe wäre sehr willkommen!

Antwort

0

Ich habe das Problem gefunden.

in der saveNewFacebookUser() -Methode, ich habe es als ein brandneuer Benutzer festgelegt.

ParseUser new = new ParseUser(); 

Dies sollte

ParseUser new = ParseUser.getCurrentUser(); 

gewesen habe ich dies für den Fall, bis jemand Fragen hat verlassen.