簡體   English   中英

服務帳戶,App引擎,Go,Google API

[英]Service account, App engine, Go, Google APIs

我嘗試使用服務帳戶連接驅動器。

其實我有

        c := appengine.NewContext(r)
        key, err := ioutil.ReadFile("key/key.pem")
        if err != nil {
            w.WriteHeader(http.StatusInternalServerError)
            c.Errorf("Pem file not found")
            return
        }
        config := &jwt.Config{
            Email: "xxx@developer.gserviceaccount.com",
            PrivateKey: key,
            Scopes: []string{
                "https://www.googleapis.com/auth/drive",
            },
            TokenURL: google.JWTTokenURL,
        }

        client := config.Client(oauth2.NoContext)
        service, err := drive.New(client)
        if (err != nil) {
            w.WriteHeader(http.StatusInternalServerError)
            c.Errorf("Service connection not works")
            return
        }
        about, err := service.About.Get().Do()
        if (err != nil) {
            w.WriteHeader(http.StatusInternalServerError)
            c.Errorf(err.Error())
            return
        }
        c.Infof(about.Name)

我在這里找到的: https : //github.com/golang/oauth2/blob/master/google/example_test.go

當然,這是行不通的,我必須使用urlfetch,但是我不知道如何...我收到的錯誤是"ERROR: Get https://www.googleapis.com/drive/v2/about?alt=json: oauth2: cannot fetch token: Post https://accounts.google.com/o/oauth2/token: not an App Engine context"

我該怎么辦?

謝謝。

Google App Engine有兩個Go軟件包: appenginegoogle.golang.org/appengine

第一個使用與oauth2軟件包使用的context.Context不兼容的appengine.Context。 您需要將第二個導入google.golang.org/appengine

另外,將client := config.Client(oauth2.NoContext)更改為client := config.Client(c)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM