[PYTHON] Use twitter API (API account registration and tweet acquisition)

prologue

Tweet and get tweets using Twitter's API.

Register for an account

Create a normal twitter account in advance, and while logged in, access twitter's development site. Click "Apply" on the upper right. 1.png

Click "Apply for a developer account". 2.png

I'm going to make a bot this time, so select "Making a bot" from Hobbyist and press "Next". 3.png

Check the contents of the account, enter the country and name, and click "Next". 4.png

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

Check the contents and if there are no problems, click "Looks good!". 6.png

Since you approve it, click "Submit Application". 7.png

An email will be sent to the email address registered on twitter to complete the registration. 8.png

Click "Confirm your email". 9.png

Enter the name of the application and press "complete". 10.png

API key etc. will be issued. You can check this later, so just press "App settings". 11.png

Issue an Access token. Click "Regenerate" in "Authentication Tokens". 12.png

An access token will be issued, so save it.

Try using

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

epilogue

I was able to register and check with the python code.

Recommended Posts

Use twitter API (API account registration and tweet acquisition)
Use Twitter API with Python
Beginners of Google Maps API and Twitter API made "tweet map"
[Go language] Use OpenWeatherMap and Twitter API to regularly tweet weather information from Raspberry Pi
Continuous acquisition by Twitter API (Tips)
I tried using Twitter api and Line api
Tweet using the Twitter API in Python
Post from another account with Twitter API
How to use Service Account OAuth and API with Google API Client for python
Tweet regularly with the Go language Twitter API
Use JIRA API
Crawling with Python and Twitter API 1-Simple search function
Book registration easily with Google Books API and Rails
Post to your account using the API on Twitter
Install tweepy with pip and use it for API 1.1