javascript - Gmail API:在没有用户交互的情况下应用身份验证令牌时出现问题
问题描述
操作系统:Windows 10,浏览器:Chrome v.81,服务器:PHP 5.6,JQuery:2.1.1 版
应用:浏览器快速入门
客户端希望这个浏览器应用程序自动更新用户的令牌,而不是每次都强制用户手动重新授权。
根据快速入门页面,“在初始用户授权后,调用使用立即:真模式的 gapi.auth.authorize 无需用户交互即可获得身份验证令牌。”
我快要让它发挥作用了。我可以授权并收集新的身份验证令牌,但我找不到正确的语法来将该令牌应用于更新的登录序列。这是我到目前为止所拥有的。
// Initializes the API client library and sets up sign-in state listeners.
function initClient() {
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
}, function(error) {
appendPre(JSON.stringify(error, null, 2));
});
}
//Checks user's login state
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
//This is the API interaction I want to run.
displayInbox();
} else {
//I think this is where the automatic token refresh would happen (when the user
//is no longer logged in), which is why I've added in the "autoRefresh" function.
autoRefresh();
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
}
}
//My Auto Refresh Function, to get the new token without client interaction
function autoRefresh() {
gapi.auth.authorize({
'client_id':CLIENT_ID,
'immediate':'true',
'scope':SCOPES
}).then(function(response) {
console.log(response);
var access_token = response.access_token;
console.log(access_token);
// This is where I'm stuck. I have the token, but idea how to feed it back to the
// API, to actually renew the client's authentication.
// The only server-side example is in Python, which I can't use (client's locked into PHP).
// Before you ask, the Client's PHP server is unable to run the GMAIL API from PHP.
// All my hopes rest on vanilla javascript... So,
// How do I authorize the client with the new access_token from here?
});
}
/**
* These are the button functions. I've added them in just to be thorough. I'm trying
* to replace the "handleAuthClick(event)" with my "autoRefresh()" function.
*/
function handleAuthClick(event) {
gapi.auth2.getAuthInstance().signIn();
}
function handleSignoutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}
如您所见,我已经接近重新授权客户端,只是不知道将令牌提交回API服务器进行重新授权的最后一步。
本教程的“身份验证”按钮的代码 (gapi.auth2.getAuthInstance().signIn();) 似乎需要用户手动登录,并且在我和我的一生中都找不到等效的代码来验证 auth2无需用户登录。
我希望这不会太长,我要提前感谢您的时间和帮助。
任何帮助、链接或建议将不胜感激。
祝你有美好的一天!
解决方案
推荐阅读
- python - python固定数字舍入
- javascript - 在react chartjs2中为折线图获取x轴标签数据onClick函数
- reactjs - 从 reactjs 中的特定频道获取所有 youtube 视频
- reactjs - Ionic React - 你必须使用 Ionic UI 组件和路由吗?
- python-3.x - 从 scipy 升级到 imageio
- javascript - 如何使用 Selenide 检查元素是否为 2 位?
- python - 如何确保 R 和 Python 脚本使用相同的排序规则?
- mysql - 从字符串中提取以关键字开头的单词(SQL 或任何其他方法?)
- performance - N1ql 计数不同的关联文档
- android - Android 波纹颜色应用于聚焦和悬停状态