nraboy / ng-cordova-oauth

AngularJS oauth library for use with Apache Cordova projects
https://www.thepolyglotdeveloper.com
MIT License
456 stars 199 forks source link

Please add Trakt.tv support #30

Closed gzumaglini closed 8 years ago

gzumaglini commented 9 years ago

Please add support for the Trakt.tv API http://docs.trakt.apiary.io/

I already started this but ran into some problems when posting back the authorization code.

https://gist.github.com/gzumaglini/3cfee13744e8efae7a3c

nraboy commented 9 years ago

Thanks, I'll have a look as soon as time permits.

m-vdv commented 8 years ago

Hi @nraboy would love to see trakt.tv support as well! @gzumaglini Maybe you got it to work?

m-vdv commented 8 years ago

Trakt.tv API OAuth flow is described here: http://docs.trakt.apiary.io/#reference/authentication-oauth

nraboy commented 8 years ago

Please test it in the development or staging branches:

https://github.com/nraboy/ng-cordova-oauth/commit/a8cc4157b9c639a8e1bf032b965e3557e7ff2c3c

m-vdv commented 8 years ago

Hi @nraboy

Thanks, just tested and a little tweak is needed for the oauth to work:

var browserRef = window.cordova.InAppBrowser.open('https://api-v2launch.trakt.tv/oauth/authorize?client_id=' + clientId + '&redirect_uri=' + redirect_uri + '&response_type=code&state=' + state, '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');

Should be changed to:

var browserRef = window.cordova.InAppBrowser.open('https://trakt.tv/oauth/authorize?client_id=' + clientId + '&redirect_uri=' + redirect_uri + '&response_type=code&state=' + state, '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');

And also the oauth token call should be changed from:

$http({method: "post", headers: {'Content-Type': 'application/x-www-form-urlencoded'}, url: "https://api-v2launch.trakt.tv/oauth/token", data: "client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=" + redirect_uri + "&grant_type=authorization_code" + "&code=" + requestToken })

To this:

$http({method: "post", headers: {'Content-Type': 'application/x-www-form-urlencoded'}, url: "https://api-v2launch.trakt.tv/oauth/token?client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=" + redirect_uri + "&grant_type=authorization_code" + "&code=" + requestToken })

One things I noticed is the InAppBrowser still gives a flash of the http://localhost/callback page. Which shows a net::ERR_CONNECTION_REFUSED error page (since it obviously can't be loaded)

So even though we look for the http://localhost/callback page in the code it is still being loaded for a tiny second, until the oauth token url is called to successfully complete the oauth flow.

Is there anything we can do about this? It's not very user friendly to have the localhost error page show up for a second and then disappear.

(I've tried a simple window.stop() event but that didn't fix it.)

UPDATE: Just noticed this issue is already known, see: https://github.com/nraboy/ng-cordova-oauth/issues/193

nraboy commented 8 years ago

Hey @m-vdv,

I updated the URLs to use the latest Trakt.tv URL rather than a particular version. However your change in the POST request is not correct. You changed the parameters to be GET request parameters, but the documentation says otherwise.

I registered for a Trakt.tv account and tested and it seems to work fine.

As far as the ERR_CONNECTION_REFUSED issue, this is because explicit grants were not intended to be used in a client facing application. Due to popular demand, I designed a hacky approach to make it work. The only way to get rid of this is to host a callback page somewhere and override the redirect uri in the ng-cordova-oauth call.

Please give it a final test and make sure everything works so I can close the ticket. Again it is only in the staging and development branches.

Best,

m-vdv commented 8 years ago

Hi @nraboy

Good choice on using the latest trakt.tv url for both calls.

I did indeed change the token call from a POST to a GET which isn't ideal (but works). Your suggested POST token call is throwing a 400 error for me.

If I change the POST data into this it does work:

$http({method: "post", headers: {'Content-Type':'application/json'}, url: "https://trakt.tv/oauth/token", data: {'code':requestToken, 'client_id':clientId,'client_secret':clientSecret,'redirect_uri':redirect_uri,'grant_type':'authorization_code'} })

So with this final change, trakt.tv integration is complete :+1: Thank you once again!

nraboy commented 8 years ago

Give it another try. Still works for me, but I want to make sure you're good.

Best,

nraboy commented 8 years ago

Staging and development branch

m-vdv commented 8 years ago

@nraboy Works perfectly! Thank you