如何使用Go从Firebase应用程序创建的服务帐号生成访问令牌?

huangapple go评论88阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2022年9月1日 01:18:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/73559617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定