doudu5029 2011-11-03 02:30
浏览 122
已采纳

将AppEngine / Go用户API与OAuth结合使用:代码示例,工作流程,任何帮助?

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条回答 默认 最新

  • dporu02280 2011-11-10 01:27
    关注

    If I well understood you — you need openid, not 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)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启