Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#You need to install the oauth2 module.
import oauth2 as oauth
from urlparse import parse_qs
import sys

CONSUMER_KEY = 'YOUR-CONSUMER-KEY'        # CONSUMER_Specify the KEY.
CONSUMER_SECRET = 'YOUR-CONSUMER-SECRET'  # CONSUMER_Specify SECRET.
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
AUTHORIZE_URL = 'https://api.twitter.com/oauth/authorize'
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'

consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
client = oauth.Client(consumer)
resp, content = client.request(REQUEST_TOKEN_URL, 'GET')
request_token = parse_qs(content)
authorize_url = "%s?oauth_token=%s" % (
        AUTHORIZE_URL,
        request_token['oauth_token'][0]
        )
message = 'Authorize this app at %s and enter the PIN#' % (authorize_url)
oauth_verifier = ''
while len(oauth_verifier) <= 0:
    oauth_verifier = raw_input(message)
else:
    token = oauth.Token(
            request_token['oauth_token'][0],
            request_token['oauth_token_secret'][0]
            )
    client = oauth.Client(consumer, token)
    resp, content = client.request(
            ACCESS_TOKEN_URL,
            "POST",
            body="oauth_verifier=%s" % oauth_verifier
            )
    access_token = parse_qs(content)
    if 'oauth_token' in access_token and 'oauth_token_secret' in access_token:
        print "oauth_token: %s\noauth_token_secret: %s" % (
                access_token['oauth_token'][0],
                access_token['oauth_token_secret'][0]
                )
    else:
        sys.stderr.write('PIN is invalid.\n')

Recommended Posts

Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Access the Twitter API in Python
Tweet using the Twitter API in Python
Get the EDINET code list in Python
Hit the New Relic API in Python to get the server status
I tried to get the authentication code of Qiita API with Python.
How to get the files in the [Python] folder
I want to get the file name, line number, and function name in Python 3.4
How to get the variable name itself in python
How to get the number of digits in Python
Sample code to get started with GLSL shaders in Processing (either Java or Python)
[Note] How to write QR code and description in the same image with python
Try hitting the Twitter API quickly and easily with Python
Get your heart rate from the fitbit API in Python!
Get the MIME type in Python and determine the file format
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
Get the current date and time in Python, considering the time difference
Specification generation and code generation in REST API development (Python edition)
Tweet Now Playing to Twitter using the Spotify API. [Python]
Call github api in python to get pull request information
Getting the arXiv API in Python
Hit the Sesami API in Python
In the python command python points to python3.8
Recursively get the Excel list in a specific folder with python and write it to Excel.
Hit the web API in Python
Get the desktop path in Python
Get the host name in Python
An easy way to hit the Amazon Product API in Python
Operate Jupyter with REST API to extract and save Python code
I wrote the code to write the code of Brainf * ck in python
Regularly upload files to Google Drive using the Google Drive API in Python
[SEO] Flow / sample code when using Google Analytics API in Python
Get the title and delivery date of Yahoo! News in Python
Get the IPv4 address assigned to the network interface in code (Linux)
How to display bytes in the same way in Java and Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Sample script to trap signals in Python
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
[Python] Get the character code of the file
Try hitting the YouTube API in Python
How to use is and == in Python
[Python Kivy] How to get the file path by dragging and dropping
The file name was bad in Python and I was addicted to import
Get and convert the current time in the system local timezone with python
Get the number of articles accessed and likes with Qiita API + Python
I tried to get the movie information of TMDb API with Python
I tried to refactor the template code posted in "Getting images from Flickr API with Python" (Part 2)
How to use the C library in Python
How to get started with the 2020 Python project (windows wsl and mac standardization)
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
I can't use the "next_results" parameter in the Twitter API Search API! ?? Causes and remedies
How to generate permutations in Python and C ++
I stumbled on the character code when converting CSV to JSON in Python
Use the MediaWiki API to get Wiki information
Try using the BitFlyer Ligntning API in Python
I tried to automate internal operations with Docker, Python and Twitter API + bonus
Personal notes to doc Python code in Sphinx