The X developer documentation is all over the place currently. Transition from v1 to v2, update to plans and access levels and so on.
To create an automated bot on X we need to follow the steps below:
- you will need to register into the Developer Portal
- to access the new V2 API you will need to create a Project and an App
- after the App is created you will need to configure "user authentication" from the settings tab of the app. Make it read and write.
- fill in the Callback URI as it will be used to get the token as GET variables
- You will need a new account on X for the bot, go and register a new account on https://x.com
- after the account is set go to Settings and Privacy, Your account, Account Information, Automation and link the bot account to your X account that will be used with the API
- now we need to request an OAuth token, you can use Postman and send a POST request to this endpoint https://api.x.com/oauth/request_token . Use OAuth 1.0 and consumer key, consumer secret, access token and token secret from the developer portal. The response will contain oauth_token and oauth_token_secret
- open a new tab in the browser that is logged in with the bot account and open this link https://api.x.com/oauth/authenticate?oauth_token= use the oauth_token received in the previous step
- you will need to allow the app to access the bot account, after that the window will refresh to the callback URI with oauth_verifier and oauth_token appended as GET variables
- back to Postman and send another POST request, this time with no auth to the endpoint https://api.x.com/oauth/access_token?oauth_verifier=XXXXXXX&oauth_token=YYYYYYYY - use the values received in the previous step
- the response will contain the oauth_token and oauth_token_secret that you can use to interact with X API on behalf of your bot. Use the oauth_token as Access Token and oauth_token_secret as Access Token Secret
Time to test the API.
In Postman use this endpoint https://api.twitter.com/2/tweets OAuth 1.0 as Authorization and the tokens and secrets from above. The body should contain the JSON data:
{
"text": "Hello World!"
}
A proper response will be:
{
"data": {
"id": "1852759774275707198",
"edit_history_tweet_ids": [
"1852759774275707198"
],
"text": "Hello World!"
}
}