0

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.

Community
  • 1
  • 1
Pat M
  • 44
  • 5
  • Not sure if this will help or not https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/web-js – Linda Lawton - DaImTo Nov 11 '15 at 14:50
  • Also, this is an alternative which is also useful: https://developers.google.com/analytics/devguides/reporting/embed/v1/getting-started – Matt Nov 11 '15 at 23:53
  • Thank you for your suggestions. I really appreciate it. These are valid approaches for web applications but mine is not a web application. With oauth there are a series of requests and responses that must be handled. I still want to use JavaScript from a variety of clients and I believe this approach would work if I could obtain the access_token value. I am running the example on a local server but plan to embed this from a series of clients on different platforms which are not using web servers. – Pat M Nov 12 '15 at 02:33
  • PS. Dave thank you for the edit! – Pat M Nov 12 '15 at 02:34

0 Answers0