Kokizzu
Kokizzu

Reputation: 26908

Correct AppEngine Golang package for OAuth2 provider

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?

Upvotes: 1

Views: 191

Answers (1)

Kokizzu
Kokizzu

Reputation: 26908

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

Upvotes: 1

Related Questions