質問

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 Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?

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

役に立ちましたか?

解決

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.

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...

他のヒント

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 (
  "fmt"
  "golang.org/x/net/context"
  "golang.org/x/oauth2/google"
   analytics "google.golang.org/api/analytics/v3"
)

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("Unable to get default client: %v", err)
  }

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

  fmt.Println(service)
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top