Last active
May 20, 2019 10:15
-
-
Save jakemmarsh/5809963 to your computer and use it in GitHub Desktop.
AngularJS Service with Controller for access to Google API with Javascript Client (and RequireJS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['angular', 'services'], function (angular) { | |
'use strict'; | |
return angular.module('myApp.controllers', ['myApp.services']) | |
.controller('IndexCtrl', ['$scope', 'googleService', function ($scope, googleService) { | |
$scope.login = function () { | |
googleService.login().then(function (data) { | |
// do something with returned data | |
console.log(data.email); | |
}, function (err) { | |
console.log('Failed: ' + err); | |
}); | |
}; | |
}]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['angular'], function (angular) { | |
'use strict'; | |
angular.module('myApp.services', []) | |
.service('googleService', ['$http', '$rootScope', '$q', function ($http, $rootScope, $q) { | |
var clientId = '{CLIENT_ID}', | |
apiKey = '{API_KEY}', | |
scopes = '{SCOPES}', | |
domain = '{OPTIONAL DOMAIN}', | |
deferred = $q.defer(); | |
this.login = function () { | |
gapi.auth.authorize({ | |
client_id: clientId, | |
scope: scopes, | |
immediate: false, | |
hd: domain | |
}, this.handleAuthResult); | |
return deferred.promise; | |
} | |
this.handleClientLoad = function () { | |
gapi.client.setApiKey(apiKey); | |
gapi.auth.init(function () { }); | |
window.setTimeout(checkAuth, 1); | |
}; | |
this.checkAuth = function() { | |
gapi.auth.authorize({ | |
client_id: clientId, | |
scope: scopes, | |
immediate: true, | |
hd: domain | |
}, this.handleAuthResult); | |
}; | |
this.handleAuthResult = function(authResult) { | |
if (authResult && !authResult.error) { | |
var data = {}; | |
gapi.client.load('oauth2', 'v2', function () { | |
var request = gapi.client.oauth2.userinfo.get(); | |
request.execute(function (resp) { | |
data.email = resp.email; | |
}); | |
}); | |
deferred.resolve(data); | |
} else { | |
deferred.reject('error'); | |
} | |
}; | |
this.handleAuthClick = function(event) { | |
gapi.auth.authorize({ | |
client_id: clientId, | |
scope: scopes, | |
immediate: false, | |
hd: domain | |
}, this.handleAuthResult); | |
return false; | |
}; | |
}]); | |
}); |
Include the email
scope. https://developers.google.com/+/web/api/rest/oauth#email-scopes
What about displaying multiple youtube channel, what need to be added?
how to display video in html page
^lol
the data object from the callback function of login is coming only in console and cant be accessed in script on line no:8 of controller.js
@glebec is right.
Sorry, new to Angular but any help would be appreciated... How do you call your service's
handleClientLoad
from the script load (which takes a global function name for callback)? Thanks-
Hey i was looking for the same . Did you got to know how is it handled ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need to get the data of the google account how can I do this because in data.email I am getting the empty array. if anyone know the answer please add it.
Thanks in advance