3
7

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 5 years have passed since last update.

PythonとTwitterAPI使ってtweetしてみる

Last updated at Posted at 2017-12-07

はじめに

twitterを使ったbotを作りたく、手始めにpythonからAPI使ってtweetしてみます。備忘録です。

環境設定

oauthlibをインストール

Pycarmにライブラリをインストール
PyCharmによるパッケージの追加方法

Twitter認証キーの取得

TwitterAPIの取得方法

Pythonからtweetする

まずは各種認証キーをconfig.pyとして準備します。

config.py
CONSUMER_KEY = "**************"
CONSUMER_SECRET = "**************"
ACCESS_TOKEN = "**************"
ACCESS_TOKEN_SECRET = "**************"
tweet.py
# coding: UTF-8
import json, 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)

params = {"status": "Hello, World!"}
req = twitter.post("https://api.twitter.com/1.1/statuses/update.json",params = params)

実行結果

スクリーンショット 2017-12-07 10.55.10.png

やった!Pythonからtweet出来ました!

参考にさせて頂きました

PythonでTwitterAPIを叩いてみる
PythonでTwitterしてみた

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?