【问题标题】:Is there a workaround to use gapi.auth2 for authentication when using gapi.drive.realtime?使用 gapi.drive.realtime 时是否有使用 gapi.auth2 进行身份验证的解决方法?
【发布时间】:2017-07-01 01:06:29
【问题描述】:

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));
    }
});

实时会话被创建并且生成的文档对象被记录到控制台并且没有错误。但是,如果将此代码更改为使用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 并没有在 header 中发送access_token,大概是因为它正在gapi.auth 中寻找它而不是gapi.auth2。这是错误。使用gapi.auth2.init 进行授权也会显示相同的行为。

我想知道是否有针对此错误的已知修复。是否有一些代码可以让gapi.drive.realtimegapi.auth2 中的函数一起使用,还是我只需要使用gapi.auth

【问题讨论】:

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


    【解决方案1】:

    尝试使用github google sample 中的示例,了解如何实现令牌刷新,因为这似乎是您遇到的问题。

    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();
      }
    };
    

    【讨论】:

    • 亲爱的@noogui 您能否将该 URL 更新为文档?因为它坏了,我也有同样的问题,所以很想看看!!
    猜你喜欢
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2017-02-20
    • 2018-03-16
    • 2016-09-26
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多