Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

post method throw error #364

Open
maychine-fatima opened this issue Dec 9, 2021 · 5 comments
Open

post method throw error #364

maychine-fatima opened this issue Dec 9, 2021 · 5 comments

Comments

@maychine-fatima
Copy link

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
Copy link

@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
Copy link

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
Copy link

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

@18888628835
Copy link

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

@18888628835
Copy link

@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) => {
        },
      )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants