TwitterのAPIを使いたかったのでDeveloper登録し、pythonで使ってみました。
この記事ではDeveloperへの登録方法からAPIをpythonで実際に使ってみるところまでをやってみます。
- はじめに
- 2. 電話番号の登録
- 3. Developerへの登録を申請する。
- 4. アプリケーション登録
- 5. API key と Access tokenを用意する。
- tweepyを使ってツイートしてみる。
- おわりに
はじめに
TwitterのAPIを利用するにはTwitterのアカウントを作成後にDeveloperに登録する必要があります。
手順は以下の通りです。
2. 電話番号の登録
Twitterのアカウントを作成後、電話番号を登録します。
Twitterのホームにある自分のアイコンから設定とプライバシーを選択、
次にモバイルを選択、
電話番号を入力して続けるをクリック。
このとき、電話番号は先頭の0は入力しません。
080XXXXOOOOの場合、80XXXXOOOOと入力します。私の場合、これでうまくいきました。
すると、認証コードが送られてくるのでそのコードを入力すると、認証が完了します。
3. Developerへの登録を申請する。
こちらにアクセスします。
右上のSign inでログイン、すでにログイン済みの場合は自分のアイコンが表示されている。
自分のTwitterアカウントにログインしてからApplyをクリック、
Apply for a developer accountをクリック、
すると、現在ログインしているTwitterのアカウントが表示されます。APIを使用したいアカウントならば Continue をクリック、
個人で使用したいので、I am requesting access for my own personal use を選択、
すると、Account name と Primary country of operationの入力が求められたので、
とりあえずAccount nameは適当でいいみたいです。国はJapanを選択してContinueをクリック。
APIを何に使うのか詳細に説明するページに移ります。
What use case(s) are you interested in? でAPIを何に使用するのかを選択します。当てはまるものすべてを選択します。
このページではさらにTwitter APIの使用について300字以上の英語で具体的に説明する必要があります。(空白は1文字にカウントされるみたい。)
Describe in your own words what you are building
Twitter APIを使って何をしたいのかを聞かれていて下に書かれている4つの質問に答えることができるならいいみたいです。(※訳すとたぶんこんな感じ)
- Twitter APIを使う目的。
- ツイートやユーザなどを分析するつもりなら、どんな方法や技術を使うのか?
- ツイートやリツイートをするならば、どういう方法でしますか?
- ユーザにどうやってTwitterのデータを見せるのか?
私は画像の通り書きました。
適当に書いた回答例(Chatbotが目的の場合)
I want to use Twitter API to make chatbot for contribution to research in artificial intelligence. I plan to analyze Tweets attribute written in Japanese and classify them according to the user preferences. In addition, my chatbot will engage in dialogue between Twitter users. I will display all of these attempts to the users.
Will you product, service, or analysis make Twitter content or derived information available to a government entiry?はNoを選択してContinueをクリックします。
あとは利用規約に同意するだけです。(※利用規約を下までスクロールしないと、チェックボックスは選択できません。)
Submit applicationをクリックするとメールが届くので、Confirm your emailをクリックします。
すると、以下のようなメールが届きます。
審査中なのでちょっと待ってね、と書いてあるので待ちます。
数十分後にこのようなメールが届きました。
どうやら追加で情報が欲しいとのこと、送られてきたメールに返信しろと書いてあるのでこのメールに返信します。というか、よく見るとさっきと質問内容が同じみたいです。
- Twitter APIを使う目的。
- ツイートやユーザなどを分析するつもりなら、どんな方法や技術を使うのか?
- ツイートやリツイートをするならば、どういう方法でしますか?
- ユーザにどうやってTwitterのデータを見せるのか?
それぞれの質問に回答し返信すると数十分後にDeveloper登録の承認メールが届きました。(※長い文章は書いていませんが、無事承認されました。)
適当に書いた回答例(Chatbotが目的の場合)
- The core use case, intent, or business purpose for your use of the Twitter APIs
I will use Twitter APIs to build chatbot.
- If you intend to analyze Tweets, Twitter users, or their content, share details about the analyses you plan to conduct and the methods or techniques
I plan to use deep learning to analyze Tweets.
- If your use involves Tweeting, Retweeting, or liking content, share how you will interact with Twitter users or their content
I will build chatbot implemented artificial intelligence. My chatbot will post the tweet and be able to talk to the user in Japanese.
- If you’ll display Twitter content off of Twitter, explain how and where Tweets and Twitter content will be displayed to users of your product or service, including whether Tweets and Twitter content will be displayed at row level or aggregated
Every Twitter user can see the content of service through Twitter applications
4. アプリケーション登録
アプリケーション登録をする必要があるので、こちらにアクセスして登録します。
Create an appを選択すると、下の画面に移るのでもう一度Create an appをクリック。
アプリケーション情報の登録画面に移ります。
以下4つの必要な情報について書いていきます。
- App name
- Application description
- Website URL
- Tell us how this app will be used
1. App name
アプリケーションの名前を付ける。(日本語でOK。)
2. Application description
どういうアプリケーションなのかを説明する。適当でいいみたい。
3. Website URL
私は自分のブログURLを登録しました。Qiitaとか自分のアカウントのURLでもいいらしい。
4. Tell us how this app will be used
アプリケーションをどう使うのか説明する。適当でいいみたい。
適当に書いた回答例(Chatbotが目的の場合)
I will build chatbot implementing artificial intelligence. This application will post tweet automatically and retweets according to the user preferences. In addition, I will implement chatbot which is able to communicate with the user in Japanese.
必要事項の入力が完了したら Create をクリック。
最後に規約をちゃんと守れよと言われるのでここでも Create をクリック。
5. API key と Access tokenを用意する。
APIを利用してツイートしたり、ツイートを取得するにはAPI keyやAccess tokenが必要になります。Keys and tokensを開くと既にAPI keyとAPI secret keyが登録されていますが、Access tokenとAccess token secretはまだなのでCreateをクリックして作りましょう。
tweepyを使ってツイートしてみる。
ここまででAPIを利用する準備ができました。今回はtweepyを使ってツイートを投稿してみます。
インストールはpip install tweepy
でOkです。
下のコードはツイートするプログラムで、key や token は各自置き換え下さい。
このプログラムを実行すると、
成功です。
おわりに
この記事では、Twitter APIを利用するための過程について書いてみました。tweepyの詳細な使い方については、そのうち書く予定です。