Oauth 2.0 使用oauth 2.0检索google联系人
我一直在这里努力获取谷歌联系人,我获取了用户的个人信息,但无法获取用户的联系人。 获取以下错误: 401(需要授权) https://www.google.com/m8/feeds/contacts/xyz@gmail.com/full?oauthtoken=[object%20Object]&callback=jquery162xxxxxxxxx\u 13xxxxxxxxx2&=13xxxxxx 在第一个代码块(带有URL)中,指定查询参数名称Oauth 2.0 使用oauth 2.0检索google联系人,oauth-2.0,google-contacts-api,google-api-client,Oauth 2.0,Google Contacts Api,Google Api Client,我一直在这里努力获取谷歌联系人,我获取了用户的个人信息,但无法获取用户的联系人。 获取以下错误: 401(需要授权) https://www.google.com/m8/feeds/contacts/xyz@gmail.com/full?oauthtoken=[object%20Object]&callback=jquery162xxxxxxxxx\u 13xxxxxxxxx2&=13xxxxxx 在第一个代码块(带有URL)中,指定查询参数名称oauthtoken。在第二个代码块中,指定参数值
oauthtoken
。在第二个代码块中,指定参数值accessToken
此参数值实际上应该是access\u token
。请看这里:
您可以尝试将您的令牌写入标头中,作为“Authorization:Bearer”+authResult,因此我认为您的代码应该如下所示:
$.ajax({
url: "https://www.google.com/m8/feeds/contacts/default/full",
type: "GET",
dataType: "jsonp",
headers: {
"Authorization": "Bearer " + authResult,
"GData-Version": "3.0"
},
success: function (data) {
console.log("json"+JSON.stringify(data));
}
});
JQuery文档对ajax headers参数做了如下说明:
要随请求一起发送的附加标头键/值对的映射[…]
这就是为什么我把你原来的字符串改成了地图。类型默认为GET,因此是可选的
希望这有帮助我回答了这个问题:
// OAuth configurations
var config = {
'client_id': 'xxxxxx.apps.googleusercontent.com',
'scope': 'https://www.google.com/m8/feeds/contacts/default/full'
};
gapi.auth.authorize(config, function(data) {
// login complete - now get token
var token = gapi.auth.getToken();
token.alt = 'json';
// retrieve contacts
jQuery.ajax({
url: 'https://www.google.com/m8/feeds/contacts/default/full/?max-results=999999',
dataType: 'jsonp',
data: token,
success: function(data) { successGmail(data); }
});
});
那里:
数据:{accessToken:authResult}
应该是数据:JSON.stringify({accessToken:authResult},null',)
这样您的auth令牌就可以正确编码,而不会得到[object%20Object]
。
// OAuth configurations
var config = {
'client_id': 'xxxxxx.apps.googleusercontent.com',
'scope': 'https://www.google.com/m8/feeds/contacts/default/full'
};
gapi.auth.authorize(config, function(data) {
// login complete - now get token
var token = gapi.auth.getToken();
token.alt = 'json';
// retrieve contacts
jQuery.ajax({
url: 'https://www.google.com/m8/feeds/contacts/default/full/?max-results=999999',
dataType: 'jsonp',
data: token,
success: function(data) { successGmail(data); }
});
});