instagram で遊ぶ前に説明。

instagram で遊ぶためにはどうしたらいいのか、サイトのドキュメントを交えて流れを追っていきます。認証に関する説明は、ドキュメントのこのページに記載されています。(訳に関しては意訳でかなりぐだぐだですが気にしないでください。間違いがあった場合の訂正は、ぜひよろしくおねがいします。)

Instagram は簡易で、しかし効果的な認証と承認のため、OAuth 2.0 プロトコルを利用しています。

Instagram's API uses the OAuth 2.0 protocol for simple, yet effective authentication and authorization.

あなたは認証する必要がありますか?

多くの場合、あなたはユーザを認証させなくてもいい、ということに注意してください。たとえば、あなたが今日人気の写真を見せるシンプルなアプリケーションを作るとします。この場合、あなたは API からデータを読むのにアクセストークンを持つ必要はありません。ただ、あなたのクライアント ID をリクエスト時に利用してください。あなたが認証されたユーザに代わってアクセスすることを強制したいときだけ、アクセストークンを必須要件にしています。

Note that in many situations, you may not authenticate users at all. For instance, you may create an app that simply shows the most popular photos today. In this case, you do not need to have an access_token to read from the API – just use your client ID with your request. We have only included the access_token requirement where we would like to enforce that you are representing an authenticated user (commenting, liking, browsing a user’s feeds).

アクセストークンの受け取り

ユーザに代わってAPIにアクセスするためにアクセストークンを受け取るには、あなたは二つのことをする必要があります。

In order to receive an access_token to access the API on a user's behalf, you must do two things

ユーザに代わってAPIにアクセスするためにアクセストークンを受け取るには、あなたは二つのことをする必要があります。

  • 承認用のURLにユーザを誘導してください。もしユーザがログインしていなかったら、ログインするかどうかを確認します。さらにどちらにせよ、instagram で所有してるデータにあなたのアプリケーションをアクセスさせたいかどうかをユーザに確認します。
  • そのあとサーバは、ふたつのうちから選んだあなたの方法でユーザをリダイレクトします。
    1. 一つ目は(アプリケーションの大半に推奨している)サーバーサイドフローとして知られる流れです。あなたがコードという値とともに指定した URI にユーザをリダイレクトさせます。そうしたらコードをうけとり、アクセストークンを取得するための URL にコードを POST で送信することで、認証されたユーザ用のアクセストークンとコードを引き換えます。
    2. 二つ目は暗黙的な流れとして知られています。この流れではユーザをコード付きの URI にリダイレクトさせる代わりに、アクセストークンを URL のフラグメント(#) の値としてアクセストークンを含めます。この手法だとサーバの要素のないアプリケーションが簡単にアクセストークンを受け取ることができます。

In order to receive an access_token to access the API on a user's behalf, you must do two things:

  • Direct the user to our authorization url. If the user is not logged in, they will be asked to login. Either way, they will the be asked if they'd like to give your application access to his or her Instagram data.
  • The server will then redirect the user in one of two ways that you choose:
    1. The first flow (which we recommend for the majority of applications) is known as the server-side flow. It involves us redirecting the user to a URI of your choice with a code parameter in the URL. You then take this code, and exchange it for an access_token for the authenticated user by POSTing the code to our access_token url.
    2. The second flow is known as the implicit flow. In this flow, instead of redirecting to a URI with a code, we include the access_token as a fragment (#) in the URL. This method allows applications without any server-component to receive an access_token with ease.

今回使用するのは、2つ目の暗黙的なフローです。

クライアント側での(暗黙的な)認証

ステップ1: 承認用のURLにユーザを誘導してください。

https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token

ステップ2: URL のフラグメントを通してアクセストークンを受け取ってください。

いったんユーザが認証し、あなたのアプリケーションを承認した後、アクセストークンを URL のフラグメントに含めたリダイレクト用の URI にユーザをリダイレクトします。

https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token

ただ単純に URL のフラグメントからアクセストークンを取得すればいいだけです。もしユーザがアプリケーションを承認しなかった場合、あなたは同じエラー明示的な流れとして受け取ることになります。

Once the user has authenticated and then authorized your application, we'll redirect them to your redirect_uri with the access_token in the url fragment. It'll look like so:

http://your-redirect-uri#access_token=ACCESS-TOKEN

Simply grab the access_token off the URL fragment and you're good to go. If the user chooses not to authorize your application, you'll receive the same error response as in the explicit flow

まとめ

要約すると、「ユーザを承認用の URL に飛ばせば、instagram 側で認証と承認をしたあとに結果を URL で返すので、URL から情報を取得してください。」ということになります。