【发布时间】:2015-06-18 21:59:48
【问题描述】:
过去几天我一直在尝试解决这个问题,并且没有高兴地遵循了许多示例和文档,我希望得到一些帮助。
以下给出了 Google 授权页面,但随后返回到 handleOAuth2Callback 页面失败,并出现以下错误:
错误:发布https://accounts.google.com/o/oauth2/token:不是应用程序 引擎上下文
我做错了什么?
import (
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/cloud"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
// "google.golang.org/api/drive/v2"
"html/template"
"net/http"
)
var cached_templates = template.Must(template.ParseGlob("templates/*.html"))
var conf = &oauth2.Config{
ClientID: "my client id",
ClientSecret: "my client secret",
RedirectURL: "http://localhost:10080/oauth2callback",
Scopes: []string{
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/userinfo.profile",
},
Endpoint: google.Endpoint,
}
func init() {
http.HandleFunc("/", handleRoot)
http.HandleFunc("/authorize", handleAuthorize)
http.HandleFunc("/oauth2callback", handleOAuth2Callback)
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
err := cached_templates.ExecuteTemplate(w, "notAuthenticated.html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
}
}
func handleAuthorize(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
url := conf.AuthCodeURL("")
http.Redirect(w, r, url, http.StatusFound)
}
func handleOAuth2Callback(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
hc := &http.Client{}
ctx := cloud.NewContext(appengine.AppID(c), hc)
log.Infof(c, "Ctx: %v", ctx)
code := r.FormValue("code")
log.Infof(c, "Code: %v", code)
// Exchange the received code for a token
tok, err := conf.Exchange(ctx, code)
// tok, err := conf.Exchange(oauth2.NoContext, code)
if err != nil {
log.Errorf(c, "%v", err)
}
log.Infof(c, "Token: %v", tok)
client := conf.Client(oauth2.NoContext, tok)
log.Infof(c, "Client: %v", client)
}
【问题讨论】:
-
我无法测试代码,但是,尝试将
hc := &http.Client{}更改为hc := urlfetch.Client(c)并包含import "appengine/urlfetch"。 -
@alvivi 害怕这会出现以下错误:“不能使用 c(类型 context.Context)作为类型“appengine”.Context 在“appengine/urlfetch”的参数中。客户端:context.Context 没有实现“appengine”.Context(缺少调用方法)“
-
您似乎将
google.golang.org/appengine/*包与AppEngine SDKappengine/*提供的包混合在一起。仅使用您的 AppEngine SDK 安装提供的包,例如import "appengine"而不是import "google.golang.org/appengine",等等。 -
@alvivi 这是不正确的。应该改用 google.golang.org/appengine 导入。
标签: google-app-engine go oauth-2.0 google-oauth