gapi.drive.realtime 中存在一个错误,导致它无法与 auth2 一起使用 ( See this Github issue 。) 以下是该错误的简要说明;当使用旧的gapi.auth.authorize
方法时,gapi.drive.realtime
提供的功能工作正常:
gapi.auth.authorize({
client_id: 'client-id.apps.googleusercontent.com',
scope: 'https://www.googleapis.com/auth/drive.file',
immediate: true
}, (response) => {
if(response.error) {
console.log("Error signing in!");
console.log(response);
} else {
gapi.drive.realtime.load("file-id", (r) => console.log(r));
}
});
创建实时 session ,并将生成的文档对象记录到控制台,没有错误。但是,如果将此代码更改为使用 gapi.auth2
中的等效函数:
gapi.auth2.authorize({
client_id: 'client-id.apps.googleusercontent.com',
scope: 'https://www.googleapis.com/auth/drive.file',
prompt: 'none'
}, (response) => {
if(response.error) {
console.log("Error signing in!");
console.log(response);
} else {
gapi.drive.realtime.load("file-id", (r) => console.log(r));
}
});
登录成功完成,但连接到文件时,记录以下错误:
GET https://drive.google.com/otservice/gs?rctype=js&rcver=0&id=file-id 401 ()
api:452 Drive Realtime API Error: token_refresh_required: The OAuth token must be refreshed.
查看网络调试器,第一次和第二次尝试之间的真正区别在于,在第二次尝试期间,gapi.drive.realtime
不会发送 access_token
header ,大概是因为它是在 gapi.auth
而不是 gapi.auth2
中查找它。这就是错误。使用 gapi.auth2.init
进行授权也会显示相同的行为。
我想知道是否有针对此错误的已知修复。是否有一些代码可以用来使 gapi.drive.realtime
与 gapi.auth2
中的函数一起使用,或者我只需要使用 gapi.auth
?
最佳答案
尝试使用此 github google sample 中的示例关于如何实现 token 刷新,因为这似乎是您遇到的问题。
dmp.auth.conditionalRefreshAuth = function() {
// We refresh the access token about 10 minutes before it expires.
if (new Date().getTime() + 600000 > dmp.auth.accessTokenExpieryTimestamp) {
dmp.auth.autoRefreshAuth();
}
};
关于javascript - 使用gapi.drive.realtime时是否有使用gapi.auth2进行身份验证的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44856506/