Skip to content

Instantly share code, notes, and snippets.

@TomXV
Last active May 2, 2020 16:50
Show Gist options
  • Save TomXV/ab7b47eb1fc3c2d641ad76bd14feb93a to your computer and use it in GitHub Desktop.
Save TomXV/ab7b47eb1fc3c2d641ad76bd14feb93a to your computer and use it in GitHub Desktop.
API.json
{
"APIs":{
"CONSUMER_KEY":"",
"CONSUMER_SECRET":"",
"ACCESS_TOKEN":"",
"ACCESS_TOKEN_SECRET":""
}
}
# coding: UTF-8
import json
with open('API.json', 'r') as f:
json_load = json.load(f)
CONSUMER_KEY = json_load["APIs"]["CONSUMER_KEY"]
CONSUMER_SECRET = json_load["APIs"]["CONSUMER_SECRET"]
ACCESS_TOKEN = json_load["APIs"]["ACCESS_TOKEN"]
ACCESS_TOKEN_SECRET = json_load["APIs"]["ACCESS_TOKEN_SECRET"]
print("CONSUMER_KEY:", CONSUMER_KEY)
print("CONSUMER_SECRET:", CONSUMER_SECRET)
print("ACCESS_TOKEN:", ACCESS_TOKEN)
print("ACCESS_TOKEN_SECRET:", ACCESS_TOKEN_SECRET)
# coding: UTF-8
import json
from requests_oauthlib import OAuth1Session
with open('API.json', 'r') as f:
json_load = json.load(f)
#API.jsonのKeyとTokenの値を書き換えておいてください
CONSUMER_KEY = json_load["APIs"]["CONSUMER_KEY"]
CONSUMER_SECRET = json_load["APIs"]["CONSUMER_SECRET"]
ACCESS_TOKEN = json_load["APIs"]["ACCESS_TOKEN"]
ACCESS_TOKEN_SECRET = json_load["APIs"]["ACCESS_TOKEN_SECRET"]
twitter = OAuth1Session(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
url = "https://api.twitter.com/1.1/statuses/update.json"
import os
os.system("cls")
input_tweet = input("What do you want tweet?: ")
tweet = input_tweet #ツイート内容
os.system("cls")
print(input_tweet + '\n')
print("Is it okay if I tweet this?\n")
input_tweet_check = str(input("Y type to 1/N type to 0: "))
params = {"status" : tweet}
if input_tweet_check.lower() == "y" or input_tweet_check.lower() == "1":
req = twitter.post(url, params = params) #ここでツイート
if req.status_code == 200: #成功
print("Succeed!")
else: #エラー
print("ERROR : %d"% req.status_code)
else:
print("tweet canceled")
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment