1 回答

TA貢獻1757條經驗 獲得超8個贊
按原樣運行代碼,返回錯誤:
Invalid OAuth scope or ID token audience provided
我從Google 的 OAuth 范圍添加了 catch-all Cloud Platform 可寫范圍:
https://www.googleapis.com/auth/cloud-platform
這樣做,似乎有效。見下文:
package main
import (
"context"
"log"
"golang.org/x/oauth2"
auth "golang.org/x/oauth2/google"
)
func main() {
var token *oauth2.Token
ctx := context.Background()
scopes := []string{
"https://www.googleapis.com/auth/cloud-platform",
}
credentials, err := auth.FindDefaultCredentials(ctx, scopes...)
if err == nil {
log.Printf("found default credentials. %v", credentials)
token, err = credentials.TokenSource.Token()
log.Printf("token: %v, err: %v", token, err)
if err != nil {
log.Print(err)
}
}
}
我最近在使用這個庫時遇到了一些挑戰(zhàn)(訪問需要 JWT 受眾的 Cloud Run 服務)。在朋友的推薦下,我改用了google.golang.org/api/idtoken。API 非常相似。
- 1 回答
- 0 關注
- 216 瀏覽
添加回答
舉報