英文:
On App Engine using Go, correct method for using OAuth2
问题
我已经尝试了几天来解决这个问题,按照许多示例和文档的指导进行操作,但都没有成功。我希望能得到一些帮助。
以下代码在显示Google授权页面后,在返回handleOAuth2Callback页面时出现以下错误:
> 错误:Post https://accounts.google.com/o/oauth2/token: not an App
> Engine context
我做错了什么?
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: "我的客户端ID",
ClientSecret: "我的客户端密钥",
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)
}
英文:
I have been trying for the last few days to solve this, and having followed many of the examples and documentation out there without joy, I would like some assistance.
The following gives the Google authorization page, but then fails on returning to the handleOAuth2Callback page with the following error:
> ERROR: Post https://accounts.google.com/o/oauth2/token: not an App
> Engine context
What am I doing wrong?
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)
}
答案1
得分: 1
我的当前解决方案,包括记录已登录用户的显示名称和其Google Drive中的所有文件-如果您发现任何错误或改进,请评论:
import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v2"
"google.golang.org/api/plus/v1"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"html/template"
"net/http"
)
var cached_templates = template.Must(template.ParseGlob("templates/*.html"))
var conf = &oauth2.Config{
ClientID: "我的客户端ID", // 用正确的ClientID替换
ClientSecret: "我的客户端密钥", // 用正确的ClientSecret替换
RedirectURL: "http://localhost:10080/oauth2callback",
Scopes: []string{
"https://www.googleapis.com/auth/drive",
"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)
code := r.FormValue("code")
tok, err := conf.Exchange(c, code)
if err != nil {
log.Errorf(c, "%v", err)
}
client := conf.Client(c, tok)
// PLUS SERVICE CLIENT
pc, err := plus.New(client)
if err != nil {
log.Errorf(c, "创建Plus客户端时出错:%v", err)
}
person, err := pc.People.Get("me").Do()
if err != nil {
log.Errorf(c, "Person错误:%v", err)
}
log.Infof(c, "姓名:%v", person.DisplayName)
// DRIVE CLIENT
dc, err := drive.New(client)
if err != nil {
log.Errorf(c, "创建Drive客户端时出错:%v", err)
}
files, err := dc.Files.List().Do()
for _, value := range files.Items {
log.Infof(c, "文件:%v", value.Title)
}
}
以上是您提供的代码的翻译。
英文:
My current solution, including logging the logged in users display name and all the files in their Google Drive - please comment if you see any errors or improvements:
import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v2"
"google.golang.org/api/plus/v1"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"html/template"
"net/http"
)
var cached_templates = template.Must(template.ParseGlob("templates/*.html"))
var conf = &oauth2.Config{
ClientID: "my client id", // Replace with correct ClientID
ClientSecret: "my client secret", // Replace with correct ClientSecret
RedirectURL: "http://localhost:10080/oauth2callback",
Scopes: []string{
"https://www.googleapis.com/auth/drive",
"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)
code := r.FormValue("code")
tok, err := conf.Exchange(c, code)
if err != nil {
log.Errorf(c, "%v", err)
}
client := conf.Client(c, tok)
// PLUS SERVICE CLIENT
pc, err := plus.New(client)
if err != nil {
log.Errorf(c, "An error occurred creating Plus client: %v", err)
}
person, err := pc.People.Get("me").Do()
if err != nil {
log.Errorf(c, "Person Error: %v", err)
}
log.Infof(c, "Name: %v", person.DisplayName)
// DRIVE CLIENT
dc, err := drive.New(client)
if err != nil {
log.Errorf(c, "An error occurred creating Drive client: %v", err)
}
files, err := dc.Files.List().Do()
for _, value := range files.Items {
log.Infof(c, "Files: %v", value.Title)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论