Get access token with react-native-google-signin

Viewed 1021

I need for my application to find user contacts from gmail. So I need to use this api from google contacts api

https://www.google.com/m8/feeds/contacts/{userEmail}/full

When I send a GET request to this url I get:

{
  "error": {
    "code": 403,
    "message": "The caller does not have permission",
    "status": "PERMISSION_DENIED"
  }
}

My question is how can I get the accessToken to request this api with react-native-google-signin? I've already tried

GoogleSignin.getCurrentUser().then(infos => console.log(infos));

I get with this function user informations as name, photo, email... and I get a field idToken but when I add this token to my header request I get always code 403, so I think that isn't the access token.

Can you please help me to find access token or is there any other solution to find contacts directly with react-native-google-signin

Edit: Also tried

GoogleSignin.getTokens().then((res) => { console.log(res); });

but nothing is logged I don't know why

2 Answers

you can try this function it work fine with me

_signIn = async () => {
    try {
        await GoogleSignin.hasPlayServices();
        const userInfo = await GoogleSignin.signIn();

                response: {
                    first_name: userInfo.user.givenName,  
                    social_id: userInfo.user.id, last_name: userInfo.user.familyName, email: userInfo.user.email
                }

            // console.log('_signIn userInfo', userInfo);


    } catch (error) {
        if (error.code === statusCodes.SIGN_IN_CANCELLED) {
            // console.log('_signIn error.code 1', error.code);
            // user cancelled the login flow
        } else if (error.code === statusCodes.IN_PROGRESS) {
            // console.log('_signIn error.code 2 ', error.code);
            // operation (f.e. sign in) is in progress already
        } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
            // console.log('_signIn error.code 3', error.code);
            // play services not available or outdated
        } else {
            // console.log('_signIn else', error);
            // some other error happened
        }
    }
};
Related