Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google OAuth Configuration #1850

Closed
dcdspace opened this issue May 20, 2016 · 9 comments
Closed

Google OAuth Configuration #1850

dcdspace opened this issue May 20, 2016 · 9 comments

Comments

@dcdspace
Copy link

What would the configuration look like for using Google OAuth to sign in users? The documentation mentions this:

{ oauth: { twitter: { consumer_key: "", // REQUIRED consumer_secret: "" // REQUIRED }, facebook: { appIds: "FACEBOOK APP ID" } } }

but not what it should look like for Google. Also for the client iOS SDK, I know I would use the Google Sign In SDK, but how do I link that to Parse? Thanks.

@pewh
Copy link

pewh commented May 26, 2016

+1 looking for this

@flovilmart
Copy link
Contributor

You should not need anything on the server, just set the access_token property of the authData object when singingUp / linking from any client.

@dcdspace
Copy link
Author

dcdspace commented Jun 5, 2016

@flovilmart I have tried that and got the error described here: #1961

@flovilmart
Copy link
Contributor

Did you try with just putting {oauth: {google: true}}

@dcdspace
Copy link
Author

dcdspace commented Jun 5, 2016

In the server configuration? Like this? I am still getting the same issue with this.

oauth: {
    google: true
  }

@benitech
Copy link

@flovilmart any gist available for Android SDK oauth similar to your https://gist.github.com/flovilmart/68a6c538496953408bb1 for iOS?

@flovilmart
Copy link
Contributor

Nope sorry. That should be pretty similar though

[F v:lmart]

Le 13 juin 2016 à 23:45, benitech notifications@github.com a écrit :

@flovilmart any gist available for Android SDK oauth similar to your https://gist.github.com/flovilmart/68a6c538496953408bb1 for iOS?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@rsweny
Copy link

rsweny commented Dec 12, 2016

I found this hard to figure out too.. but in case it helps others oauth: { google: true } is not needed on the server. Android code looks like this and is called from Activity.onActivityResult:

	private void handleSignInResult(GoogleSignInResult result) {
		Log.w(TAG, "-------- handleSignInResult Google: " + result.isSuccess());
		ParseUser curUser = ParseUser.getCurrentUser();
		if (curUser != null)
		{
			Log.w(TAG, "Skip Google Auth - Found cur user: " + curUser.getObjectId());
			return;
		}

		if (result.isSuccess()) {
			GoogleSignInAccount acct = result.getSignInAccount();

			if (acct != null) {
				Log.w(TAG, "SUCCESS: " + acct.getDisplayName());
				Log.w(TAG, acct.getIdToken() + " - " + acct.getId() + " - " + acct.getEmail());

				final String[] names = acct.getDisplayName().split(" ");
				final String personPhotoUrl = acct.getPhotoUrl().toString();
				final String googleEmail = acct.getEmail();
				//Log.w(TAG, personPhotoUrl);

				Map<String,String> authData = new HashMap<String,String>();
				authData.put("id_token", acct.getIdToken());
				authData.put("id", acct.getId());
				Task<ParseUser> t = ParseUser.logInWithInBackground("google", authData);
				t.continueWith(new Continuation<ParseUser, Void>() {
					public Void then(Task task) throws Exception {
						if (task.isCancelled()) {
							Log.w(TAG, "Task cancelled");
						} else if (task.isFaulted()) {
							Log.w(TAG, "Save FAIL" + task.getError());
							Utilities.showToast(getResources().getString(R.string.errorLogin) + task.getError(), MainActivity.this);
						} else {
							// the object was saved successfully.
							ParseUser user = (ParseUser)task.getResult();
							Log.w(TAG, "Success " + user.getObjectId() + " " + user.getUsername() + " " + user.getEmail() + " " + user.getSessionToken());

It will create a new account even if a Facebook user exists with that email already so best not to fill in the User.email field or you will have problems because duplicates aren't allowed. Setting a new field like "User.appEmail" might work better.

@satyajeetjadhav
Copy link

@rsweny Thanks for this blob of code. It helped me.
In case, anyone wants to avoid duplicate email ids, this is how I am doing it.

                Task<ParseUser> t = ParseUser.logInWithInBackground("google", authData);
                t.continueWith(new Continuation<ParseUser, Void>() {
                    @Override
                    public Void then(Task<ParseUser> task) throws Exception {
                        if (task.isCancelled()) {
                            Log.w("Login", "Task cancelled");
                        } else if (task.isFaulted()) {
                            Log.w("Login", "Save FAIL" + task.getError());
                        } else {
                            // the object was saved successfully.
                            /**
                             * current user is not null at this point
                             * if a user exists with the authData already, then that user was logged in.
                             * else, a new user was created.
                             * Now set the email id for this user and try to save the user. If a user already exists with this email id
                             * an error will be thrown stating an account already exists for this email id.
                             * If you get this error delete the new empty user that was created and also log them out.                             *
                             */
                            final ParseUser pUser = (ParseUser) task.getResult();
                            pUser.setEmail(googleSignInAccount.getEmail());
                            pUser.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(ParseException e) {
                                    if (e == null) {
                                        Log.w("Login", "Success " + pUser.getObjectId() + " " + pUser.getUsername() + " " + pUser.getEmail() + " " + pUser.getSessionToken());
                                        /* Handle successful login */
                                    } else {
                                        Log.e("Login", e.getMessage());
                                        pUser.deleteInBackground();
                                        ParseUser.logOutInBackground();
                                    }
                                }
                            });

                        }
                        return null;
                    }
                });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants