【问题标题】:Correct AppEngine Golang package for OAuth2 provider为 OAuth2 提供程序正确的 AppEngine Golang 包
【发布时间】:2016-06-20 03:36:52
【问题描述】:

使用 OAuth 的正确方法是什么

如果我从golang.org/x/net/context 使用context.Context,错误是:

"golang.org/x/net/context".Context does not implement "appengine".Context (missing Call method)

但如果我使用来自appengine(SDK)的appengine.Context,错误是:

cannot use oauth2.NewClient(c) (type *http.Client) as type "golang.org/x/net/context".Context in argument to provider.Client:
*http.Client does not implement "golang.org/x/net/context".Context (missing Deadline method)

如果我使用oauth2.NoContext,运行时错误是

Post https://accounts.google.com/o/oauth2/token: not an App Engine context

均使用 Go 1.4 和 1.7b3 进行测试

使用这段代码:

func Public_YoutubeOauth(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    provider, csrf := getOAuth(r)
    gets, err := url.ParseQuery(r.URL.RawQuery)
    if RenderHtmlIfError(w,err) {
        return
    }
    if csrf != gets.Get(`state`) {
        RenderHtmlError(w,`incorrect CSRF state`)
        return
    }
    code := gets.Get(`code`)
    token, err := provider.Exchange(c, code) // error here
    if RenderHtmlIfError(w,err) {
        return
    }
    RenderHtml(w,`page`,map[string]interface{`token`:token})
}

provider 是:

&oauth2.Config{
        ClientID:     `aaa`,
        ClientSecret: `bbb`,
        RedirectURL:  url + `/foo/youtube_oauth`,
        Scopes: []string{
            `openid`,
            `email`,
            `profile`,
            `https://www.googleapis.com/auth/youtube`,
        },
        Endpoint: google.Endpoint,
    }

解决这个问题的正确方法是什么?

【问题讨论】:

    标签: google-app-engine go


    【解决方案1】:

    解决方案:

    1. 将所有 "appengine 导入更改为 "google.golang.org/appengine
    2. 继续使用context.Context(来自golang.org/x/net/context)而不是appengine.Context

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 2012-07-27
      • 2021-12-15
      • 2016-06-17
      • 1970-01-01
      • 2014-01-08
      • 2011-06-21
      相关资源
      最近更新 更多