Search code examples
javascriptandroidreact-nativegoogle-signin

How to get accessToken of React Native GoogleSignIn?


I am not able to get accessToken, i need it in my backend api.

When i try this,

googleLogin = () => {

  GoogleSignin.signIn()
    .then((data) => {
      console.log("TEST "+JSON.stringify(data));
      var postData = {
        access_token: data.accessToken,
        code: data.idToken,
      };

      let axiosConfig = {
        headers: {
            'Content-Type': 'application/json',
            "Accept": "application/json",
        }
      };
     ....
      //Backend api axios call
     ....

    })
    .then((user) => {
      console.log("TEST G LOGIN 1 "+JSON.stringify(user))
    })
    .catch((error) => {
      console.log("....."+JSON.stringify(error))
    });
}

got this response, it doesn't inculde

accessToken

{
    "scopes": ["https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/drive.readonly"],
    "serverAuthCode": "4/jwE5LLLLLLa7-f33333jmMD2V978oyp44444eb9yGe_qhHnkXXXXXXXLFXKZlQdDzlH-iIJx_gzqlLLLLLL3Q0PP0",
    "idToken": "...v65a4MCC-ZUQmys_wf_DoCOBJEMuI........",
    "user": {
        "photo": "https://lh3.googleusercontent.com/-tLLLLLyeS0KE/AAAMMMMAAAI/AAAAAAAAAAA/ACHi3reMhihoeTe_6NjL666666EUVU82Q/s96-c/photo.jpg",
        "email": "[email protected]",
        "familyName": "tech",
        "givenName": "test",
        "name": "testtech",
        "id": "11688888817288868"
    }
}

as per documentation

getTokens() Resolves with an object containing { idToken: string, accessToken: string, } or rejects with an error. Note that using accessToken is discouraged.

So, i tried this in

GoogleSignin.Sign({
....
 var gettoken =  GoogleSignin.currentUserAsync(data.user).then((token) => {
      console.log('USER token', token);
    }).done();
...
 })

it got error and also tried const token = GoogSignIn.getTokens(), it return null.

package.json info

{
...
    "react": "16.8.3",
    "react-native": "0.59.9",
    "react-native-firebase": "5.3.1",
    "react-native-google-signin": "^2.0.0"
...
}

Please suggest what would be procedure to get accessToken.


Solution

  • Finally i get accessToken.

    Step 1:- I deleted all the generated clidenId in goolge developer console (keep only web application clientId as i used in my web project) and also deleted android app in firebase project.

    Step 2:- Created new android app in firebase and download google-services.json and paste it in android/app/google-services.json

    Step 3:- Copied the clidenId from this part of google-services.json

    ...
      "services": {
            "appinvite_service": {
              "other_platform_oauth_client": [
                {
                  "client_id": "xxxxxxx-xxxxx.apps.googleusercontent.com", //<--- copied this clientID
                  "client_type": 3
                },
                {
                  "client_id": "XXXXXXXXXX-fBBBBBBBBBBBBBBBBBBBBBBBBpugnhrade.apps.googleusercontent.com",
                  "client_type": 2,
                  "ios_info": {
                    "bundle_id": "com.projectios"
                  }
                }
              ]
            }
          }
    
    ...
    

    and paste in

    GoogleSignin.configure({
      webClientId: 'paste it here',
    });
    

    Step 4:- This is the code to get accessToken (But this code was not working in my previous google-services.json file)

    googleLogin = () => {
     
        GoogleSignin.signIn()
          .then((data) => {
    
            console.log("TEST " + JSON.stringify(data));
    
            const currentUser = GoogleSignin.getTokens().then((res)=>{
              console.log(res.accessToken ); //<-------Get accessToken
              var postData = {
                access_token: res.accessToken,
                code: data.idToken,
        
              };
        
              let axiosConfig = {
                headers: {
                  'Content-Type': 'application/json',
                  "Accept": "application/json",
                }
              };
             -----
              backend api call
             -----
    });
    
          })
          .then((user) => {
            console.log("TEST G LOGIN 1 " + JSON.stringify(user))
          })
          .catch((error) => {
            console.log("....." + JSON.stringify(error))
          });   
      }