-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
237e244
commit 146800d
Showing
1 changed file
with
14 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
import os | ||
import requests | ||
from requests_oauthlib import OAuth1 | ||
|
||
# 環境変数からTwitterのアクセストークンを取得 | ||
bearer_token = os.environ['X_BEARER_TOKEN'] | ||
# 環境変数からTwitterの認証情報を取得 | ||
consumer_key = os.environ['X_API_KEY'] | ||
consumer_secret = os.environ['X_API_KEY_SECRET'] | ||
access_token = os.environ['X_ACCESS_TOKEN'] | ||
access_token_secret = os.environ['X_ACCESS_TOKEN_SECRET'] | ||
|
||
# ツイート内容をコマンドライン引数から取得 | ||
tweet_content = os.getenv('X_POST_MESSAGE') | ||
|
||
# ツイートを作成するエンドポイント | ||
url = "https://api.twitter.com/2/tweets" | ||
# OAuth1認証 | ||
auth = OAuth1(consumer_key, consumer_secret, access_token, access_token_secret) | ||
|
||
# リクエストヘッダー | ||
headers = { | ||
"Authorization": f"Bearer {bearer_token}", | ||
"Content-Type": "application/json", | ||
} | ||
# ツイートを作成するエンドポイント | ||
url = "https://api.twitter.com/1.1/statuses/update.json" | ||
|
||
# リクエストボディ | ||
data = { | ||
"text": tweet_content, | ||
"status": tweet_content, | ||
} | ||
|
||
# POSTリクエストを送信 | ||
response = requests.post(url, headers=headers, json=data) | ||
response = requests.post(url, auth=auth, data=data) | ||
|
||
# レスポンスを確認 | ||
if response.status_code == 201: | ||
if response.status_code == 200: | ||
print("ツイートが正常に投稿されました!") | ||
else: | ||
print(f"エラーが発生しました: {response.status_code} - {response.json()}") | ||
print(f"エラーが発生しました: {response.status_code} - {response.json()}") |