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.
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.
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.
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.
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.
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