【问题标题】:Using AppEngine/Go Users API with OAuth: code sample, workflow, any help?将 AppEngine/Go 用户 API 与 OAuth 结合使用:代码示例、工作流,有什么帮助吗?
【发布时间】:2011-12-20 20:06:42
【问题描述】:

虽然我对 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。我只是不知道在那之后该怎么办。工作流程是什么?有谁知道我可以查看的任何示例代码,以大致了解我必须做什么以及我必须处理哪些数据?我所有的 google-fu 都让我无处可去。

明确地说,我不希望与这些 OpenId 提供商提供的任何服务进行交互;我不想创建推文或嗡嗡声。我不想访问联系人、文档、墙贴或其他任何内容。我只是想要一个经过身份验证的凭据,我可以在我的应用程序中使用它来限制用户只能访问他或她自己的数据。

【问题讨论】:

    标签: google-app-engine oauth go


    【解决方案1】:

    如果我很理解你——你需要,而不是。 我为 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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 2011-08-25
      • 1970-01-01
      • 2014-06-23
      • 2012-03-08
      相关资源
      最近更新 更多