Простой блоггер бот - PullRequest
       6

Простой блоггер бот

0 голосов
/ 30 августа 2018

Здравствуйте, я пытаюсь сделать простого бота для блога с вставить публиковать удалить

но я застрял на 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 ° Вставьте ссылку в браузере для входа в Google

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

я уже обновляю ключи и ту же старую вещь

1 Ответ

0 голосов
/ 03 сентября 2018

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

...