服务账号,应用引擎,Go语言,Google API

huangapple go评论73阅读模式
英文:

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"

我该怎么办?

谢谢。

英文:

I try to connect to drive with a service account.

Actually I have

		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)

That I found here : https://github.com/golang/oauth2/blob/master/google/example_test.go

Of course it doesn't work, I have to use urlfetch, but I don't know how...
The error I get is "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"

How I can do?

Thank you.

答案1

得分: 1

有两个用于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)

英文:

There are two Go packages for Google App Engine: appengine and google.golang.org/appengine.

The first one uses appengine.Context that is not compatible with the context.Context used by the oauth2 packages. You need to import the second one to google.golang.org/appengine.

Also, change client := config.Client(oauth2.NoContext) to client := config.Client(c).

huangapple
  • 本文由 发表于 2015年7月3日 22:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/31208905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定