Title: Unable to obtain Google OATH access_token using Javascript embedded in an application.
I have been struggling to obtain the Google OATH access_token value in JavaScript for several days now. Eventually I want to use it for Google Analytics but for now I hope to just login to my Google account. I saw in the following post code which is very close to what I hope to use: How to login with Google OAuth2 using Angularjs inside Phonegap using ClientID and ClientSecret
My implementation of this example runs without error but it fails to obtain the access token. Can anybody show which adjustment is needed to obtain the access_token value? This is my JavaScript:
// Module declaration.
var myApp = angular.module('myApp', []);
var googleapi = {
authorize: function(options) {
console.log(options); // **** THIS WORKS.
var deferred = $.Deferred();
deferred.reject({ error: 'Not Implemented' });
return deferred.promise();
}
};
// Controller.
myApp.controller('MainController', ['$scope', function ($scope) {
$scope.greeting = "Hello";
$scope.call_google = function(){
console.log("inside call_google"); // **** THIS WORKS.
googleapi.authorize({
client_id: '108adf6o7nn.apps.googleusercontent.com',
client_secret: 'mySecret',
redirect_uri: 'http://localhost',
scope: 'https://www.googleapis.com/auth/userinfo.email'
}).done(function(data) { // **** THIS BLOCK IS NEVER ENTERED EVEN WHEN client_id is empty.
accessToken=data.access_token;
console.log(data.access_token);
});
};
}]);
This is my HTML. I realize using JQuery and AngularJS together is bloat and this is fine for now. My main concern is being able to obtain the access_token in JavaScript.
<!doctype html>
<html>
<head>
<title>Starting Angular</title>
</head>
<body ng-app="myApp">
<!-- Identify the controller and model to be used. -->
<div ng-controller="MainController" ng-init="call_google()">
<h1 ng-bind="greeting"></h1>
<br />
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"> </script>
<script src="js/controllers.js"></script>
</body>
</html>
Thank you for your assistance and insight.