スクリプトからInstagram APIのaccess_tokenを取得する方法

Instagramapiはclient_idだけで使用できるものと、access_tokenが必要なものがあります。
例えば/media/popularなどはclient_idだけで取得できます。

特定のユーザーのpostだけを取得する/users/user-id/media/recentなどはaccess_tokenが必要です。

スクリプトからaccess_tokenを取得する方法

python-instagramリポジトリに必要なスクリプトが用意されています。
https://github.com/Instagram/python-instagram


まずsample_app.pyを動かします
これはbottleを使った小さいwebアプリケーションです

$ python sample_app.py

8515ポートで動きます。
sample_app.pyを起動したまま、こんどはget_access_token.pyを動かします

$ python get_access_token.py

コマンドライン上で以下の順番で聞かれますので、入力していきます。

1. CientID、Client Secret

http://instagram.com/developer/の右上のManage Clientsの先のページから確認できますので、コピーして入力していきます。


2. Redirect URI

先ほど動かしたsample_app.pyが8515ポートで動いてますので、http://localhost:8515/oauth_callbackを入力します


3. Request scope

データを取得するだけであればブランクで大丈夫です

「いいね」をつけたり、コメントをつけたりする場合はlikesやcommentsを入力します

詳しくはこちらの「Scope(Permissions)」に記載されてます
http://instagram.com/developer/authentication/


4. 「Visit this page and authorize access in your browser:」

上記のメッセージとURLが表示されますので、ブラウザでそのURLにアクセスします。

ブラウザ上はリダイレクトされ、まっしろなページが表示されます。
ここでURL欄のクエリパラメータのcodeをコピーします。

http://localhost:8515/oauth_callback?code=xxxxxxxxxxxxxxxxxxxxxxxxxxx

5.「Paste in code in query string after redirect:」

と聞かれてますので4でコピーしたcodeを入力します


これでaccess_tokenが取得できるはずです