这是一个身份验证错误,可能是因为您提供的访问令牌已过期或在其有效期之前尝试使用。解决方法是在令牌过期前重新生成一个新的访问令牌,并在调用API时使用新令牌。以下是使用JavaScript和Node.js重新生成访问令牌的示例代码:
const request = require('request');
const clientId = 'your_client_id';
const clientSecret = 'your_client_secret';
const refreshToken = 'your_refresh_token';
const options = {
url: 'https://accounts.google.com/o/oauth2/token',
form: {
client_id: clientId,
client_secret: clientSecret,
refresh_token: refreshToken,
grant_type: 'refresh_token'
}
};
request.post(options, (err, res, body) => {
if (err) {
console.error(err);
return;
}
const accessToken = JSON.parse(body).access_token;
console.log(`New Access Token: ${accessToken}`);
// Use the new access token to call the API
});