Developer登録をしてTwitterのAPIをpythonで使えるようにしてみた。

 

TwitterAPIを使いたかったのでDeveloper登録し、pythonで使ってみました。

この記事ではDeveloperへの登録方法からAPIpythonで実際に使ってみるところまでをやってみます。

 

 

はじめに

TwitterAPIを利用するにはTwitterのアカウントを作成後にDeveloperに登録する必要があります。

手順は以下の通りです。

  1. Twitterのアカウントを作成(省略します。)
  2. 電話番号の登録
  3. Developerへの登録を申請する。
  4. アプリケーション登録
  5. API key と Access tokenを用意する。

2. 電話番号の登録

Twitterのアカウントを作成後、電話番号を登録します。

Twitterのホームにある自分のアイコンから設定とプライバシーを選択、

f:id:pytry3g:20190121202254p:plain

 

次にモバイルを選択、

f:id:pytry3g:20190121202339p:plain

 

 

電話番号を入力して続けるをクリック。

このとき、電話番号は先頭の0は入力しません。

080XXXXOOOOの場合、80XXXXOOOOと入力します。私の場合、これでうまくいきました。

f:id:pytry3g:20190121202555p:plain

 

すると、認証コードが送られてくるのでそのコードを入力すると、認証が完了します。

f:id:pytry3g:20190121202716p:plain

3. Developerへの登録を申請する。

こちらにアクセスします。

右上のSign inでログイン、すでにログイン済みの場合は自分のアイコンが表示されている。

f:id:pytry3g:20190121203504p:plain

自分のTwitterアカウントにログインしてからApplyをクリック、

f:id:pytry3g:20190121203743p:plain

 

Apply for a developer accountをクリック、

f:id:pytry3g:20190121204109p:plain

 

すると、現在ログインしているTwitterのアカウントが表示されます。APIを使用したいアカウントならば Continue をクリック、

f:id:pytry3g:20190121204428p:plain

 

個人で使用したいので、I am requesting access for my own personal use を選択、

すると、Account namePrimary country of operationの入力が求められたので、

f:id:pytry3g:20190121205018p:plain

とりあえずAccount nameは適当でいいみたいです。国はJapanを選択してContinueをクリック。

APIを何に使うのか詳細に説明するページに移ります。

What use case(s) are you interested in?APIを何に使用するのかを選択します。当てはまるものすべてを選択します。

f:id:pytry3g:20190121211333p:plain

 

このページではさらにTwitter APIの使用について300字以上の英語で具体的に説明する必要があります。(空白は1文字にカウントされるみたい。)

Describe in your own words what you are building

Twitter APIを使って何をしたいのかを聞かれていて下に書かれている4つの質問に答えることができるならいいみたいです。(※訳すとたぶんこんな感じ)

  1. Twitter APIを使う目的。
  2. ツイートやユーザなどを分析するつもりなら、どんな方法や技術を使うのか?
  3. ツイートやリツイートをするならば、どういう方法でしますか?
  4. ユーザにどうやって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.

f:id:pytry3g:20190121211616p:plain

Will you product, service, or analysis make Twitter content or derived information available to a government entiry?Noを選択してContinueをクリックします。

 

あとは利用規約に同意するだけです。(※利用規約を下までスクロールしないと、チェックボックスは選択できません。)

f:id:pytry3g:20190121212224p:plain

Submit applicationをクリックするとメールが届くので、Confirm your emailをクリックします。

f:id:pytry3g:20190121212552p:plain

 

すると、以下のようなメールが届きます。

f:id:pytry3g:20190121212826p:plain

審査中なのでちょっと待ってね、と書いてあるので待ちます。

 

数十分後にこのようなメールが届きました。

f:id:pytry3g:20190121215815p:plain

どうやら追加で情報が欲しいとのこと、送られてきたメールに返信しろと書いてあるのでこのメールに返信します。というか、よく見るとさっきと質問内容が同じみたいです。

  • 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

f:id:pytry3g:20190122082033p:plain

4. アプリケーション登録

アプリケーション登録をする必要があるので、こちらにアクセスして登録します。

f:id:pytry3g:20190122100348p:plain

Create an appを選択すると、下の画面に移るのでもう一度Create an appをクリック。

f:id:pytry3g:20190122100511p:plain

 

アプリケーション情報の登録画面に移ります。

f:id:pytry3g:20190122100820p:plain

以下4つの必要な情報について書いていきます。

  1. App name
  2. Application description
  3. Website URL
  4. 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.

f:id:pytry3g:20190122110226p:plain

必要事項の入力が完了したら Create をクリック。

最後に規約をちゃんと守れよと言われるのでここでも Create をクリック。

f:id:pytry3g:20190122110620p:plain

5. API key と Access tokenを用意する。

APIを利用してツイートしたり、ツイートを取得するにはAPI keyやAccess tokenが必要になります。Keys and tokensを開くと既にAPI keyとAPI secret keyが登録されていますが、Access tokenとAccess token secretはまだなのでCreateをクリックして作りましょう。

f:id:pytry3g:20190122111455p:plain

tweepyを使ってツイートしてみる。

ここまででAPIを利用する準備ができました。今回はtweepyを使ってツイートを投稿してみます。

インストールはpip install tweepyでOkです。

下のコードはツイートするプログラムで、key や token は各自置き換え下さい。

import tweepy

consumer_key = "API key"
consumer_secret = "API secret key"
access_token = "Access token"
access_token_secret = "Access token secret"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

api.update_status("Hello World!!!")

このプログラムを実行すると、

f:id:pytry3g:20190122114650p:plain

成功です。

おわりに

この記事では、Twitter APIを利用するための過程について書いてみました。tweepyの詳細な使い方については、そのうち書く予定です。