英文:
How to generate an access token from a service account created from firebase app using Go
问题
有一种方法可以使用从 Firebase 应用生成的服务帐号文件来生成访问令牌。来源:https://firebase.google.com/docs/database/rest/auth#python。
是否有一种方法可以使用 Go SDK 生成访问令牌?
[更新]
我已经研究了他们的文档,并达到了这个级别,但是在如何从那一点上获取访问令牌方面有些迷茫。
package main
import (
"context"
"fmt"
"math"
"regexp"
"sort"
"strconv"
"strings"
"google.golang.org/api/oauth2/v2"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
oauth2Service, err := oauth2.NewService(ctx, option.WithCredentialsFile("service-account.json"), option.WithScopes("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/firebase.database"))
if err != nil {
panic(err.Error())
}
tokenInfo, err := oauth2Service.Tokeninfo().Do()
if err != nil {
panic(err.Error())
}
fmt.Println(tokenInfo)
}
英文:
There's a way to generate an access token using a service account file generated from the firebase app. Source: https://firebase.google.com/docs/database/rest/auth#python.
Is there a way to generate an access token from a Go SDK?
[Update]
I've studied their doc and reached this level and am kinda lost on how to fetch/get the access_token from that point.
package main
import (
"context"
"fmt"
"math"
"regexp"
"sort"
"strconv"
"strings"
"google.golang.org/api/oauth2/v2"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
oauth2Service, err := oauth2.NewService(ctx, option.WithCredentialsFile("service-account.json"), option.WithScopes("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/firebase.database"))
if err != nil {
panic(err.Error())
}
tokenInfo, err := oauth2Service.Tokeninfo().Do()
if err != nil {
panic(err.Error())
}
fmt.Println(tokenInfo)
}
答案1
得分: 1
刚刚在深入研究他们的文档后找到了解决方案:
传输包的文档
ctx := context.Background()
creds, err := transport.Creds(ctx, option.WithCredentialsFile("service-account.json"), option.WithScopes("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/firebase.database"))
if err != nil {
panic(err.Error())
}
ts := creds.TokenSource
tok, err := ts.Token()
if err != nil {
panic(err.Error())
}
fmt.Printf("access_token %v", tok.AccessToken)
英文:
Just got the solution after digging more into their docs:
Doc to transport package
ctx := context.Background()
creds, err := transport.Creds(ctx, option.WithCredentialsFile("service-account.json"), option.WithScopes("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/firebase.database"))
if err != nil {
panic(err.Error())
}
ts := creds.TokenSource
tok, err := ts.Token()
if err != nil {
panic(err.Error())
}
fmt.Printf("access_token %v", tok.AccessToken)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论