服务器到服务器的OAuth2

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

Server to server OAuth2

问题

我正在尝试实现一个用于访问Google Analytics数据的Golang应用程序,但所有的示例都使用在一小时内失效的令牌。
在API访问中,我找到了一个"证书访问"的方式,它被设计用于从服务器访问,但我找不到在Golang中实现它的示例。是否有相关的阅读材料,或者你能为我指点迷津吗?

我正在使用这个库:
code.google.com/p/google-api-go-client/

在这里阅读了一些帖子,我找到了这个链接:https://stackoverflow.com/questions/9863509/service-applications-and-google-analytics-api-v3-server-to-server-oauth2-authen

但似乎它不能直接工作。难道没有其他方法可以在不进行额外处理的情况下实现吗?

英文:

I'm trying to implement a Golang application for accessing Google Analytics data. but all the examples uses tokens that dies in one hour.
In the api access i found a "Certificate access" that are designed to be used to access from servers, but i failed to find examples of its implementation in Golang. there is some reading or may you can enlighten my path for this?

I'm using this library.
code.google.com/p/google-api-go-client/

reading some post here I found this https://stackoverflow.com/questions/9863509/service-applications-and-google-analytics-api-v3-server-to-server-oauth2-authen

but it seems that it will not work directly. is really no way of doing this w/o hacking it around?

答案1

得分: 1

你是否已经查看了OAuth2包?我曾经使用它进行用户授权调用,并对其进行了一些修改,使其能够处理多个授权来源。我还没有测试过它在纯服务器之间的通信中的应用,但你应该能够修改传输代码以满足需求...

英文:

<p>have you checked out the OAuth2 package? I've used it for user-authorised calls, and hacked it around a bit to allow it to handle multiple authorisation sources.</p><p>I haven't tested it with pure server-to-server comms, but you should be able to hack the transport code to get it to do what it needs...</p>

答案2

得分: 0

这可能有点晚了,但我还没有找到一个好的例子来帮助人们入门。

在开始之前,请确保你:

安装了 Go 1.5

安装了 Google Cloud SDK(cloud.google.com/sdk - 这将允许本地开发)

在你的 Google App Engine / Cloud 控制台中创建了一个服务账号,并下载了 JSON 文件(API 和身份验证 > 凭据)

一旦上述设置完成:

设置一个路径,指向你之前下载的安全凭据

export GOOGLE_APPLICATION_CREDENTIALS=~/DIRECTORY/CREDENTIALS.json

现在你可以使用 Go 进行身份验证。

package main

import (
  "fmt"
  "golang.org/x/net/context"
  "golang.org/x/oauth2/google"
   analytics "google.golang.org/api/analytics/v3"
)

var (
  scope = analytics.AnalyticsReadonlyScope 
)

func main() {
  // 在本地运行时,身份验证由 gcloud 工具提供;在 Compute Engine 上运行时,由相关的服务账号提供。
  client, err := google.DefaultClient(context.Background(), scope)
  if err != nil {
        fmt.Printf("无法获取默认客户端:%v", err)
  }

  service, err := analytics.New(client)
  if err != nil {
        fmt.Printf("无法创建存储服务:%v", err)
  }

  fmt.Println(service)
}

希望对你有帮助!

英文:

this might be a little late but I havent found a good example to get people started.

Before you start make sure you

> install golang 1.5

> install google cloud SDK (cloud.google.com/sdk - this will allow for local development)

> Create a service account in your google appengine / cloud console and download the json (API's and auth > Credentials )

Once above is setup:

> set a path for your security credentials that you downloaded earlier

>export GOOGLE_APPLICATION_CREDENTIALS=~/DIRECTORY/CREDENTIALS.json

now you can authenticate with go.

package main

import (
  &quot;fmt&quot;
  &quot;golang.org/x/net/context&quot;
  &quot;golang.org/x/oauth2/google&quot;
   analytics &quot;google.golang.org/api/analytics/v3&quot;
)

var (
  scope = analytics.AnalyticsReadonlyScope 
)

func main() {
  // Authentication is provided by the gcloud tool when running locally, and
  // by the associated service account when running on Compute Engine.
  client, err := google.DefaultClient(context.Background(), scope)
  if err != nil {
        fmt.Printf(&quot;Unable to get default client: %v&quot;, err)
  }

  service, err := analytics.New(client)
  if err != nil {
        fmt.Printf(&quot;Unable to create storage service: %v&quot;, err)
  }

  fmt.Println(service)
}

huangapple
  • 本文由 发表于 2013年10月15日 05:04:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/19369280.html
匿名

发表评论

匿名网友

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

确定