我正在使用Google Contacts API v3来提取谷歌联系人。
我开始使用适用于JavaScript的Google API客户端库进行身份验证和授权。我对API访问的这一部分没有任何问题。
但在执行身份验证部分后,我必须获取谷歌联系人(只读访问对我来说没问题)。我正在为Google Contacts API v3使用gdata-javascript-client。 我也指谷歌官方文档并复制了代码并进行了必要的修改以便为我工作。
我的问题是,
我正在关注两段代码,一条用于可编辑模式,另一条用于只读模式。
可修改模式访问:
function handleAuthResult(authResult){
if (authResult && !authResult.error) {
fetch_contacts_data(authResult);
};
}
function auth() {
var config = {
'client_id': 'CLIENT_ID',
'scope': 'https://www.google.com/m8/feeds'
};
gapi.auth.authorize(config, handleAuthResult);
}
function fetch_contacts_data(token) {
$.ajax({
url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token + "&max-results=70000&alt=json" + "&callback=?",
dataType: "json",
success:function(data) {
contacts = [];
for (var i = 0, entry; entry = data.feed.entry[i]; i++) {
var contact = {
'name' : entry['title']['$t'],
'id' : entry['id']['$t'],
'emails' : [],
'phoneNumber' : []
};
if (entry['gd$email']) {
var emails = entry['gd$email'];
for (var j = 0, email; email = emails[j]; j++) {
contact['emails'].push(email['address']);
}
}
if (entry['gd$phoneNumber']) {
var phoneNumber = entry['gd$phoneNumber'];
for (var j = 0, phone; phone = phoneNumber[j]; j++) {
contact['phoneNumber'].push(phone['$t']);
}
}
if (!contact['name']) {
contact['name'] = contact['emails'][0] || "<Unknown>";
}
contacts.push(contact);
}
numContacts = contacts.length;
friend_list_json_str = '';
for(var j=0;j<numContacts;j++) {
name = (contacts[j])['name'];
emails = (contacts[j])['emails'];
phone = (contacts[j])['phoneNumber'];
email_list= '';
phone_list= '';
for(var k=0;k<emails.length;k++) {
email_list += '"'+emails[k] + '",' ;
}
email_list = email_list.substring(0, email_list.length -1)
for(var k=0;k<phone.length;k++) {
phone_list = '"'+phone[k] + '",';
}
phone_list += phone_list.substring(0, phone_list.length -1)
friend_json_str = '';
friend_json_str += '{"name":"'+name + '",';
friend_json_str += '"emails":['+email_list+'],';
friend_json_str += '"phoneNumber":['+phone_list+']' ;
friend_json_str += '},';
friend_list_json_str += friend_json_str;
}
friend_list_json_str = friend_list_json_str.substring(0, friend_list_json_str.length - 1);
var user_data = get_user_data();
var len = user_data.length;
user_data = user_data.substring(0, len - 2);
user_data += friend_list_json_str + ']}';
data = "invite_data="+ user_data;
url = '/invite';
var posting = $.post( url, data );
posting.done(function( response_data ) {
});
}
});
}
只读访问权限:
function auth() {
var config = {
'client_id': 'CLIENT_ID',
'scope': 'https://www.googleapis.com/auth/contacts.readonly'
};
gapi.auth.authorize(config, handleAuthResult);
}
注意:其余代码与上面相同
在两种情况下,ajax调用失败,
$.ajax({
url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token + "&max-results=70000&alt=json" + "&callback=?",
dataType: "json",
success:function(data)
有人可以告诉我为什么这段代码无效吗?
答案 0 :(得分:0)
您已被浏览器的弹出窗口拦截器阻止。
点击按钮后尝试调用import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
def annotate_points(ax, A, B):
for xy in zip(A, B):
ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='offset points')
points = [(3.28,0.00),(4.00,0.50),(4.40,1.0),(4.60,1.52),(5.00,2.5),(5.00,3.34),(4.70,3.8)]
points = points + [(4.50,3.96),(4.20,4.0),(3.70,3.90),(3.00,3.5),(2.00,2.9)]
x, y = zip(*points)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.scatter(x, y, color='black')
annotate_points(ax, x, y)
tck,u = interpolate.splprep([x, y], s=0)
unew = np.arange(0, 1.01, 0.01)
out = interpolate.splev(unew, tck)
plt.plot(x, y, 'orange', out[0], out[1])
plt.legend(['connect the dots', 'cubic spline'])
plt.show()
方法,它应该可以正常工作。
要解决此问题,您需要:
auth()
调用gapi.auth.authorize
。如果用户已经允许,它将尝试在后台获取令牌。{immediate:true}
。gapi.auth.authorize
醇>