gpt4 book ai didi

javascript - 使用 angularjs 导入 Google 联系人

转载 作者:行者123 更新时间:2023-11-29 14:41:12 28 4
gpt4 key购买 nike

我正在尝试使用 Angular Js 导入用户的 gmail 联系人。该代码在普通 javascript 中运行良好,但在 angular js 中出错。

HTML 代码..

<a class="btn btn-primary btn-simple" ng-click="importgoogle()"><u>Import Gmail Friends</u></a>

Angular 代码..

   var clientId = 'Client ID';
var scopes = 'https://www.googleapis.com/auth/contacts.readonly';
$scope.importgoogle = function(){
window.setTimeout(authorize); //calls authorize()
}

var authorize = function(){
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization); //calls handleAuthorization()
}

var handleAuthorization = function(){
if (authorizationResult && !authorizationResult.error) {
$.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=500&v=3.0",
function(response){
console.log(response);
});
}
}

输入用户的 Id 和密码后,控制台中显示以下错误消息。

  Uncaught ReferenceError: authorizationResult is not defined

无法理解我哪里出错了,因为这段代码在 Javascript 中运行。请帮忙..

最佳答案

这是使用 Angular Js 的工作示例:

app.controller("importGCCtrl", function($scope, $http) {
$scope.config = {
'client_id': 'Client ID',
'scope': 'https://www.google.com/m8/feeds'
};

$scope.inviteContacts = function() {
gapi.auth.authorize($scope.config, function() {
$scope.fetch(gapi.auth.getToken());
});
}

$scope.fetch = function(token) {
$http.get("https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token + "&alt=json").then(function(response) {
console.log(response);
//console.log(response.data.feed.entry);
//$scope.contacts = response.data.feed.entry; // to assign data
});
}

});

*注意:请确保您已包含 API - <script src="https://apis.google.com/js/client.js"></script>在页面上

关于javascript - 使用 angularjs 导入 Google 联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38049744/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com