gpt4 book ai didi

rest - 如何刷新 OAuth2 token ?我需要等待 token 过期吗? (Patreon API)

转载 作者:搜寻专家 更新时间:2023-10-30 21:58:28 27 4
gpt4 key购买 nike

我正在使用 Patreon 的 api 试用 OAuth。我对 OAuth 流程还​​很陌生,一直在使用 Patreon 的 Javascript 包来帮助我管理请求。

到目前为止,我已经能够通过以下方式成功获取 token :

import * as patreon from 'patreon';
const patreonOAuthClient = patreon.oauth(clientId, clientSecret);
patreonOAuthClient.getTokens(oauthGrantCode, redirectURL).then((tokenResponse) => {
console.log(tokenResponse);
})

我收到的token是这样出来的:

   // Example Token from getTokens()'s then()-response
tokenResponse = {
access_token: "UbHYT3H51GpeYueBeBuvBj1fnEFzv5A5870s_rYeMHo",
expires_in: 2678400,
refresh_token: "AP5aAw-gJbVf35tWxQb74rmJJz2MhwIYq660m0jiZQ4",
scope: "my-campaign pledges-to-me users",
token_type: "Bearer",
version: "0.0.1"
}

在我的本地服务器中,我正在尝试让刷新 token 正常工作,这样我就不必每个月都不断询问用户许可。

尽管当我使用刷新 token 方法时,我收到了 400 错误请求:

patreonOAuthClient.refreshToken(tokenResponse).then(response => {
console.log(response, 'success!');
}).catch(err => {
console.log(err, ':(');
});

它没有在 npm 文档中显示,但您可以在 github source code 上找到 refreshToken()的 patreon。

根据 here在他们的 api 文档中:

If you wish to get up-to-date information after the token has expired, a new token may be issued to be used for the following month. To refresh a token, make a POST request to the token endpoint with a grant type of refresh_token, as in the example. You may also manually refresh the token on the appropriate client in your clients page.

我得到 400 的原因是因为我需要等待一个月才能刷新 token ,还是我只是错误地实现了 API?我希望有更多 OAuth 经验的人可以告诉我我们是否应该在 token 过期之前之后进行 token 刷新?

(如果你在它过期之前刷新它,有没有某种方法可以让快速服务器在一个月过期之前完成它?因为我认为它为每个 token 添加超时对内存来说真的很糟糕)。

最佳答案

我终于让 SDK 开始工作了。源代码给我的印象是 token 是从响应中接收到的对象。但事实证明, token 是字符串值。

所以:

// Using tokenResponse.refresh_token instead of just tokenResponse
patreonOAuthClient.refreshToken(tokenResponse.refresh_token).then(response => {
console.log(response, 'success!');
}).catch(err => {
console.log(err, ':(');
});

关于rest - 如何刷新 OAuth2 token ?我需要等待 token 过期吗? (Patreon API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50411029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com