google-app-engine - 服务帐户、App 引擎、Go、Google API

标签 google-app-engine go google-api service-accounts

我尝试使用服务帐户连接到驱动器。

其实我有

        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:不是 App Engine 上下文”

我该怎么做?

谢谢。

最佳答案

Google App Engine 有两个 Go 包:appenginegoogle.golang.org/appengine

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

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

关于google-app-engine - 服务帐户、App 引擎、Go、Google API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31208905/

相关文章:

go - 如何在 Nitrous.io 上将 Go 版本升级到 1.2.1?

laravel - 在 Laravel 5 中使用 Guzzle 和 Socialite 向 Google API 发出请求

meteor - 如何使用服务配置 Meteor 包重新定义 OAuth 范围?

php - Google 本地搜索超过 7 个结果?

google-app-engine - gae样板文档

file - 如何将文件保存在内存中——在服务器之间传输文件而不将它们存储在本地

django - 无法使用 Django ModelForm 验证动态选择

xml - 你能在 GO 中动态生成 XML 吗?

java - 按日期(字符串)对 GAE 的查询结果进行排序

java - Google App Engine 上 Spring Boot 应用程序的 Maven 配置文件