ciaranj / node-oauth

OAuth wrapper for node.js
MIT License
2.44k stars 662 forks source link

post method throw error #364

Open maychine-fatima opened 2 years ago

maychine-fatima commented 2 years ago

after usign it with get, it worked but when i wanted to use it with an api with post methode it therows error this is my code

`    const oauth = new OAuth.OAuth(
      "https://api.twitter.com/oauth/request_token",
      "https://api.twitter.com/oauth/access_token",
      TWITTER_CONSUMER_KEY,
      TWITTER_CONSUMER_SECRET,
      "1.0A",
      null,
      "HMAC-SHA1"
    );
    oauth.post(
      "https://api.twitter.com/2/tweets",
      TWITTER_ACCESS_TOKEN, //  user token
      TWITTER_SECRET_TOKEN, //  user secret
      { text: "hi" },
      "application/json",
      async function(e, data) {
        if (e) console.log(e);
        console.log(data);
      });`

it return this error { statusCode: 400, data: '{"errors":[{"parameters":{},"message":"Requests with bodies must have content-type of application/json."}],"title":"Invalid Request","detail":"One or more parameters to your request was invalid.","type":"https://api.twitter.com/2/problems/invalid-request"}' }

aditodkar commented 2 years ago

@maychine-fatima Any update on this? Is it working for you now? I am also facing similar issue. FYR: https://twittercommunity.com/t/how-to-use-twitter-v2-apis/163669

aditodkar commented 2 years ago

Not sure if this issue is specific to node-oauth but using axios I was able to tweet using twitter v2 APIs. Working code snippet added for reference:

const express = require('express');
const router = express.Router();
const OAuth = require('oauth-1.0a');
const axios = require('axios');

router.post('/twitter/tweet', async (req, res) => {
    try {
        const oauth = OAuth({
            consumer: {
                key: process.env.TWITTER_CONSUMER_KEY,
                secret: process.env.TWITTER_CONSUMER_SECRET
            },
            signature_method: 'HMAC-SHA1',
            hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
        });

        const token = {
            key: '',
            secret: ''
        };

        const authHeader = oauth.toHeader(oauth.authorize({
            url: 'https://api.twitter.com/2/tweets',
            method: 'POST'
        }, token));

        const data = { "text": "Hello world!!" };

        await axios.post('https://api.twitter.com/2/tweets',
            data,
            {
                headers: {
                    Authorization: authHeader["Authorization"],
                    'user-agent': "v2CreateTweetJS",
                    'content-type': "application/json",
                    'accept': "application/json"
                }
            }
        );

        res.status(201).send({ message: "Tweet successful" });
    } catch (error) {
        console.log("error", error)
        res.status(403).send({ message: "Missing, invalid, or expired tokens" });
    }
});

FYR: @maychine-fatima

18888628835 commented 1 year ago

what's matter with the authors?Why don't resolve this issue?

18888628835 commented 1 year ago

oh my god。this issue has been here for two years ~~

18888628835 commented 1 year ago

@maychine-fatima @aditodkar I fixed this problem with the following code:

      oauth.post(
        `https://api.twitter.com/2/users/${twitterUserId}/following`,
        accessToken,
        accessTokenSecret,
        JSON.stringify({ target_user_id: '1613090899432722432' }),
        'application/json',
        (err, result) => {
        },
      )