Validating Google sign in ID token in Go

前端 未结 3 1826
半阙折子戏
半阙折子戏 2021-01-02 15:32

I am finding the way to validate ID token for Google sign-in for Android with a Go backend server project.

What is the equivalent function for validating ID tokens

3条回答
  •  执念已碎
    2021-01-02 16:17

    This is how I've done it using https://github.com/google/google-api-go-client library:

    import (
        "google.golang.org/api/oauth2/v2"
        "net/http"
    )
    
    var httpClient = &http.Client{}
    
    func verifyIdToken(idToken string) (*oauth2.Tokeninfo, error) {
        oauth2Service, err := oauth2.New(httpClient)
        tokenInfoCall := oauth2Service.Tokeninfo()
        tokenInfoCall.IdToken(idToken)
        tokenInfo, err := tokenInfoCall.Do()
        if err != nil {
            return nil, err
        }
        return tokenInfo, nil
    }
    

    oauth2.Tokeninfo object has info about the user. Note that this makes a call to https://www.googleapis.com/oauth2/v2/tokeninfo and I think that all Google API Client Libraries make this http call under the hood.

提交回复
热议问题