1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TweepyでPINベース認証する方法

Posted at

TweepyでPINベース認証をやったのでメモ程度に

前提条件

Python 3.7.4
tweepy 3.10.0

PINベース認証する方法

tewwpy_auth.py
import tweepy
import webbrowser

TWITTER_CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxx'
TWITTER_CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxx'

# ここで oob と入力することで、アクセス許可後にPINが表示される様になる
auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, 'oob')

# 認証用URLを取得後ブラウザで開く
authorization_url= auth.get_authorization_url()
webbrowser.open(authorization_url)

# アクセスを許可後出てきたPINを入力
print('PIN CODE >>', end='')
pin_code = input()

# PINコードを元にトークンなどを取得
auth.get_access_token(pin_code)
print(f"ACCESS_TOKEN = {auth.access_token}")
print(f"ACCESS_SECRET = {auth.access_token_secret}")

# 取得した情報を認証情報に追加
auth.set_access_token(auth.access_token, auth.access_token_secret)
api = tweepy.API(auth)
username = api.me().name
print(f"ユーザー名: {username}")
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?