Skip to content

HTTPError: Response code 403 (Forbidden) using delete_tweets.js #96

Open
@CopyOfA

Description

@CopyOfA

Describe the bug
I'm new to javascript and I'm trying to use the delete_tweets.js code provided in the Twitter-API-v2-sample-code. After copying over most of the code, and making a few small changes, I am encountering a HTTP 403 error. Furthermore, I'm seeing lots of type errors when using the "got" package. I've downgraded the package to install a pre-pure-ESM version (i.e., npm install got@"<12.0.0"). The full output of the error is:

HTTPError: Response code 403 (Forbidden)
    at Request.<anonymous> (/path/to/my/code/directory/node_modules/got/dist/source/as-promise/index.js:118:42)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1655492679063,
    socket: 1655492679064,
    lookup: 1655492679066,
    connect: 1655492679117,
    secureConnect: 1655492679168,
    upload: 1655492679168,
    response: 1655492679234,
    end: 1655492679235,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 1,
      dns: 2,
      tcp: 51,
      tls: 51,
      request: 0,
      firstByte: 66,
      download: 1,
      total: 172
    }
  }
}

The header I am submitting is:

{
  Authorization: 'OAuth oauth_consumer_key="XXX", oauth_nonce="XXX", oauth_signature="XXX", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1655493042", oauth_token="XXX", oauth_version="1.0"',
  'user-agent': 'v2DeleteTweetJS',
  'content-type': 'application/json',
  accept: 'application/json'
}

To Reproduce
I've installed all the dependencies listed in the README for javascript. I've made a couple of minor changes. The first change I made is to use a .txt file to store my credentials, so at the beginning of the script I read them in using PapaParse:

function get_creds(credentialsFile) {

    let credentialFile = fs.readFileSync(
        credentialsFile,
        "utf-8"
    );

    let credentials = papa.parse(
        credentialFile,
        {
            delimiter: ",",
            header: true,
        }
    );

    return credentials.data[0]
};

const credentialFile = "/path/to/my/twitter/credentials.txt";

const credentials = get_creds(credentialFile);

I also used this credentials variable when passing in my access_token, so I've replaced some code:

(async () => {
    try {
        // ########################################################
        // THIS CODE IS COMMENTED OUT BUT IS PART OF THE ORIGINAL SCRIPT

        // // Get request token
        // const oAuthRequestToken = await requestToken();
        // // Get authorization
        // authorizeURL.searchParams.append(
        //     "oauth_token",
        //     oAuthRequestToken.oauth_token
        // );
        // console.log("Please go here and authorize:", authorizeURL.href);
        // const pin = await input("Paste the PIN here: ");
        // // Get the access token
        // const oAuthAccessToken = await accessToken(oAuthRequestToken, pin.trim());

       // ########################################################

        const oAuthAccessToken = {
            "oauth_token": credentials.access_token,
            "oauth_token_secret": credentials.access_token_secret,
            "screen_name": "my-screen-name--this-is-fake",
            "user_id": credentials.access_token.split("-")[0],
        };

        // Make the request
        const response = await getRequest(oAuthAccessToken);
        console.dir(response, {
            depth: null,
        });

    } catch (e) {
        console.log(e);
        process.exit(-1);
    }

    process.exit();
}) ();

I doubt that using this credentials file is changing much, but as a dummy check, I used the commented out method (i.e., getting a PIN from my app) and got the following error:

HTTPError: Response code 403 (Forbidden)
    at Request.<anonymous> (/path/to/my/code/directory/node_modules/got/dist/source/as-promise/index.js:118:42)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1655492679063,
    socket: 1655492679064,
    lookup: 1655492679066,
    connect: 1655492679117,
    secureConnect: 1655492679168,
    upload: 1655492679168,
    response: 1655492679234,
    end: 1655492679235,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 1,
      dns: 2,
      tcp: 51,
      tls: 51,
      request: 0,
      firstByte: 66,
      download: 1,
      total: 172
    }
  }
}

Expected behavior
I expect that I can pass in a tweet ID and it would get deleted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions