英文:
Using AppEngine/Go Users API with OAuth: code sample, workflow, any help?
问题
虽然我对AppEngine/Python运行时非常有经验,但对Go运行时还是个新手。我的第一个应用程序即将发布,但我仍然需要提供用户登录的功能。我希望使用OpenID,因为我不想要求用户拥有Google Id。
然而,似乎没有或几乎没有可用的示例,而且AppEngine文档明确省略了我需要实现的函数的内容:
func init() {
http.HandleFunc("/_ah/login_required", openIdHandler)
}
func openIdHandler(w http.ResponseWriter, r *http.Request) {
// ...
}
openIdHandler
函数中应该放什么?
我了解到我需要提供一个页面,让用户选择其中一个OpenId提供商并输入他们在该系统中的Id。我只是不知道之后该怎么做。工作流程是什么?有人知道我可以查看的示例代码,以便我大致了解我必须做什么和处理什么数据吗?我所有的谷歌搜索都没有找到任何有用的信息。
明确一点,我不打算与这些OpenId提供商提供的任何服务进行交互;我不想创建推文或Buzz。我不需要访问联系人、文档、墙上的帖子或其他任何内容。我只想要一个经过身份验证的凭证,以便在我的应用程序中限制用户只能访问自己的数据。
英文:
While I am very experienced with the AppEngine/Python runtime, I am a newbie to the Go runtime. My first application is close to being ready to roll out, but I still need to provide a capability for the user to log in. I am hoping to use OpenID, as I would rather not require that the user have a Google Id.
However, it seems that there are no or almost no working examples out there, and the AppEngine documentation explicity omits the contents of the function that I need to implement:
func init() {
http.HandleFunc("/_ah/login_required", openIdHandler)
}
func openIdHandler(w http.ResponseWriter, r *http.Request) {
// ...
}
What goes inside the openIdHandler
func?
I understand that I need to provide a page that will allow the user to select one of the many OpenId providers and enter their Id for that system. I just don't know what to do after that. What is the workflow? Does anyone know of any sample code that I can look at to get a general idea of what I must do and what data I must handle? All of my well-honed google-fu has lead me nowhere.
To be clear, I am not looking to interact with any of the services provided by these OpenId providers; I do not wish to create Tweets or Buzz. I do not want access to contacts, docs, Wall postings or anything else. I just wanted an authenticated credenital that I can use inside my application to limit a users access to only his or her own data.
答案1
得分: 10
如果我理解正确的话,你需要 [tag:openid],而不是 [tag:oath]。
我为go-lang重写了python示例(Federated login and logout)。希望这对你有帮助。
package gae_go_openid_demo
import (
"fmt"
"os"
"http"
"appengine"
"appengine/user"
)
func init() {
http.HandleFunc("/", hello)
http.HandleFunc("/_ah/login_required", openIdHandler)
}
func hello(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u != nil {
url, err := user.LogoutURL(c, "/")
check(err);
fmt.Fprintf(w, "Hello, %s! (<a href='%s'>Sign out</a>)", u, url)
} else {
fmt.Fprintf(w, "Please, <a href='/_ah/login_required'>login</a>.")
}
}
func openIdHandler(w http.ResponseWriter, r *http.Request) {
providers := map[string]string {
"Google" : "www.google.com/accounts/o8/id", // shorter alternative: "Gmail.com"
"Yahoo" : "yahoo.com",
"MySpace" : "myspace.com",
"AOL" : "aol.com",
"MyOpenID" : "myopenid.com",
// add more here
}
c := appengine.NewContext(r)
fmt.Fprintf(w, "Sign in at: ")
for name, url := range providers {
login_url, err := user.LoginURLFederated(c, "/", url)
check(err);
fmt.Fprintf(w, "[<a href='%s'>%s</a>]", login_url, name)
}
}
// check aborts the current execution if err is non-nil.
func check(err os.Error) {
if err != nil {
panic(err)
}
}
英文:
If I well understood you — you need [tag:openid], not [tag:oath].
I rewrote python example (Federated login and logout) for go-lang. Hope this help.
package gae_go_openid_demo
import (
"fmt"
"os"
"http"
"appengine"
"appengine/user"
)
func init() {
http.HandleFunc("/", hello)
http.HandleFunc("/_ah/login_required", openIdHandler)
}
func hello(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u != nil {
url, err := user.LogoutURL(c, "/")
check(err);
fmt.Fprintf(w, "Hello, %s! (<a href='%s'>Sign out</a>)", u, url)
} else {
fmt.Fprintf(w, "Please, <a href='/_ah/login_required'>login</a>.")
}
}
func openIdHandler(w http.ResponseWriter, r *http.Request) {
providers := map[string]string {
"Google" : "www.google.com/accounts/o8/id", // shorter alternative: "Gmail.com"
"Yahoo" : "yahoo.com",
"MySpace" : "myspace.com",
"AOL" : "aol.com",
"MyOpenID" : "myopenid.com",
// add more here
}
c := appengine.NewContext(r)
fmt.Fprintf(w, "Sign in at: ")
for name, url := range providers {
login_url, err := user.LoginURLFederated(c, "/", url)
check(err);
fmt.Fprintf(w, "[<a href='%s'>%s</a>]", login_url, name)
}
}
// check aborts the current execution if err is non-nil.
func check(err os.Error) {
if err != nil {
panic(err)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论