Skip to content

Commit

Permalink
add traktv support
Browse files Browse the repository at this point in the history
  • Loading branch information
nraboy committed May 18, 2016
1 parent cf495b1 commit a8cc415
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,11 @@
0.2.8 -

* Add Trakt.tv support

0.2.7 - May 17, 2016

* Fix `Can't find variable: requestToken` error for various providers

0.2.6 - May 12, 2016

* Add Xing support
Expand Down
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -94,6 +94,7 @@ Each web service API acts independently in this library. However, when configur
$cordovaOauth.xing(string clientId, string clientSecret, object options);
$cordovaOauth.fiveHundredsPx(string sdkKey, object options);
$cordovaOauth.netatmo(object options);
$cordovaOauth.trakttv(string clientId, string clientSecret, string state);

Each API call returns a promise. The success callback will provide a response object and the error
callback will return a string.
Expand Down Expand Up @@ -174,13 +175,15 @@ $cordovaOauth.$inject = [
];
```

## Have a question or found a bug (compliments work too)?
## Support

This project is maintained by **Nic Raboy** and **Matheus Rocha**.
This project is maintained by **Nic Raboy** and **Matheus Rocha**. This plugin is and will always be open source. That said, support is not. If you'd like to **purchase** paid support for this library, please contact either of us on Twitter.

Tweet Nic Raboy on Twitter - [@nraboy](https://www.twitter.com/nraboy)
Nic Raboy on Twitter - [@nraboy](https://www.twitter.com/nraboy)

Tweet Matheus Rocha on Twitter - [@matheusrocha](https://www.twitter.com/matheusrocha)
Matheus Rocha on Twitter - [@matheusrocha](https://www.twitter.com/matheusrocha)

The issue tracker is to be used for bug reporting only. We will not help you build your application, diagnose your problems, or teach you how to use the various oauth providers through the issue tracker. Free support can be found in the forums or on Stack Overflow.


## Resources
Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "ng-cordova-oauth",
"main": "dist/ng-cordova-oauth.js",
"version": "0.2.7",
"version": "0.2.8",
"homepage": "https://github.com/nraboy/ng-cordova-oauth",
"authors": [
"Nic Raboy <http://www.nraboy.com>"
Expand Down
80 changes: 76 additions & 4 deletions dist/ng-cordova-oauth.js
Expand Up @@ -1069,7 +1069,8 @@
'oauth.pocket',
'oauth.mercadolibre',
'oauth.xing',
'oauth.netatmo'])
'oauth.netatmo',
'oauth.trakttv'])
.factory("$cordovaOauth", cordovaOauth);

function cordovaOauth(
Expand All @@ -1078,7 +1079,7 @@
$ngCordovaTwitter, $ngCordovaMeetup, $ngCordovaSalesforce, $ngCordovaStrava, $ngCordovaWithings, $ngCordovaFoursquare, $ngCordovaMagento,
$ngCordovaVkontakte, $ngCordovaOdnoklassniki, $ngCordovaImgur, $ngCordovaSpotify, $ngCordovaUber, $ngCordovaWindowslive, $ngCordovaYammer,
$ngCordovaVenmo, $ngCordovaStripe, $ngCordovaRally, $ngCordovaFamilySearch, $ngCordovaEnvato, $ngCordovaWeibo, $ngCordovaJawbone, $ngCordovaUntappd,
$ngCordovaDribble, $ngCordovaPocket, $ngCordovaMercadolibre, $ngCordovaXing, $ngCordovaNetatmo) {
$ngCordovaDribble, $ngCordovaPocket, $ngCordovaMercadolibre, $ngCordovaXing, $ngCordovaNetatmo, $ngCordovaTraktTv) {

return {
azureAD: $ngCordovaAzureAD.signin,
Expand Down Expand Up @@ -1119,7 +1120,8 @@
pocket: $ngCordovaPocket.signin,
mercadolibre: $ngCordovaMercadolibre.signin,
xing: $ngCordovaXing.signin,
netatmo: $ngCordovaNetatmo.signin
netatmo: $ngCordovaNetatmo.signin,
trakttv: $ngCordovaTraktTv.signin
};
}

Expand Down Expand Up @@ -1163,7 +1165,8 @@
'$ngCordovaPocket',
'$ngCordovaMercadolibre',
'$ngCordovaXing',
'$ngCordovaNetatmo'
'$ngCordovaNetatmo',
'$ngCordovaTraktTv'
];
})();

Expand Down Expand Up @@ -2192,6 +2195,74 @@
stripe.$inject = ['$q', '$http', '$cordovaOauthUtility'];
})();

(function() {
'use strict';

angular.module('oauth.trakttv', ['oauth.utils'])
.factory('$ngCordovaTraktTv', trakttv);

function trakttv($q, $http, $cordovaOauthUtility) {
return { signin: oauthTraktTv };

/*
* Sign into the Trakt.tv service
*
* @param string clientId
* @param string clientSecret
* @param string state
* @param object options
* @return promise
*/
function oauthTraktTv(clientId, clientSecret, appScope, state, options) {
var deferred = $q.defer();
if(window.cordova) {
if($cordovaOauthUtility.isInAppBrowserInstalled()) {
var redirect_uri = "http://localhost/callback";
if(options !== undefined) {
if(options.hasOwnProperty("redirect_uri")) {
redirect_uri = options.redirect_uri;
}
}
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');
browserRef.addEventListener('loadstart', function(event) {
if((event.url).indexOf(redirect_uri) === 0) {
try {
var requestToken = (event.url).split("code=")[1].split("&")[0];
$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 })
.success(function(data) {
deferred.resolve(data);
})
.error(function(data, status) {
deferred.reject("Problem authenticating");
})
.finally(function() {
setTimeout(function() {
browserRef.close();
}, 10);
});
}catch(e){
setTimeout(function() {
browserRef.close();
}, 10);
}
}
});
browserRef.addEventListener('exit', function(event) {
deferred.reject("The sign in flow was canceled");
});
} else {
deferred.reject("Could not find InAppBrowser plugin");
}
} else {
deferred.reject("Cannot authenticate via a web browser");
}
return deferred.promise;
}
}

trakttv.$inject = ['$q', '$http', '$cordovaOauthUtility'];
})();

(function() {
'use strict';

Expand Down Expand Up @@ -3073,6 +3144,7 @@
* Jawbone
* Untappd
* Xing
* Trakt.tv
*/

angular.module("ngCordovaOauth", [
Expand Down
6 changes: 3 additions & 3 deletions dist/ng-cordova-oauth.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "ng-cordova-oauth",
"version": "0.2.7",
"version": "0.2.8",
"description": "Connect to Oauth providers using Apache Cordova and the InAppBrowser plugin",
"author": {
"name": "Nic Raboy",
Expand Down
11 changes: 7 additions & 4 deletions src/oauth.js
Expand Up @@ -42,7 +42,8 @@
'oauth.pocket',
'oauth.mercadolibre',
'oauth.xing',
'oauth.netatmo'])
'oauth.netatmo',
'oauth.trakttv'])
.factory("$cordovaOauth", cordovaOauth);

function cordovaOauth(
Expand All @@ -51,7 +52,7 @@
$ngCordovaTwitter, $ngCordovaMeetup, $ngCordovaSalesforce, $ngCordovaStrava, $ngCordovaWithings, $ngCordovaFoursquare, $ngCordovaMagento,
$ngCordovaVkontakte, $ngCordovaOdnoklassniki, $ngCordovaImgur, $ngCordovaSpotify, $ngCordovaUber, $ngCordovaWindowslive, $ngCordovaYammer,
$ngCordovaVenmo, $ngCordovaStripe, $ngCordovaRally, $ngCordovaFamilySearch, $ngCordovaEnvato, $ngCordovaWeibo, $ngCordovaJawbone, $ngCordovaUntappd,
$ngCordovaDribble, $ngCordovaPocket, $ngCordovaMercadolibre, $ngCordovaXing, $ngCordovaNetatmo) {
$ngCordovaDribble, $ngCordovaPocket, $ngCordovaMercadolibre, $ngCordovaXing, $ngCordovaNetatmo, $ngCordovaTraktTv) {

return {
azureAD: $ngCordovaAzureAD.signin,
Expand Down Expand Up @@ -92,7 +93,8 @@
pocket: $ngCordovaPocket.signin,
mercadolibre: $ngCordovaMercadolibre.signin,
xing: $ngCordovaXing.signin,
netatmo: $ngCordovaNetatmo.signin
netatmo: $ngCordovaNetatmo.signin,
trakttv: $ngCordovaTraktTv.signin
};
}

Expand Down Expand Up @@ -136,6 +138,7 @@
'$ngCordovaPocket',
'$ngCordovaMercadolibre',
'$ngCordovaXing',
'$ngCordovaNetatmo'
'$ngCordovaNetatmo',
'$ngCordovaTraktTv'
];
})();
67 changes: 67 additions & 0 deletions src/oauth.trakttv.js
@@ -0,0 +1,67 @@
(function() {
'use strict';

angular.module('oauth.trakttv', ['oauth.utils'])
.factory('$ngCordovaTraktTv', trakttv);

function trakttv($q, $http, $cordovaOauthUtility) {
return { signin: oauthTraktTv };

/*
* Sign into the Trakt.tv service
*
* @param string clientId
* @param string clientSecret
* @param string state
* @param object options
* @return promise
*/
function oauthTraktTv(clientId, clientSecret, appScope, state, options) {
var deferred = $q.defer();
if(window.cordova) {
if($cordovaOauthUtility.isInAppBrowserInstalled()) {
var redirect_uri = "http://localhost/callback";
if(options !== undefined) {
if(options.hasOwnProperty("redirect_uri")) {
redirect_uri = options.redirect_uri;
}
}
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');
browserRef.addEventListener('loadstart', function(event) {
if((event.url).indexOf(redirect_uri) === 0) {
try {
var requestToken = (event.url).split("code=")[1].split("&")[0];
$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 })
.success(function(data) {
deferred.resolve(data);
})
.error(function(data, status) {
deferred.reject("Problem authenticating");
})
.finally(function() {
setTimeout(function() {
browserRef.close();
}, 10);
});
}catch(e){
setTimeout(function() {
browserRef.close();
}, 10);
}
}
});
browserRef.addEventListener('exit', function(event) {
deferred.reject("The sign in flow was canceled");
});
} else {
deferred.reject("Could not find InAppBrowser plugin");
}
} else {
deferred.reject("Cannot authenticate via a web browser");
}
return deferred.promise;
}
}

trakttv.$inject = ['$q', '$http', '$cordovaOauthUtility'];
})();
1 change: 1 addition & 0 deletions src/plugin.js
Expand Up @@ -54,6 +54,7 @@
* Jawbone
* Untappd
* Xing
* Trakt.tv
*/

angular.module("ngCordovaOauth", [
Expand Down

0 comments on commit a8cc415

Please sign in to comment.