【发布时间】:2020-07-26 23:13:56
【问题描述】:
按照 google api doc https://developers.google.com/sheets/api/quickstart/nodejs,无法通过 oauth2 客户端找到使用刷新令牌获取新令牌的方法。
文档说:
"The application should store the refresh token for future use and use the access token to access a Google API. Once the access token expires, the application uses the refresh token to obtain a new one."
如何通过 google oAuth2 Client 使用刷新令牌获取新令牌?
到目前为止,我已经使用一个简单的帖子进行了管理
const getTokenWithRefresh = async (refresh_token) => {
return axios
.post("https://accounts.google.com/o/oauth2/token", {
client_id: clientId,
client_secret: clientSecret,
refresh_token: refresh_token,
grant_type: "refresh_token",
})
.then((response) => {
// TODO save new token here
console.log("response", response.data.access_token);
return response.data;
})
.catch((response) => console.log("error", response))
}
但理想情况下希望看到更清洁的方式来做到这一点。
【问题讨论】:
-
刷新令牌只要被使用就不会过期,为什么要获得一个新令牌?
标签: node.js google-sheets google-api google-oauth google-api-nodejs-client