Twitter_api_v2 reply to a tweet example

319 Views Asked by At

Could anyone Show me an example of your finished Parameters and Endpoint for a Twitter Reply maybe with a Screenshot? Because i dont understand exactly what to type in my Params and, do I got to change anything in the Pre-request Script?

Kind regards Alex

For the Params for https://api.twitter.com/2/tweets I tried:

Key : in_reply_to

Value : tweet_id

And the Result was "errors"

               "message": "The query Parameters [in_reply_to] is not one of [expantions,tweet.fields,media.fields,poll.fields,place.fields,user.fields]"

"title":"Invalid Request" "detail": "One or more Parameters to your request Was invalid.", "type":"https://api.twitter.com/2/problems/invalid-request"

1

There are 1 best solutions below

2
On

From the twitter's documentation query parameter ids is required. You missed that parameter.

enter image description here

I will get tweet this demo

https://twitter.com/pascal_bornet/status/1604754709000200193

enter image description here

BY Postman

enter image description here

Full code by node.js

#1 Get an access token by API Key and API secret #2 Get text by access token

Credential in config.json

{
    "API_KEY" : "7hK your API Key GND",
    "API_KEY_SECRET" : "Zr4 your API Key secret 0qX0"
}

Save as get-tweet.js

const axios = require('axios')
const config = require('./config.json');

const getAccessToken = async () => {
    try {
        const resp = await axios.post(
            'https://api.twitter.com/oauth2/token',
            '',
            {
                params: {
                    'grant_type': 'client_credentials'
                },
                auth: {
                    username: config.API_KEY,
                    password: config.API_KEY_SECRET
                }
            }
        );
        // console.log(resp.data);
        return Promise.resolve(resp.data.access_token);
    } catch (err) {
        // Handle Error Here
        console.error(err);
        return Promise.reject(err);
    }
};

const getTweetText = async (token, tweet_id) => {
    try {
        const resp = await axios.get(
            `https://api.twitter.com/2/tweets?ids=${tweet_id}`,
            {
                headers: {
                    'Authorization': 'Bearer '+ token,
                }
            }
        );
        return Promise.resolve(resp.data);
    } catch (err) {
        // Handle Error Here
        console.error(err);
        return Promise.reject(err);
    }
};

getAccessToken()
    .then((token) => {
        console.log(token);
        getTweetText(token, '1604754709000200193')
            .then((result) => {
                console.log(result.data[0].text);
            })
    })

Get Result

$ node get-tweet.js
AAAAAksadf--very long access token in here ----JlIMJIIse
Is this the future of Christmas shopping?

Credit: Nike
#innovation #AR # VR #AugmentedReality https://~~~

enter image description here