dongqiang5541 2018-07-14 01:39
浏览 73
已采纳

使用Go验证Remote Config Rest API的服务帐户

Over here the Firebase docs explain how you can retrieve a token required to make requests to the Remote Config Rest API.

It provides example code for Python, Java and Node.js. Because there is no code for Go, it sends me to the Google Client Library (for Go). You might be able to understand why I am getting lost there...

The examples use GoogleCredential in Java, ServiceAccountCredentials in Python and google.auth.JWT in Node.js. I was not able to find any of those here. I do not know why there are no clear naming conventions.

I have found

firebaseremoteconfig-gen.go: The code looks like it already implements what the Firebase documentation page tries to achieve "manually". Comparison: doc, package.

Help

Because the "Usage example" of the package ends strangely abrupt and is the opposite of extensive, I do not understand how to make use of it.

I would be helped if someone could tell me how I can use this:

firebaseremoteconfigService, err := firebaseremoteconfig.New(oauthHttpClient)

I could not figure out where I would get oauthHttpClient from. There is an oauth2 package in the repository, but there I face the same problem:

oauth2Service, err := oauth2.New(oauthHttpClient)

I need oauthHttpClient again, so this cannot be a solution.
http.Client could be anything, but I need to authenticate with a service-account.json file, like shown in the three example snippets here.

Tags explanation

I hope that someone has either had experience with integrating Firebase Remote Config with Go, someone knows how Google Client API authentication works or someone is good enough with Go to get how the usage works.

  • 写回答

2条回答 默认 最新

  • douhe2305 2018-07-17 07:38
    关注

    There are a couple of main ways of authenticating with the google APIs, they are documented here:

    Link to docs

    The ways documented are "3-legged OAuth", "Using API Keys" and finally "Service Accounts".

    From the links that you've included in the question; you are looking at the Python / Java / Node examples of "Service Accounts".

    Using Service Accounts in go

    The oauthHttpClient that you are referring to, is an http client that will attach the authentication information to the requests automatically.

    You can create one using this package:

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

    The examples linked in other languages use a "service account json key file".

    Using the method linked below, you can read that keyfile and create a jwt.Config struct that will give you access to the client that you need.

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

    The go equivalent of the other language examples linked is;

    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("...")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况