goth icon indicating copy to clipboard operation
goth copied to clipboard

GAE compatibility (urlfetch)

Open RubenN opened this issue 11 years ago • 8 comments

I'd like to use this on app engine. What would the best way be to get the proper (urlfetch) compatible Transport in? (FetchUser uses the default http.Get)

The result i got was: Post https://accounts.google.com/o/oauth2/token: http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://cloud.google.com/appengine/docs/go/urlfetch/

RubenN avatar Feb 16 '15 11:02 RubenN

The pattern for this kind of thing thus far has been callback functions that the user can set to provide customized functionality.

I would recommend having an GetClient() function that returns the http.Client to use for fetching the URL. By default, this should return a new client, but it could be overridden by calling code (see GetProviderName) to return the urlfetch.Client for use on App Engine.

This also has the added benefit of not strongly binding goth to any app engine code or logic.

@markbates thoughts?

tylerstillwater avatar Feb 16 '15 15:02 tylerstillwater

Sounds like a valid solution if it is in line with Mark's thoughts.

RubenN avatar Feb 18 '15 07:02 RubenN

I don't use GAE, so I'm not sure of the exact nature of the problem, so I'll leave this to Tyler's discretion, as he seems to have a handle on this.

markbates avatar Feb 18 '15 16:02 markbates

Sounds good. The approach I posted above should work just fine. @RubenN do you want to go ahead and implement this, then submit a PR?

tylerstillwater avatar Feb 18 '15 16:02 tylerstillwater

Ok

RubenN avatar Feb 19 '15 07:02 RubenN

It turned out to be even simpler and doesn't require code changes. The key is to change the default http client and transport like this:

http.DefaultClient = &http.Client{ Transport: &urlfetch.Transport{Context: c}, } http.DefaultTransport = &urlfetch.Transport{Context: c}

RubenN avatar Feb 19 '15 19:02 RubenN

For the record, this does not work anymore with newer versions of oauth2. It looks like it should be unnecessary now, but I can't get it to work at all with oauth2 released after this commit: https://github.com/golang/oauth2/commit/96e89befdc0f88d5868b297eb0e0727ce6978884

Works if you revert back to before that commit and use @RubenN 's sollution

oyvindsk avatar Oct 07 '15 23:10 oyvindsk

As before, I don't use GAE, so I can't produce. However, @oyvindsk if you can reproduce and know a fix please submit a PR so we can close this. Thanks.

markbates avatar Oct 09 '15 02:10 markbates