Ошибка токена запроса OAuth - PullRequest
0 голосов
/ 21 февраля 2019

Я пытаюсь интегрировать вход в Twitter с API Twitter.Когда я пытаюсь запросить OAuth-токен, я получаю код состояния 400 (215 код Твиттера) и не понимаю, почему.Я следовал за документацией API к T.

Вот ссылки на документацию, за которой я следовал:

Реализация Войти через Twitter

POST oauth / request_token

Авторизация запроса

Создание подписи

И вот мой код:

const http_method = 'POST';
const base_url = 'https://api.twitter.com/oauth/request_token';
const oauth_callback = 'http://127.0.0.1:5000';
const oauth_consumer_key = 'consumerKey';
const oauth_consumer_secret = 'consumerSecret';
const oauth_nonce = crypto
  .randomBytes(32)
  .toString('ascii')
  .replace(/\W/g, '');
const oauth_signature_method = 'HMAC-SHA1';
const oauth_signing_key = encodeURIComponent(oauth_consumer_secret) + '&';
const oauth_timestamp = Math.round(new Date().getTime() / 1000.0);
const oauth_version = '1.0';

// Normalized parameter string
const parameter_string =
  encodeURIComponent('oauth_callback') +
  '=' +
  encodeURIComponent(oauth_callback) +
  '&' +
  encodeURIComponent('oauth_consumer_key') +
  '=' +
  encodeURIComponent(oauth_consumer_key) +
  '&' +
  encodeURIComponent('oauth_nonce') +
  '=' +
  encodeURIComponent(oauth_nonce) +
  '&' +
  encodeURIComponent('oauth_signature_method') +
  '=' +
  encodeURIComponent(oauth_signature_method) +
  '&' +
  encodeURIComponent('oauth_timestamp') +
  '=' +
  encodeURIComponent(oauth_timestamp) +
  '&' +
  encodeURIComponent('oauth_token') +
  '=' +
  '' +
  '&' +
  encodeURIComponent('oauth_version') +
  '=' +
  encodeURIComponent(oauth_version);

// Create signature string
const signature_string =
  encodeURIComponent(http_method) +
  '&' +
  encodeURIComponent(base_url) +
  '&' +
  encodeURIComponent(parameter_string);

// Create OAuth signature
const oauth_signature = crypto
  .createHmac('sha1', oauth_signing_key)
  .update(signature_string)
  .digest('base64');

// Header parameter string
const oauth_header_string =
  'OAuth ' +
  encodeURIComponent('oauth_callback') +
  '=' +
  '"' +
  encodeURIComponent(oauth_callback) +
  '"' +
  ', ' +
  encodeURIComponent('oauth_consumer_key') +
  '=' +
  '"' +
  encodeURIComponent(oauth_consumer_key) +
  '"' +
  ', ' +
  encodeURIComponent('oauth_nonce') +
  '=' +
  '"' +
  encodeURIComponent(oauth_nonce) +
  '"' +
  ', ' +
  encodeURIComponent('oauth_signature') +
  '=' +
  '"' +
  encodeURIComponent(oauth_signature) +
  '"' +
  ', ' +
  encodeURIComponent('oauth_signature_method') +
  '=' +
  '"' +
  encodeURIComponent(oauth_signature_method) +
  '"' +
  ', ' +
  encodeURIComponent('oauth_timestamp') +
  '=' +
  '"' +
  encodeURIComponent(oauth_timestamp) +
  '"' +
  ', ' +
  encodeURIComponent('oauth_version') +
  '=' +
  '"' +
  encodeURIComponent(oauth_version) +
  '"';

// Request OAuth Token
axios
  .post(base_url, {
      headers: {
          Authorization: oauth_header_string,
      },
  })
  .then(res => console.log(res))
  .catch(console.error);

Спасибо за помощь заранее!

...