Sign Up With Google
10 min
sign in with google tutorial introduction sign in with google enables users to sign in to apps using their google accounts prerequisites to complete this tutorial, you will need an app created at back4app see the create new app tutorial https //www back4app com/docs/get started/new parse app to learn how to create an app at back4app set up a subdomain for your back4app app see activating your web hosting and live query https //www back4app com/docs/platform/activating web hosting to learn how to create an subdomain in back4app an google developer account https //developers google com/?hl=pt br 1 create a new back4app app first of all, it’s necessary to make sure that you have an existing app created at back4app however, if you are a new user, you can check this tutorial https //www back4app com/docs/get started/new parse app to learn how to create one 2 create a new client identifier log into your google developer account https //developers google com/ and go to \<font color="#2166ae">google api console\</font> click \<font color="#2166ae">credentials\</font> and choose \<font color="#2166ae">oauth 2 0 client ids\</font> if you do not have a consent screen, google will ask you to create one click on \<font color="#2166ae">configure consent screen\</font> , you will be redirected to the following page complete the screen consent configuration and hit \<font color="#2166ae">save\</font> pick the platform you will need for this example, i am using javascript (web application), but you should pick the one you will be using in \<font color="#2166ae">authorized javascript origins\</font> , replace the url with your subdomain in \<font color="#2166ae">authorized redirect uris\</font> , insert your subdomain followed by \<font color="#2166ae">/redirect\</font> as shown in the image below note if you do not have your subdomain enabled yet, please check the following guide to know how can you do this create your subdomain https //www back4app com/docs/platform/activating web hosting after that you should have your client id and secret 3 retrieve your code visit the following url, changing the values for \<font color="#2166ae">redirect uri\</font> and \<font color="#2166ae">client id\</font> for the ones you created https //accounts google com/o/oauth2/v2/auth?scope=https%3a//www googleapis com/auth/drive metadata readonly\&access type=offline\&include granted scopes=true\&response type=code\&state=state parameter passthrough value\&redirect uri=redirect url\&client id=client id the scopes necessary to retrieve the auth token and later on the user id are https //www googleapis com/auth/userinfo email https //www googleapis com/auth/plus me https //www googleapis com/auth/userinfo profile log in with your google account and the redirected website will have your code in the url copy the code part of the url only and run the following curl command replacing the values \<font color="#2166ae">your code\</font> , \<font color="#2166ae">client id\</font> , \<font color="#2166ae">client secret\</font> , and \<font color="#2166ae">redirect uri\</font> for the values of your application 1 curl x post \\ 2 https //oauth2 googleapis com/token \\ 3 f 'grant type=authorization code' \\ 4 f 'code=your code' \\ 5 f 'client id=client id' \\ 6 f 'client secret=client secret' \\ 7 f 'redirect uri=redirect uri' run it and you should retrieve your access token remember the code can be used only once if you get an error or don’t use your token, you must re generate your code to be able to run it again now it is time to retrieve your \<font color="#2166ae">google's user id\</font> it is a numeric string that you will pass along as the \<font color="#2166ae">id\</font> in step 4 to do so, run the following command replacing the \<font color="#2166ae">your token\</font> string for the token you received in the previous command 1 curl x get https //www googleapis com/userinfo/v2/me?access token=your token 4 start the development now that the sign in with google is configured, you can start the development process the format for authdata is 1 { 2 "google" { 3 "id" "user's google id (string)", 4 "id token" "an authorized google id token for the user (use when not using access token)", 5 "access token" "an authorized google access token for the user (use when not using id token)" 6 } 7 } here is the method for the ios sdk 1 pfuser loginwithauthtype(inbackground "google", authdata \["access token"\ tokenstring, "id" user]) continuewith { task > any? in 2 3 } and here for the android sdk 1 map\<string, string> authdata = new hashmap\<string, string>(); 2 authdata put("access token", tokenstring); 3 authdata put("id", user); 4 parseuser loginwithinbackground("google", authdata){ 5 6 } remember, this must be done at every login for every user