gpt4 book ai didi

google-app-engine - 转到 App 引擎 oauth2 请求

转载 作者:数据小太阳 更新时间:2023-10-29 03:28:58 26 4
gpt4 key购买 nike

我一直在尝试实现 OAuth2 for Go使用 App Engine 服务器到服务器请求。

这里是代码(下面的例子):

oauthConf, err := google.NewServiceAccountJSONConfig(
"./key.json",
"https://www.googleapis.com/auth/adsense.readonly",
)
if err != nil {
log.Fatal(err)
}

client := http.Client{Transport: oauthConf.NewTransport()}
resp, err := client.Get(urlStr)
...

然后我收到错误消息:

http.DefaultTransport and http.DefaultClient are not available in App Engine.

我很确定 json.key 文件对于其他内容是有效的

谷歌搜索让我明白最好使用 urlfetch,但我不知道如何使用 oauth2 配置进行设置。

最佳答案

NewServiceAccountJSONConfig 返回一个 oauth2.JWTConfig https://github.com/golang/oauth2/blob/master/google/google.go#L69-L87

它的 .NewTransport() 您正在使用:
https://github.com/golang/oauth2/blob/master/jwt.go#L86-L88

默认为 http.DefaultTransport,appengine 不支持:
https://github.com/golang/oauth2/blob/master/jwt.go#L163-L168

我会转而使用 AppEnigneConfig(如果可能)。参见 http://godoc.org/github.com/golang/oauth2/google

c := appengine.NewContext(nil)
config := google.NewAppEngineConfig(c, "https://www.googleapis.com/auth/bigquery")
// The following client will be authorized by the App Engine
// app's service account for the provided scopes.
client := http.Client{Transport: config.NewTransport()}
client.Get("...")

否则,如果您需要使用 ServiceAccountJSONConfig,您可能可以,但您需要使用 urlfetch ClientTransport 。查看 AppEngineConfig 是如何设置的:https://github.com/golang/oauth2/blob/master/google/appengine.go

关于google-app-engine - 转到 App 引擎 oauth2 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26662738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com