繁体   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