[PYTHON] Post from another account with Twitter API

Overview

It's easy to tweet automatically from your account using the Twitter API, but a memo when I didn't know how to tweet from another account [^ 1]. [^ 1]: Since the purpose of use should be stated when applying for the API, it is naturally NG to use it beyond that range. At your own risk.

Premise

--Python is available (I'm version 3.7.0) --Twitter API application has been completed

Preparation

Package installation

I will use here (I am version 1.18.0).

pip install twitter

Creating an application

The method to make a note from now on is probably using PIN-based authorization. Callback_url doesn't seem to be used. However, since the official document says as follows, I will also fill in the Callback URLs like the image [^ 2]. [^ 2]: It shouldn't be used, so anything is fine, but for the time being, I used the URL of my github page.

The callback_url within the Twitter app settings is still required, even when using PIN-based auth.

image.png

Python script preparation

Prepare the following two files in the same directory.

config.py


app_name = "XXXXXXXXXX" #Application name created above
consumer_key = "XXXXXXXXXX" #Consumer API Keys on the application management screen> API key
consumer_secret = "XXXXXXXXXX"  #Consumer API Keys on the application management screen> API secret key

main.py


from twitter import *
from config import *
oauth_dance(app_name, consumer_key, consumer_secret, token_filename="./config.txt", open_browser=False)

Executing a Python script

When you run main.py, the URL will be displayed as shown below and you will be prompted to enter your PIN.

Hi there! We're gonna get you all set up to use sample.
Opening: https://api.twitter.com/oauth/authorize?oauth_token=xxxxxxxxxx


Please go to the following URL, authorize the app, and copy the PIN:

https://api.twitter.com/oauth/authorize?oauth_token=xxxxxxxxxx
Please enter the PIN:

Open the URL in your browser, and then log in with the account you want to tweet and get a PIN [^ 3]. When you enter the PIN, the following message will be displayed and the token and token_secret will be saved in config.txt.

[^ 3]: Normally, I would ask the user of my application to log in, but I checked the operation with my sub-account.

That's it! Your authorization keys have been written to ./config.txt.

Post

The actual Python script to tweet is as follows. The same package as used for preparation is used.

from twitter import *
from config import *
t = Twitter(
    auth=OAuth(
        'XXXXXXXXXX', # token(config.1st line of txt)
        'XXXXXXXXXX', # token_secret(config.2nd line of txt)
        consumer_key,
        consumer_secret,
    )
)
t.statuses.update(status="message")

If you change the " message " on the last line to any string, you should be able to tweet as you like.

Recommended Posts

Post from another account with Twitter API
Collecting information from Twitter with Python (Twitter API)
Use Twitter API with Python
Twitter post from command line
Post to your account using the API on Twitter
Support yourself with Twitter API
Successful update_with_media with twitter API
Specifying the date with the Twitter API
Post multiple Twitter images with python
Easily post to twitter with Python 3
Tweet from python with Twitter Developer + Tweepy
Extract sudden buzzwords with twitter streaming API
Collecting information from Twitter with Python (Environment construction)
Tweet regularly with the Go language Twitter API
Use twitter API (API account registration and tweet acquisition)
How to automatically generate API document with Django REST framework & POST from document screen
Post youtube soaring title to twitter with python3
Hit the Twitter API after Oauth authentication with Django
Collecting information from Twitter with Python (morphological analysis with MeCab)
Get schedule from Garoon SOAP API with Python + Zeep
[Life hack] Women's Anna support bot with Twitter API
Crawling with Python and Twitter API 1-Simple search function
[Piyopiyokai # 1] Let's play with Lambda: Get a Twitter account
How to post a ticket from the Shogun API
Pick up only crispy Japanese with Twitter streaming API
Twitter OAuth with Django
Extrude with Fusion360 API
Try hitting the Twitter API quickly and easily with Python
Image recognition with API from zero knowledge using AutoML Vision
I tried follow management with Twitter API and Python (easy)
Streamline information gathering with the Twitter API and Slack bots
Get data from analytics API with Google API Client for python
Collecting information from Twitter with Python (MySQL and Python work together)
Python: Extract file information from shared drive with Google Drive API