Tweet and get tweets using Twitter's API.
Create a normal twitter account in advance, and while logged in, access twitter's development site. Click "Apply" on the upper right.
Click "Apply for a developer account".
I'm going to make a bot this time, so select "Making a bot" from Hobbyist and press "Next".
Check the contents of the account, enter the country and name, and click "Next".
You will need to enter the usage and other information. Enter each in English and press "Next". English was OK if I translated and pasted it on an appropriate translation site.
Check the contents and if there are no problems, click "Looks good!".
Since you approve it, click "Submit Application".
An email will be sent to the email address registered on twitter to complete the registration.
Click "Confirm your email".
Enter the name of the application and press "complete".
API key etc. will be issued. You can check this later, so just press "App settings".
Issue an Access token. Click "Regenerate" in "Authentication Tokens".
An access token will be issued, so save it.
Let's check if the API key obtained quickly using python is available.
config.py
CONSUMER_KEY = "hogehoge"
CONSUMER_SECRET = "hogehoge"
ACCESS_TOKEN = "hogehoge"
ACCESS_TOKEN_SECRET = "hogehoge"
test.py
# -*- coding:utf-8 -*-
import json
import config
from requests_oauthlib import OAuth1Session
CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK, CS, AT, ATS) #Twitter authentication
url = "https://api.twitter.com/1.1/statuses/user_timeline.json" #end point
params ={'count' : 1}
res = twitter.get(url, params = params)
if res.status_code == 200:
tweets = json.loads(res.text)
for tweet in tweets:
print('name : ' + tweet['user']['name'])
print('text : ' + tweet['text'])
print('created_at: ' + tweet['created_at'])
print('*******************************************')
else:
print("Failed: %d" % res.status_code)
C:\hoge>python test.py
name : hogehogeo
text : HelloWorld!!
created_at: Sat Aug 15 05:42:25 +0000 2020
*******************************************
I was able to register and check with the python code.
Recommended Posts