'Twitter oAuth using chrome extension

I am working on twitter oauth through chrome extension. I need to get oauth_token to authenticate the user. I am referring to https://developer.twitter.com/en/docs/tutorials/authenticating-with-twitter-api-for-enterprise/oauth1-0a-and-user-access-tokens. Can you guide me to send post request for my oauth token in javascript ?

You can refer to the above link for steps but I need to implement my post request in background.js instead to sending it in postman. I need my ext to create new request for each login, which for create different oauth token for each session.

I want to create a post request with following requirements:

URL-'https://api.twitter.com/oauth/request_token'

query- 'oauth_callback':'oob'

auth- we want to provide consumer key and consumer secret here

headers- 'Content-Type':'application/json'

This is a screenshot of postman. On implementing this, the post request returns oauth token and secret.

Please help me out on this.



Solution 1:[1]

import oauth from 'oauth';

const oauthCallback = process.env.FRONTEND_URL;
const CONSUMER_KEY = process.env.CONSUMER_KEY;
const CONSUMER_SECRET = process.env.CONSUMER_SECRET;
const _oauth = new oauth.OAuth(
  'https://api.twitter.com/oauth/request_token',
  'https://api.twitter.com/oauth/access_token',
  CONSUMER_KEY, // consumer key
  CONSUMER_SECRET, // consumer secret
  '1.0',
  oauthCallback,
  'HMAC-SHA1',
);

export const getOAuthRequestToken = () => {
  return new Promise((resolve, reject) => {
    _oauth.getOAuthRequestToken((error, oauth_token, oauth_token_secret,       
results) => {
  if (error) {
    reject(error);
  } else {
    console.log({ oauth_token, oauth_token_secret, results });
    resolve({ oauth_token, oauth_token_secret, results });
       }
     });
  });
 };

Try this method in your backend to get the OAuth token and secret. It helped in my case, maybe it can work for you as well.

Use this to install oauth lib

npm i oauth

Refer for more info:

https://javascript.works-hub.com/learn/building-with-twitter-authentication-35ad6

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bhawna Dey