javascript - 使用gapi.drive.realtime时是否有使用gapi.auth2进行身份验证的解决方法?

标签 javascript google-authentication google-drive-realtime-api

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.realtimegapi.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/

相关文章:

google-sheets - 无需身份验证 token 即可访问 Google 电子表格 API

google-drive-api - Google Drive 的 FileID 格式是什么?我需要使用 API 查明新文件是否已上传到我的驱动器

google-apps-script - 如何将已上传的 Excel 文件转换为 Google 的电子表格格式?

javascript - 将 id 动态添加到每个表格单元格

javascript - 如何在 JavaScript 中更改音频的位置?

javascript - 如何在 MVC JsonResult 中返回换行符

Android Google 登录 : App Release Build Always Got "Error: 12500",,但调试版本未登录

javascript - jQuery Textarea 验证不起作用

swift - iOS firebase : FIRAuthUIDelegate. authUI 未被调用

javascript - Google 文档 - 实时访问文本更改