Home > Article > Backend Development > How to Verify Google Sign-In ID Tokens in Go?
Verifying Google Sign-In ID Tokens in Go
In your quest to validate Google sign-in ID tokens using a Go backend server project, you've come across a challenge: locating an equivalent function to Java and Python's Google API Client Library examples. While the library has implementations for various languages, the Go documentation seems to omit a specific function for ID token verification.
A Go-to Solution
Fortunately, the solution in Go is straightforward and concise. Use the official Google API Client Library for Go:
go get google.golang.org/api/idtoken
Once the library is installed, the following code snippet provides a one-liner verification:
payload, err := idtoken.Validate(context.Background(), tokenString, audience) if err != nil { panic(err) } fmt.Print(payload.Claims)
Replace tokenString with the actual ID token you wish to validate, and audience with your application's client ID.
Sample Output
Upon successful validation, you'll receive a payload object containing the following claims:
By utilizing this method, you can efficiently verify Google sign-in ID tokens in your Go backend project, providing a secure authentication mechanism while minimizing latency and network issues associated with using the token info endpoint.
The above is the detailed content of How to Verify Google Sign-In ID Tokens in Go?. For more information, please follow other related articles on the PHP Chinese website!