CapacitorGoogleAuth icon indicating copy to clipboard operation
CapacitorGoogleAuth copied to clipboard

Missing accessToken on native device

Open seweryn-vibbe opened this issue 3 years ago • 7 comments

const user = await GoogleAuth.signIn();

Ionic 5 Capacitor 3

If I testing this on web all working fine, but if I am on native device (in my case android) user.authentication object has missing accessToken field. There is only idToken

seweryn-vibbe avatar Jun 28 '21 12:06 seweryn-vibbe

because in plugin java he only call tokenId

this is the code

GoogleSignInAccount account = completedTask.getResult(ApiException.class);

  JSObject authentication = new JSObject();
  authentication.put("idToken", account.getIdToken());

  JSObject user = new JSObject();
  user.put("serverAuthCode", account.getServerAuthCode());
  user.put("idToken", account.getIdToken());
  user.put("authentication", authentication);
  
  user.put("displayName", account.getDisplayName());
  user.put("email", account.getEmail());
  user.put("familyName", account.getFamilyName());
  user.put("givenName", account.getGivenName());
  user.put("id", account.getId());
  user.put("imageUrl", account.getPhotoUrl());

Mersal-Mohamed avatar Jul 08 '21 15:07 Mersal-Mohamed

i fixed this in my js code i will post the full solution tomorow Insha'Allah

Mersal-Mohamed avatar Jul 14 '21 00:07 Mersal-Mohamed

google has an end point which return access_token if you give it serverAuthCode

you can get server auth code through the ordinary request which you made by await GoogleAuth.signIn().

this is the end point link https://oauth2.googleapis.com/token

it's post request which should have this body const body = client_id=yourclientid&client_secret=yourclientsecret&code=serverAuthCode&grant_type=authorization_code&redirect_uri=${redirect_uri};

redirect uri you can get it through these steps : go to https://console.cloud.google.com/ open your project credentials page click on android which under OAuth 2.0 Client IDs download json from android client page you will find redirect uri inside it

response of this request contain ACCESS_TOKEN which you could use it in your sign in process

Mersal-Mohamed avatar Jul 14 '21 21:07 Mersal-Mohamed

this link will help you with this issue

https://www.daimto.com/how-to-get-a-google-access-token-with-curl/

Mersal-Mohamed avatar Jul 18 '21 12:07 Mersal-Mohamed

Thanks man

seweryn-vibbe avatar Jul 19 '21 07:07 seweryn-vibbe

Thanks to the steps from @Mersal-Mohamed, the following code solves the problem

const googleUser = await GoogleAuth.signIn();
let accessToken = googleUser.authentication.accessToken;
if (!accessToken) {
    const body = {
      client_id: YOUR_CLIENT_ID,
      client_secret: YOUR_CLIENT_SECRET,
      code: googleUser.serverAuthCode,
      grant_type: 'authorization_code',
      redirect_uri: 'urn:ietf:wg:oauth:2.0:oob'
    };
    const authResponse = await this.http.post('https://oauth2.googleapis.com/token', body).toPromise();
    accessToken = authResponse['access_token'];
}

From the guide, redirect_uri must be set like that for "installed apps". Use firstValueFrom if toPromise is deprecated.

jeqcho avatar Aug 30 '21 18:08 jeqcho

In my implementation, testing locally in Android emulator, I'm not receiving the googleUser.serverAuthCode. So this is not just missing acecssToken but also missing other items from the response.

The response body from this:

let googleUser = await GoogleAuth.signIn();
console.log("signIn:", JSON.stringify(googleUser,undefined,2));

is

{
      "idToken": "String",
      "authentication": {
        "idToken": "String"
      },
      "displayName": "String",
      "email": "String",
      "familyName": "String",
      "givenName": "String",
      "id": "String",
      "imageUrl": "String"
    }

I'm using: @codetrix-studio/capacitor-google-auth": "^3.0.2",

ASomerN avatar Sep 19 '21 18:09 ASomerN