1
4

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.

PythonのプログラムからTwitterで呟かせる方法

Last updated at Posted at 2018-11-11

##PythonのプログラムからTwitterで呟かせる方法。

###Twitter Developerサイトからトークンを発行

###プログラムにTwitter呟き機能を組み込む
requests をインポート

import json, config
from requests_oauthlib import OAuth1Session

###使用する変数の定義

twConsumerKey = ""
twConsumerSecret = ""
twToken = ""
twTokenSecret = ""
twitter = OAuth1Session(config.twConsumerKey, config.twConsumerSecret, config.twToken, config.twTokenSecret)
twurl = "https://api.twitter.com/1.1/statuses/update.json"

###def文を使って関数 twitterBot を定義

def twitterBot(message):
    params = {"status" : message}
    req = twitter.post(twurl, params = params)
    if req.status_code == 200:
        print("Succeed!")
    else:
        print("ERROR : %d"% req.status_code)

エラーで403を出力していたら、Twitter側のエラーで、同じ文章は連続投稿できません!ってエラーの可能性が高いです。

###変数 message に出力したい文章を入れる

message = "Twitterで呟かせたい文言"

###関数 twitterBot を呼び出す

twitterBot(message)

###または、関数 twitterBot を呼び出す時に引数を渡す

twitterBot("Twitterで呟かせたい文言")
1
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?