Created
December 5, 2016 17:46
-
-
Save kranthilakum/d891b5e6914a2dc9e7454577153f9be6 to your computer and use it in GitHub Desktop.
Angular Service for Google API Authentication
This file contains hidden or 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
| // <script src="https://apis.google.com/js/client:platform.js" type="text/javascript"></script> | |
| var app = angular.module("MyApp", []); | |
| app.service('GoogleService', ['$q', '$http', '$log', '$window', function($q, $http, $log, $window){ | |
| var gapi = $window.gapi; | |
| var updateSigninStatus = function(isSignedIn) { | |
| if (isSignedIn) { | |
| makeApiCall(); | |
| } | |
| }; | |
| this.handleAuthClick = function(event) { | |
| gapi.auth2.getAuthInstance().signIn(); | |
| console.log("Auth ..."); | |
| }; | |
| this.handleSignoutClick = function(event) { | |
| gapi.auth2.getAuthInstance().signOut(); | |
| } | |
| var makeApiCall= function() { | |
| gapi.client.people.people.get({ | |
| resourceName: 'people/me' | |
| }).then(function(resp) { | |
| console.log('Hello, ' + resp.result.names[0].givenName); | |
| }, function(reason) { | |
| console.log('Error: ' + reason.result.error.message); | |
| }); | |
| }; | |
| gapi.client.init({ | |
| apiKey: 'AIzaSyBw6nm9KAMDZM3nLOuczcdAVHhBygQcDoI', | |
| discoveryDocs: ["https://people.googleapis.com/$discovery/rest?version=v1"], | |
| clientId: '854868583703-ecorfkdv6pbv877v5nlcmab198fe1f8r.apps.googleusercontent.com', | |
| scope: 'profile' | |
| }).then(function () { | |
| // Listen for sign-in state changes. | |
| gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus); | |
| // Handle the initial sign-in state. | |
| updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get()); | |
| }); | |
| }]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment