Javascript 如何使用Google登录按钮设置授权范围

Javascript 如何使用Google登录按钮设置授权范围,javascript,google-drive-api,Javascript,Google Drive Api,我正在与Google Drive集成。我遵循谷歌推荐的路径,将登录按钮如下: <div class="g-signin2" data-onsuccess="onSuccessFunc" data-theme="dark"></div> 请尝试使用Google Drive API。 这就是我用来创建一个从GoogleDrive读取文件的应用程序 按照“步骤1:打开驱动器API”中的步骤在您的Google云帐户中启用凭据 首先使用immediate:true调用author

我正在与Google Drive集成。我遵循谷歌推荐的路径,将登录按钮如下:

<div class="g-signin2" data-onsuccess="onSuccessFunc" data-theme="dark"></div>
请尝试使用Google Drive API。 这就是我用来创建一个从GoogleDrive读取文件的应用程序

按照“步骤1:打开驱动器API”中的步骤在您的Google云帐户中启用凭据

首先使用
immediate:true
调用
authorize
,然后在出现错误时切换到
immediate:false

gapi.auth.authorize(authparam, (auth_result)=> {
    if (auth_result && !auth_result.error)
        $scope.handleAuthResult(auth_result)
    else {
        authparam.immediate = false;
        gapi.auth.authorize(authparam, (auth_result)=> {
            if (auth_result && !auth_result.error)
                $scope.handleAuthResult(auth_result)
            else
                console.error(auth_result)
        })
    }
})

是的,我会帮你的。问题是客户端API是自动初始化的,并且没有相关文档。我让该示例中的所有代码以相同的方式编码,但是对
gapi.auth.authorize
的调用完全失败。(顺便说一下,报告了一条非常好的错误消息)。所以我知道到底哪里不对。但是我找不到如何配置登录按钮以请求正确的权限。您应该传递
{scope:'https://www.googleapis.com/auth/drive“}
在您的
gapi.auth.authorize
调用中,以及您的
客户端id
。我给它打了两次电话,第一次是用
immediate:true
,如果失败,它会尝试用
immediate:false
。我把授权代码放在了最上面的帖子里。除了immediate变量外,第二个调用完全相同?是的,只需更新
immediate
值。已经用你的代码更新了我的答案。这很有效,谢谢。我在你链接的参考文档中没有找到。这是这个问题的最佳参考吗?
gapi.auth.authorize(authparam, (auth_result)=> {
    if (auth_result && !auth_result.error)
        $scope.handleAuthResult(auth_result)
    else {
        authparam.immediate = false;
        gapi.auth.authorize(authparam, (auth_result)=> {
            if (auth_result && !auth_result.error)
                $scope.handleAuthResult(auth_result)
            else
                console.error(auth_result)
        })
    }
})