登录
首页 >  Golang >  Go问答

验证远程配置 REST API 服务帐户的 Go 实现

来源:stackoverflow

时间:2024-03-06 09:54:26 171浏览 收藏

本篇文章给大家分享《验证远程配置 REST API 服务帐户的 Go 实现》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

问题内容

这里的 firebase 文档解释了如何检索向 remote config rest api 发出请求所需的令牌

它提供了python、java 和 node.js 的示例代码。因为没有 go 代码,所以它将我发送到 google 客户端库(针对 go)。你也许能够理解为什么我在那里迷路了......

这些示例在 java 中使用 googlecredential,在 python 中使用 serviceaccountcredentials,在 node.js 中使用 google.auth.jwt。我在这里找不到任何一个。我不知道为什么没有明确的命名约定。

我发现了

firebaseremoteconfig-gen.go:代码看起来已经实现了 firebase 文档页面尝试“手动”实现的内容。比较:文档、包。

帮助

由于该包的“使用示例”结尾奇怪地突然,并且与广泛相反,我不明白如何使用它。

如果有人能告诉我如何使用它,我将会得到帮助:

firebaseremoteconfigservice, err := firebaseremoteconfig.new(oauthhttpclient)

我不知道从哪里可以获得 oauthhttpclient。存储库中有一个 oauth2 包,但我遇到了同样的问题:

oauth2Service, err := oauth2.New(oauthHttpClient)

我再次需要 oauthhttpclient,所以这不是一个解决方案。

http.client 可以是任何内容,但我需要使用 service-account.json 文件进行身份验证,如此处的三个示例片段所示。

标签说明

我希望有人有将 firebase remote configgo 集成的经验,有人知道 google 如何集成客户端 api 身份验证有效,或者某人足以使用 go 来了解使用方式。


解决方案


有几种使用 google api 进行身份验证的主要方法,它们记录在此处:

Link to docs

记录的方法是“三足 oauth”、“使用 api 密钥”,最后是“服务帐户”。

来自您在问题中包含的链接;您正在查看“服务帐户”的 python / java / node 示例。

在 go 中使用服务帐户

您所指的 oauthhttpclient 是一个 http 客户端,它将自动将身份验证信息附加到请求中。

您可以使用此包创建一个:

https://godoc.org/golang.org/x/oauth2/google

以其他语言链接的示例使用“服务帐户 json 密钥文件”。

使用下面链接的方法,您可以读取该密钥文件并创建一个 jwt.config 结构,该结构将使您能够访问所需的客户端。

https://godoc.org/golang.org/x/oauth2/google#JWTConfigFromJSON

链接的其他语言示例的 go 等效项是;

data, err := ioutil.readfile("/path/to/your-project-key.json")
if err != nil {
    log.fatal(err)
}
conf, err := google.jwtconfigfromjson(data, "https://www.googleapis.com/auth/firebase.remoteconfig")
if err != nil {
    log.fatal(err)
}
// initiate an http.client. the following get request will be
// authorized and authenticated on the behalf of
// your service account.
client := conf.client(oauth2.nocontext)
client.get("...")

我刚刚开始使用相同的库(来自 appengine 标准项目)。这就是我创建服务客户端的方式:

import (
    "context"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"

    "golang.org/x/oauth2/google"
    fb "google.golang.org/api/firebaseremoteconfig/v1"
    "google.golang.org/appengine"
    "google.golang.org/appengine/log"
)

const (
    // name of our service account file
    safilename = "my-firebase-sa.json"

    // oauth scopes used for remote config api
    scoperemoteconfig = "https://www.googleapis.com/auth/firebase.remoteconfig"
)

func createfirebaseservice(ctx context.context) (*fb.service, error) {
    data, err := ioutil.readfile(safilename)
    if err != nil {
        return nil, err
    }

    conf, err := google.jwtconfigfromjson(data, scoperemoteconfig)
    if err != nil {
        return nil, err
    }

    return fb.new(conf.client(ctx))
}

我这样称呼它:

func fetchConfig(ctx context.Context) (*fb.RemoteConfig, error) {
    s, err := createFirebaseService(ctx)
    if err != nil {
        log.Errorf(ctx, "Failed to create firebase service: %v", err)
        return nil, fmt.Errorf("Failed to initialize Firebase service")
    }

    projectID := "projects/" + appengine.AppID(ctx)

    cfg, err := s.Projects.GetRemoteConfig(projectID).Do()
    if err != nil {
        log.Errorf(ctx, "Failed to call Firebase remote config API: %v", err)
        return nil, err
    }

    return cfg, nil
}

代码使用项目 id 来形成其路径;阅读完 lib 代码后,我注意到该路径中缺少 /projects/ ;所以我只是将其添加到我的项目 id 中并且它可以工作;-) 至少在他们修复该问题并且我的代码停止工作之前..

希望这对某人有帮助。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>