您好我正在尝试使用插入发布删除为blogspot创建一个简单的bot
但我坚持oauth2Client
我尝试了什么:
1°获取令牌代码:
const oauth2Client = new google.auth.OAuth2(
'CLIENT_ID ,
'CLIENTE SECRET ID ',
'REDIRECT URL '
);
const scopes = [
'https://www.googleapis.com/auth/blogger'
];
const url = oauth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',
scope: scopes
});
console.log(url);
2°粘贴de url在浏览器中登录谷歌
然后使用tonken代码3°
const {tokens} = oauth2Client.getToken('TOKEN CODE THAT WAS RETURN )
oauth2Client.setCredentials({
access_token: tokens,
// Optional, provide an expiry_date (milliseconds since the Unix Epoch)
expiry_date: (new Date()).getTime() + (1000 * 60 * 60 * 24 * 7)
});
const blogger = google.blogger({
version: 'v3',
auth: oauth2Client
});
async function runSample () {
console.log("running")
const res = blogger.posts.insert({
blogId: 'BLOGID',
requestBody: {
title: 'Hello from the googleapis npm module!',
content: 'Visit https://github.com/google/google-api-nodejs-client to learn more!'
}
});
console.log(res.data);
return res.data;
}
runSample();
反应是
错误:未设置访问权限,刷新令牌或API密钥
我已经刷新了钥匙和旧东西
Tokens
返回的oauth2Client.getToken()
对象包含access_token和refresh_token(它还包含expires_in和token_type字段)。
所以你需要写:
oauth2Client.setCredentials({
access_token: tokens.access_token,
refresh_token: tokens.refresh_token,
expiry_date: (new Date()).getTime() + (1000 * 60 * 60 * 24 * 7)
});
参考:https://developers.google.com/identity/protocols/OAuth2WebServer#handlingresponse