通过多次搜索,我得到了有问题的脚本按预期工作。如果用户点击我网页上的按钮,则会在其Google联系人列表中创建联系人(如果已获得授权)。
我留下的问题是授权永不过期。在成功授权后,用户可以多次按下按钮,甚至可以在不同的会话中稍后返回页面,并且永远不会再请求授权。
也许这是GoogleAPI的预期行为,但出于我的预期目的,我不希望超过1次点击,我希望如果用户稍后回到我的页面,他们会被问到再次授权。
这可能吗?我尝试了以下......
gapi.auth.signOut();
gapi.auth2.signOut();
gapi.auth2.disconnect();
撤销令牌(见帖子末尾)
......没有成功。
代码可以在这里找到: https://jsfiddle.net/brian_hill/chvtmmjr/7/
function addContact(entry) {
var config = {
'client_id': '403037917634-qproaer1g5gcq83c941heo4q07olol23.apps.googleusercontent.com',
'scope': 'https://www.google.com/m8/feeds',
'cookie_policy': 'single_host_origin'
};
gapi.auth.authorize(config, function() {
insert(config, entry);
});
}
function insert(config, entry) {
gapi.client.request({
'method': 'POST',
'path': '/m8/feeds/contacts/default/full/',
'headers': {
'GData-Version': 3.0
},
'body': {
'entry': [entry]
},
'callback': function(data) {
if (data.hasOwnProperty('entry')) {
var msg = "Your Google Contacts have been updated to include ";
window.alert(msg.concat(data.entry.title.$t))
} else {
var msg = "Contact information could not be added for "
window.alert(msg.concat(entry.title.$t))
}
}
});
}
和HTML
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="https://apis.google.com/js/client.js"></script>
<body style="background-color:rgba(32, 177, 17, 0.3);">
<p>
The button below will add a contact to your GMail contacts</p>
<div style="width:150px">
<script>
function addJohn() {
var entry = {
"category": [{
"scheme": "http:\/\/schemas.google.com\/g\/2005#kind",
"term": "http:\/\/schemas.google.com\/contact\/2008#contact"
}],
"title": {
"type": "text",
"$t": "John Doe"
},
"content": [{
"type": "text",
"$t": "[Automatically Created]"
}],
"gd$email": [{
"rel": "http:\/\/schemas.google.com\/g\/2005#other",
"address": "john.doe@abcd.com",
"primary": "true"
}],
"gd$postalAddress": [{
"rel": "http:\/\/schemas.google.com\/g\/2005#home",
"$t": "123 Main Street\nOttawa, ON\nCanada"
}],
"gd$phoneNumber": [{
"rel": "http:\/\/schemas.google.com\/g\/2005#home",
"$t": "555.123.4567",
"primary": "true"
}]
};
addContact(entry);
}
function addJane() {
var entry = {
"category": [{
"scheme": "http:\/\/schemas.google.com\/g\/2005#kind",
"term": "http:\/\/schemas.google.com\/contact\/2008#contact"
}],
"title": {
"type": "text",
"$t": "Jane Doe"
},
"content": [{
"type": "text",
"$t": "[Automatically Created]"
}],
"gd$email": [{
"rel": "http:\/\/schemas.google.com\/g\/2005#other",
"address": "jane.doe@abcd.com",
"primary": "true"
}],
"gd$postalAddress": [{
"rel": "http:\/\/schemas.google.com\/g\/2005#home",
"$t": "321 Unknown Street\nOttawa, ON\nCanada"
}],
"gd$phoneNumber": [{
"rel": "http:\/\/schemas.google.com\/g\/2005#home",
"$t": "555.765.4321",
"primary": "true"
}]
};
addContact(entry);
}
</script>
<button onclick="addJohn();">Add Contact - John</button>
<button onclick="addJane();">Add Contact - Jane</button>
</div>
</body>
(注意:由于GoogleAPI授权流程的性质,它似乎无法在Chrome或Firefox上通过JS Fiddle工作 - 我确实让它在Microsoft Edge上运行。)
提前致谢, 布赖恩
PS。添加我尝试使用&#39;撤销&#39;选项。哪些仍然不起作用(我仍然没有得到重新提示授权),但有时它也有效(更新地址),有时它不会。
function addContact(entry) {
var config = {
'client_id': '403037917634-qproaer1g5gcq83c941heo4q07olol23.apps.googleusercontent.com',
'scope': 'https://www.google.com/m8/feeds',
'cookie_policy': 'single_host_origin'
};
gapi.auth.authorize(config, function() {
insert(config, entry);
}).then(signOut);
}
function signOut() {
$.ajax({
'type': 'GET',
'url': 'https://accounts.google.com/o/oauth2/revoke?token=' +
gapi.auth.getToken().access_token,
'async': false,
'contentType': "application/json",
'dataType': 'jsonp',
'success': function (nullResponse) {
window.alert('Disconnected');
},
'error': function (e) {
// Handle the error
console.log(e);
}
});
}
function insert(config, entry) {
gapi.client.request({
'method': 'POST',
'path': '/m8/feeds/contacts/default/full/',
'headers': {
'GData-Version': 3.0
},
'body': {
'entry': [entry]
},
'callback': function(data) {
if (data.hasOwnProperty('entry')) {
var msg = "Your Google Contacts have been updated to include ";
window.alert(msg.concat(data.entry.title.$t))
} else {
var msg = "Contact information could not be added for "
window.alert(msg.concat(entry.title.$t))
}
}
});
}
答案 0 :(得分:0)
尝试使用OAuth 2.0中的Revoke token指令:
在某些情况下,用户可能希望撤消对应用程序的访问权限。用户可以通过访问“帐户设置”来撤消访问权限。应用程序也可以以编程方式撤销对其的访问权限。在用户取消订阅或删除应用程序的情况下,程序化撤销很重要。换句话说,删除过程的一部分可以包括API请求,以确保删除授予应用程序的权限。\
要以编程方式撤消令牌,您的应用程序会向https://accounts.google.com/o/oauth2/revoke
发出请求,并将令牌作为参数包含在内:
curl -H "Content-type:application/x-www-form-urlencoded" \
https://accounts.google.com/o/oauth2/revoke?token={token}