如何在Go中使用App Engine上的客户端ID进行OAuth2?

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

How do I use a client ID for OAuth2 on App Engine in Go?

问题

我在AppEngine中运行了一些相当简单的Go代码,应该使用OAuth2从用户帐户中获取文件列表。它似乎成功初始化了服务,但当它尝试获取文件列表时,我收到了这个错误:
OAuthError: RoundTrip: no Token supplied

package foo

import (
    "appengine"
    "appengine/urlfetch"
    "code.google.com/p/goauth2/oauth"
    "code.google.com/p/google-api-go-client/drive/v2"
    "fmt"
    "net/http"
)

var config = &oauth.Config{
    ClientId:     "(已编辑).apps.googleusercontent.com",
    ClientSecret: "已编辑",
    Scope:        "https://www.googleapis.com/auth/drive",
    AuthURL:      "https://accounts.google.com/o/oauth2/auth",
    TokenURL:     "https://accounts.google.com/o/oauth2/token",
}

func init() {
    http.HandleFunc("/", home)
}

func home(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    transport := &oauth.Transport{
        Config:    config,
        Transport: &urlfetch.Transport{Context: c}}
    svc, err := drive.New(transport.Client())
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    q := svc.Files.List()
    _, err = q.Do()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    fmt.Fprintf(w, "Success!")
}

我无法弄清楚我在这里做错了什么。任何帮助将不胜感激。

英文:

I have some fairly simple Go code running in AppEngine that should be using OAuth2 to fetch the list of files from the user's account. It seems to initialize the service OK but when it tries to fetch the file list, I get this error:
OAuthError: RoundTrip: no Token supplied

package foo

import (
    "appengine"
    "appengine/urlfetch"
    "code.google.com/p/goauth2/oauth"
    "code.google.com/p/google-api-go-client/drive/v2"
    "fmt"
    "net/http"
)

var config = &oauth.Config{
    ClientId:     "(redacted).apps.googleusercontent.com",
    ClientSecret: "REDACTED",
    Scope:        "https://www.googleapis.com/auth/drive",
    AuthURL:      "https://accounts.google.com/o/oauth2/auth",
    TokenURL:     "https://accounts.google.com/o/oauth2/token",
}

func init() {
    http.HandleFunc("/", home)
}

func home(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    transport := &oauth.Transport{
        Config:    config,
        Transport: &urlfetch.Transport{Context: c}}
    svc, err := drive.New(transport.Client())
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    q := svc.Files.List()
    _, err = q.Do()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    fmt.Fprintf(w, "Success!")
}

I cannot figure out what I'm doing wrong here. Any help would be kindly appreciated.

答案1

得分: 3

这个页面有点旧,但是它用Go代码很好地概述了步骤。

英文:

This page is kinda old but it outlines the steps nicely with Go code.
http://golangtutorials.blogspot.com/2011/11/oauth2-3-legged-authorization-in-go-web.html

答案2

得分: 1

令牌配置不足,您首先需要按照以下步骤获取有效的访问令牌:

  1. 将用户重定向到由AuthCodeURL返回的页面。用户将看到您的应用程序名称和请求的权限。

  2. 如果用户授予了权限,他们将被重定向到您在配置中提供的RedirectURL。URL将包含一个名为code的查询参数。

  3. 检索code参数并将其传递给Exchange。如果一切顺利,现在应该可以正确进行身份验证的请求。

英文:

The token configuration is not enough; you first have to get a valid access token with the following steps:

  1. Redirect the user to the page returned by AuthCodeURL. The user will be shown the name of your application and the requested permissions.

  2. If the user grants the permissions, they will be redirected to the RedirectURL you gave in the configuration. The URL will contain a query parameter named code.

  3. Retrieve the code parameter and pass it to Exchange. If everything went well, the requests should now be authenticated properly.

huangapple
  • 本文由 发表于 2013年4月22日 19:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/16146117.html
匿名

发表评论

匿名网友

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

确定