我想从谷歌获取访问令牌。谷歌API说,要获得访问令牌,将代码和其他参数发送到令牌生成页面,响应将是一个JSON对象,如:

{
"access_token" : "ya29.AHES6ZTtm7SuokEB-RGtbBty9IIlNiP9-eNMMQKtXdMP3sfjL1Fc",
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : "1/HKSmLFXzqP0leUihZp2xUt3-5wkU7Gmu2Os_eBnzw74"
}

但是,我没有收到刷新令牌。我的回答是:

{
 "access_token" : "ya29.sddsdsdsdsds_h9v_nF0IR7XcwDK8XFB2EbvtxmgvB-4oZ8oU",
"token_type" : "Bearer",
"expires_in" : 3600
}

当前回答

为了在每次验证时获得新的refresh_token,在仪表板中创建的OAuth 2.0凭据类型应该是“Other”。同样如上所述,在生成authURL时应该使用access_type='offline'选项。

当使用类型为“Web application”的凭据时,prompt/approval_prompt变量的组合将不起作用——您仍然只能在第一个请求上获得refresh_token。

其他回答

1. 如何获得'refresh_token' ?

解决方案:access_type='offline'选项应该在生成authURL时使用。 来源:为Web服务器应用程序使用OAuth 2.0

2. 但即使有'access_type=offline',我没有得到'refresh_token' ?

解决方案:请注意,您只会在第一次请求时获得它,所以如果您将它存储在某个地方,并且在您的代码中有一个条款可以在上次过期后获得新的access_token时覆盖它,那么请确保不要覆盖这个值。

From谷歌Auth Doc:(this value = access_type)

该值指示谷歌授权服务器返回一个 刷新令牌和访问令牌,第一次您的应用程序 将授权代码交换为令牌。

如果你再次需要'refresh_token',那么你需要按照Rich Sutton的回答中所写的步骤删除对应用程序的访问。

refresh_token仅在用户第一次授权时提供。后续的授权,比如您在测试OAuth2集成时所做的授权,将不会再次返回refresh_token。:)

进入可以访问您帐户的应用程序页面: https://myaccount.google.com/u/0/permissions。 在第三方应用程序菜单下,选择您的应用程序。 单击“删除访问”,然后单击“确定”确认 下一个OAuth2请求将返回一个refresh_token(前提是它还包括'access_type=offline'查询参数)。

或者,您可以将查询参数prompt=consent&access_type=offline添加到OAuth重定向(参见谷歌的OAuth 2.0 for Web服务器应用程序页面)。

这将提示用户再次授权应用程序,并总是返回一个refresh_token。

为了获得刷新令牌,你必须同时添加approval_prompt=force和access_type="offline" 如果您正在使用谷歌提供的java客户端,它将看起来像这样:

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, getClientSecrets(), scopes)
            .build();

AuthorizationCodeRequestUrl authorizationUrl =
            flow.newAuthorizationUrl().setRedirectUri(callBackUrl)
                    .setApprovalPrompt("force")
                    .setAccessType("offline");

在我意识到添加access_type=offline是在前端客户端对授权代码的请求上完成的,而不是在将该代码交换为access_token的后端请求上完成的之后,Rich Sutton的回答终于对我有用了。我给他的回答加上了一条评论,并在谷歌上添加了这个链接,以获取有关刷新令牌的更多信息。

注:如果你正在使用Satellizer,下面是如何将该选项添加到$authProvider。AngularJS中的谷歌。

我正在使用nodejs客户端访问私有数据

解决方案是在oAuth2Client的settings对象中添加值为consent的promp属性。generateAuthUrl函数。 这是我的代码:

const getNewToken = (oAuth2Client, callback) => {
    const authUrl = oAuth2Client.generateAuthUrl({
        access_type: 'offline',
        prompt: 'consent',
        scope: SCOPES,
    })
    console.log('Authorize this app by visiting this url:', authUrl)
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
    })
    rl.question('Enter the code from that page here: ', (code) => {
        rl.close()
        oAuth2Client.getToken(code, (err, token) => {
            if (err) return console.error('Error while trying to retrieve access token', err)
            oAuth2Client.setCredentials(token)
            // Store the token to disk for later program executions
            fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
                if (err) return console.error(err)
                console.log('Token stored to', TOKEN_PATH)
            })
            callback(oAuth2Client)
        })
    })
}

你可以使用在线参数提取器来获取生成令牌的代码:

在线参数提取器

以下是谷歌官方文档的完整代码:

https://developers.google.com/sheets/api/quickstart/nodejs

我希望这些信息对你有用