Post to your account using the API on Twitter

I touched Twitter for the first time in a long time, and when I tried to create a process to automatically POST tweets to my account, I unexpectedly got a lot of points, so it is a memorandum.

Step 1: Create a Twitter account

The procedure for creating an account for a normal user is OK.

It's different from the old days, such as being able to register with a phone number, and I'm a little excited.

Step 2: Apply for API usage

It seems that an application was required to use it from around last year ...

[Summary of procedures from Twitter API registration (account application method) to approval](Summary of procedures from Twitter API registration (account application method) to approval)

I will refer to and clear.

Since I couldn't get hit by the application, I tried my best to write the purpose of use ** a little bit more ** so that it looks like a serious person. ), ** The next moment I applied, the approval email flew and ** I missed the beat. Even if you check the contents, it probably is not absolutely human power.

Step 3: Create an app and issue an authentication key

I'm worried that I'm not creating an app, but it's necessary to issue an authentication key, so I'm afraid to click the Create New App button to create an app and issue an authentication key. I wonder if the following page will be helpful.

How to use Twitter REST API

Step 4: Call the API for posting (using Python)

With reference to the sample code in Official Document, it looks like this in the end.

tweet.py


def tweet(text):
    url = "https://api.twitter.com/1.1/statuses/update.json?status={}".format(text)

    consumer = oauth2.Consumer(key='API key of Consumer API keys that can be obtained in step 3', secret='API secret key of Consumer API keys that can be obtained in step 3')
    token = oauth2.Token(key='Access token that can be obtained in step 3', secret='Access token that can be obtained in step 3 secret')
    client = oauth2.Client(consumer, token)
    resp, content = client.request( url, method="POST")
    
    return content

tweet("test")

Actually, this was the ** biggest addiction point **. The result was very easy, but I couldn't find a page that explained it quickly because it was easy or because of it.

In the first place, it was a bit surprising that the tweet posting was a path called "statuses / update", and I was stuck there for a while, but above all, there are pages that search and hit even in the official documents published by Twitter ** The premise is that the service "authenticates to a third party and posts to that user's account" **, and ** just wanting to use it as a single user is too easy and is neglected. (*) ** There was even. (Persecutory delusion)

Anyway, this is a successful tweet.

We hope you find this useful.

* Digression

As for how much the API usage by a single user is neglected, [a syntax error is mixed in the sample code of the official document published by Twitter](https://developer.twitter.com/ en / docs / basics / authentication / guides / single-user) level.

The following is the code copied from ʻUsing Python-OAuth2 library on the above page, but pay attention to the argument post_body`.

def oauth_req(url, key, secret, http_method="GET", post_body=””, http_headers=None):
    consumer = oauth2.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
    token = oauth2.Token(key=key, secret=secret)
    client = oauth2.Client(consumer, token)
    resp, content = client.request( url, method=http_method, body=post_body, headers=http_headers )
    return content

home_timeline = oauth_req( 'https://api.twitter.com/1.1/statuses/home_timeline.json', 'abcdefg', 'hijklmnop' )

You'll be crying like this.

Recommended Posts

Post to your account using the API on Twitter
Try using the Twitter API
Try using the Twitter API
Tweet Now Playing to Twitter using the Spotify API. [Python]
Tweet using the Twitter API in Python
Post the subject of Gmail on twitter
Regularly post to Twitter using AWS lambda!
Post from another account with Twitter API
Save the search results on Twitter to CSV.
Steps to install the latest Python on your Mac
Try to delete tweets in bulk using Twitter API
How to post a ticket from the Shogun API
Try using the PeeringDB 2.0 API
Create a real-time auto-reply bot using the Twitter Streaming API
Image Optimize on the server side using TinyPNG's Web API
Specifying the date with the Twitter API
How to get followers and followers from python using the Mastodon API
[Lambda] [Python] Post to Twitter from Lambda!
I tried using the COTOHA API (there is code on GitHub)
I tried to digitize the stamp stamped on paper using OpenCV
Try to log in to Netflix automatically using python on your PC
Easily post to twitter with Python 3
Regularly upload files to Google Drive using the Google Drive API in Python
Access the Twitter API in Python
Notes on using matplotlib on the server
I tried using the checkio API
For the time being using FastAPI, I want to display how to use API like that on swagger
Connect to VPN with your smartphone and turn off / on the server
[EV3 x Python] Stream the camera image to your PC using mjpg-streamer.
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
I tried to summarize various sentences using the automatic summarization API "summpy"
Snow on your Mac the old-fashioned way
Try using the Kraken API in Python
Bulk posting to Qiita: Team using Qiita API
Run the Matrix to your boss's terminal!
Put Anaconda on your Mac using Pyenv
I stumbled on the Hatena Keyword API
Get Twitter bookmarks on CentOS using Selenium
Notes on using OpenCL on Linux on the RX6800
5 reasons to install Linux on your laptop.
Create an application using the Spotify API
Program to get favorite images on Twitter
I tried to touch the COTOHA API
Play with puns using the COTOHA API
Record custom events using the Shotgun API
I tried using the BigQuery Storage API
[2015/11/19] How to register a service locally using the python SDK on naoqi os
Let the COTOHA API do the difficult things-Introduction to "learn using" natural language processing-
[Rails 6 / Google Map API] Post an address and set multiple markers on the map
The key to deploying Flask apps using Python Buildpack on Cloud Foundry (Diego)
Automatically post aggregated Twitter data to WordPress with cron to create the best self-log
Use twitter API to get the number of tweets related to a certain keyword
I want to convert horizontal text to vertical text and post it on Twitter etc.
Convert the cURL API to a Python script (using IBM Cloud object storage)
To automatically send an email with an attachment using the Gmail API in Python
Continue to retrieve tweets containing specific keywords using the Streaming API in Python
Tweet regularly with the Go language Twitter API
I checked the library for using the Gracenote API
I tried using Remote API on GAE / J
Use twitter API (API account registration and tweet acquisition)
Procedure to use TeamGant's WEB API (using python)