2013-10-14 192 views
0

即時通訊嘗試實施Golang應用程序以訪問Google Analytics數據。但所有示例都使用在一小時內死亡的令牌。 在api訪問中,我發現了一個設計用於從服務器訪問的「證書訪問」,但我未能在Golang中找到它的實現示例。有一些閱讀,或者你可以啓發我的道路呢?服務器到服務器oauth2

使用此圖書館的資料。 code.google.com/p/google-api-go-client/

閱讀這裏的一些文章中,我發現這個Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?

,但它似乎不會直接工作。真的沒有辦法做到這一點不圍繞它?

回答

1

你檢查了OAuth2包嗎?我已經將它用於用戶授權的調用,並且繞過它一點,以允許它處理多個授權源。

我沒有用純服務器到服務器的通訊科測試,但你應該能夠破解運輸代碼來獲取它做它需要...

+0

裏面goauth2/oauth/jwt是代碼尋找。謝謝 – Freaktor

0

這可能是有點晚,但我沒有找到一個很好的例子來讓人們開始。

開始之前確保你

安裝golang 1.5

安裝谷歌雲SDK(cloud.google.com/sdk - 這將允許本地開發)

創建服務帳戶在您的谷歌appengine /雲端控制檯和下載JSON(API的和身份驗證>證書)

一旦以上是s etup:

設定的路徑爲之前下載的

出口GOOGLE_APPLICATION_CREDENTIALS =〜/目錄中的安全憑證/ CREDENTIALS.json

現在你可以去驗證。

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) 
}