为了账号安全,请及时绑定邮箱和手机立即绑定

为 OAuth2 提供程序正确的 AppEngine Golang 包

为 OAuth2 提供程序正确的 AppEngine Golang 包

Go
一只萌萌小番薯 2022-01-17 18:46:15
使用 OAuth 的正确方法是什么如果我使用context.Contextfrom golang.org/x/net/context,错误是:"golang.org/x/net/context".Context does not implement "appengine".Context (missing Call method)但如果我appengine.Context从appengine(SDK)使用,错误是: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,    }解决这个问题的正确方法是什么?
查看完整描述

1 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

解决方案:

  1. 将所有"appengine导入更改为"google.golang.org/appengine

  2. 继续使用context.Context(from golang.org/x/net/context) 而不是appengine.Context


查看完整回答
反对 回复 2022-01-17
  • 1 回答
  • 0 关注
  • 197 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号