I am implementing the Google OAuth 2.0 authorization code flow to fetch Gmail data. However, during the token exchange step on my backend, I consistently encounter the following error:
Error during Google OAuth callback: GaxiosError: invalid_grant
...
error_description: 'code_verifier or verifier is not needed.'**
What I am doing:
On the frontend (React Native), I use @react-native-google-signin/google-signin configured as follows:
GoogleSignin.configure({
scopes: ['https://www.googleapis.com/auth/gmail.readonly', 'email', 'profile'],
webClientId: '<MY_CLIENT_ID>', // Google Cloud Console web client ID
offlineAccess: true,
forceCodeForRefreshToken: true,
});
After user sign-in, I obtain the serverAuthCode and send it to my backend API to exchange for tokens.
On the backend (Node.js with the googleapis library), I call:
const { tokens } = await oauth2Client.getToken({
code: receivedCode,
});
I do not use or generate any code_verifier anywhere in the flow.
The problem:
The error indicates that "code_verifier": "undefined" is included in the token request.
The error message states: 'code_verifier or verifier is not needed.'
My backend does not use PKCE or send any code_verifier.
My redirect URI in Google Cloud matches the URI used in both the frontend and backend.
Despite these checks, I still receive the invalid_grant error.
What I suspect:
- The backend is inadvertently sending a
code_verifierparameter with a value ofundefined. - There may be a mismatch in the redirect URI or misuse of PKCE parameters.
What I tried:
- Verified that the redirect URI is identical in both the frontend and backend.
- Ensured no
code_verifierparameter is explicitly added. - Checked the OAuth credentials in Google Cloud Console.
Questions:
- Does Google expect a
code_verifieronly when using PKCE? - How can I prevent
code_verifier=undefinedfrom being sent? - How can I verify that the redirect URIs exactly match?
- What are the best practices for debugging
invalid_granterrors in the OAuth 2.0 code flow?