标题:无法使用应用程序中嵌入的Javascript获取Google OATH access_token。
我几天来一直在努力获取JavaScript中的Google OATH access_token值。最终我想将其用于Google Analytics,但现在我希望只需登录我的Google帐户即可。我在下面的帖子代码中看到了非常接近我希望使用的内容: How to login with Google OAuth2 using Angularjs inside Phonegap using ClientID and ClientSecret
我对此示例的实现运行时没有错误,但无法获取访问令牌。任何人都可以显示获取access_token值需要哪些调整?这是我的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);
});
};
}]);
这是我的HTML。我意识到使用JQuery和AngularJS一起是膨胀的,现在这很好。我主要担心的是能够在JavaScript中获取access_token。
<!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>
感谢您的帮助和见解。