dsm13698679318 2016-06-19 19:25
浏览 35
已采纳

适用于OAuth2提供程序的正确AppEngine Golang程序包

What's the correct way to use OAuth with

If I use context.Context from golang.org/x/net/context, the error is:

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

But if I use appengine.Context from appengine (SDK), the error is:

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)

if I use oauth2.NoContext, the runtime error is

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

both tested using Go 1.4 and 1.7b3

using this piece of code:

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})
}

the provider was:

&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,
    }

What's the correct way to solve this?

  • 写回答

1条回答 默认 最新

  • duan02468 2016-06-19 19:36
    关注

    Solution:

    1. Change all "appengine imports to "google.golang.org/appengine
    2. Keep use context.Context (from golang.org/x/net/context) instead of appengine.Context
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析