Google sign-in with gapi.auth2 fails to start in a React app
gapi.auth2.getAuthInstance().signIn()Short answer
gapi.auth2 belongs to the Google Sign-In JavaScript library, which Google deprecated on 31 March 2023. New OAuth client IDs cannot use it, so initialisation fails. Switch to Google Identity Services: load the gsi client script and render the Google button, which replaces the signIn() call.
Fix it fast
1Find out which cause you have
Find out which Google library the app loads and when the OAuth client ID was created in the Google Cloud console.
grep -rn "apis.google.com/js\|gapi.auth2\|accounts.google.com/gsi" src/ public/2Match what you see, then jump to its fix
What does the error look like?
gapi.auth2.getAuthInstance().signIn()New OAuth Client IDs cannot use the deprecated library.
Cause 1 of 2
The OAuth client ID is too new for the deprecated library
1Check
Google's notice says new OAuth client IDs cannot use the deprecated Google Sign-In JavaScript library. Client IDs created before 29 July 2022 can still use it.
grep -rn "gapi.auth2.init" src/2Fix
Stop using gapi.auth2 and use Google Identity Services. Load the client library and initialise it with your client ID and a callback.
<script src="https://accounts.google.com/gsi/client" async></script>
google.accounts.id.initialize({
client_id: "YOUR_GOOGLE_CLIENT_ID",
callback: handleCredentialResponse
});3Verify
The callback fires after a user signs in and receives a credential.
console.log(response.credential)Confirmed by developers.google.com
Cause 2 of 2
The code still calls signIn() on the old auth instance
1Check
Google Identity Services has no signIn() call. Sign-in starts when the Google button is rendered and the user clicks it.
grep -rn "signIn()\|signOut()" src/2Fix
Remove the signIn() and signOut() calls, render the button into a container, and manage the signed-in state in your own app.
<div id="buttonDiv"></div>
google.accounts.id.renderButton(
document.getElementById("buttonDiv"),
{ theme: "outline", size: "large" }
);3Verify
The Google button shows and clicking it starts the sign-in.
npm run devConfirmed by developers.google.com
How do I stop it coming back?
- Do not start new work on gapi.auth2: Google says to migrate as soon as possible.
- Treat the credential from sign-in as an ID token: Google's migration guide says it replaces OAuth 2.0 access tokens for sign-in.
- Use the separate authorization migration guide if the app needs Google API scopes.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Security
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fixSecurity
An [Authorize] endpoint rejects a request with a bearer token
Authorization Header sent: no Bearer token sent.
An [Authorize] endpoint without a policy name requires an authenticated user.
4 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fixSecurity
ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
401 Unauthorized
IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five.
3 causes, each with a fixSecurity
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 causes, each with a fix