Skip to content

Instantly share code, notes, and snippets.

@rayterrill
Created April 15, 2023 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayterrill/78c5dfa25265b9c6f2294c96f99ae56c to your computer and use it in GitHub Desktop.
Save rayterrill/78c5dfa25265b9c6f2294c96f99ae56c to your computer and use it in GitHub Desktop.

Build Project, App, and Generate Keys

  1. Sign into twitter with your bot account, and go to https://developer.twitter.com.
  2. If you're in the free account tier and your previous app was suspended, you may need to delete it. Free tier accounts appear to only support 1 app at a time.
  3. Create a project if you don't already have one, and then create an app. Make sure you take note of the keys you get at the end - you'll need these. These are your Consumer Keys and authenticate your application.
  4. Click on your app in the Developer Portal, make sure you're on the "Settings" tab, and click "Set up" under "User authentication settings"
  5. Select "Read and write" under "App permissions, "Web App, Automated App or Bot" under "Type of App", and enter something for Callback URI and Website URL (these aren't used in my flow), then click "Save". You'll get a Client ID and Client Secret - I didn't need these so I just ignored this.
  6. On the app page, click onto the "Keys and tokens" tab, then click "Generate" on "Access Token and Secret" under the "Authentication Tokens" section. Take note of these - you'll need these later. Make sure once these are generated you see "Created with Read and Write permissions" under the "Access Token and Secret" section. These are your user keys and authenticate the bot account as a user.

Code Example

I leveraged twitter-api-v2 to perform operations against the twitter api.

Here's a basic example in NodeJS of taking an image and posting it as a tweet:

'use strict'

//v2 api
const { TwitterApi, EUploadMimeType } = require('twitter-api-v2');

//build our client
const v2Client = new TwitterApi({
  appKey: process.env.TWITTER_APP_KEY,
  appSecret: process.env.TWITTER_APP_SECRET,
  accessToken: process.env.TWITTER_ACCESS_TOKEN,
  accessSecret: process.env.TWITTER_ACCESS_SECRET,
});

//build a function we can use elsewhere
async function postTweet(image, imageURL, description) {
  try {
    console.log("uploading image...")
    const mediaId = await v2Client.v1.uploadMedia(Buffer.from(image), {mimeType: EUploadMimeType.Jpeg})

    var params = {
      status: 'Image link: ' + imageURL,
      media_ids: [mediaId],
    }

    console.log("posting tweet...")
    await v2Client.v2.tweet({text: params.status, media: { media_ids: params.media_ids }});
  } catch (error) {
    console.log('Error occured: ' + error)
  }
}

if (require.main === module) {
  //application is run directly - like for testing
  postTweet()
} else {
  //application imported as a module via require - export function
  module.exports.postTweet = postTweet
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment