【问题标题】:Service account, App engine, Go, Google APIs服务帐号、App 引擎、Go、Google API
【发布时间】:2015-07-03 14:14:24
【问题描述】:

我尝试使用服务帐户连接到云端硬盘。

其实我有

        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 google-api service-accounts


    【解决方案1】:

    Google App Engine 有两个 Go 包:appengine 和 google.golang.org/appengine。

    第一个使用与 oauth2 包使用的 context.Context 不兼容的 appengine.Context。您需要将第二个导入到google.golang.org/appengine。

    另外,将client := config.Client(oauth2.NoContext) 更改为client := config.Client(c)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-28
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多