douzhiling3166 2016-04-19 10:47 采纳率: 100%
浏览 427
已采纳

在Go中验证Google登录ID令牌

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 by using a Google API Client Library in Go?

From this page on Using a Google API Client Library section

https://developers.google.com/identity/sign-in/android/backend-auth#using-a-google-api-client-library

There are Java and Python examples and there are links for verify ID tokens with the Google API Client Library for PHP, Node.js, and other languages. I checked for my target language; Go here

https://github.com/google/google-api-go-client/blob/master/GettingStarted.md

However, I found not equivalent function for validating token like in Java and Python example. Is there any function in Go for doing such thing?

I don't want to use token info endpoint

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123

since it introduces possible latency and network error. I wish to use Google API Client Library. Please guide me where should I look into.

  • 写回答

2条回答 默认 最新

  • dongzong5017 2016-04-19 11:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题