目次
この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。
今回は、前回(その1) で説明出来なかった部分の補足となっています。
1.認可コード要求・応答
1.1. 認可コード要求・応答
リファレンスサイト を参照して頂ければ解決しますが、ポイントになる部分のみを、抜き出して書いておきます。
前回(その1) で説明をした下図の遷移に関しての補足となります。


以下ののURLに対して、パラメタを設定して送信することで、認可コードを得る事ができます。
■URLhttps://account.box.com/api/oauth2/authorize
■パラメタresponse_type
「code」固定です。client_id
認証を要求するアプリケーションのクライアントIDredirect_uri
認証が完了した場合に、ブラウザにリダイレクトされるURLstate
認証が完了した場合のリダイレクトURLに同じ文字列が返ります。攻撃からの保護のため、リクエスト時にランダムなハッシュ値を格納し、応答時に、同じ値が返却されてくるかチェックに使用すると良いでしょう。
■リクエスト例
https://account.box.com/api/oauth2/authorize?response_type=code&client_id=<MY_CLIENT_ID>&redirect_uri=<MY_REDIRECT_URL>&state=<MY_SECURITY_TOKEN>
※client_id
と、redirect_uri
は、下図の登録したアプリケーションの構成情報から取得します。


実行すると、下図のBoxの認証画面が表示され、ログインすることで登録したアプリケーションでBoxにアクセス許可を求めてきます。
「Boxへのアクセスを許可
」を選択することで、認証コード
を得ることができます。


1.2.認可コード応答
認可コードは、以下のパラメタに格納されています。
code=<認可コード>
2.アクセストークン要求・応答
2.1.アクセストークン要求


以下ののURLに対して、パラメタを設定して送信することで、アクセストークンを得る事ができます。
■URLhttps://api.box.com/oauth2/token
■パラメタgrant_type
「authorization_code」固定です。code
認可コード応答で取得した認可コードclient_id
認証を要求するアプリケーションのクライアントIDclient_secret
認証を要求するアプリケーションのクライアント秘密コード
■リクエスト例
curl https://api.box.com/oauth2/token \
-d 'grant_type=authorization_code' \
-d 'code=<MY_AUTH_CODE>' \
-d 'client_id=<MY_CLIENT_ID>' \
-d 'client_secret=<MY_CLIENT_SECRET>' \
-X POST
※client_id
と、client_secret
は、下図の登録したアプリケーションの構成情報から取得できます。


実行すると、アクセストークン
を得ることができます。
2.2.アクセストークン応答
アクセストークンは、JSON型の応答 access_token
に含まれています。
{
"access_token": "T9cE5asGnuyYCCqIZFoWjFHvNbvVqHjl",
"expires_in": 3600,
"restricted_to": [],
"token_type": "bearer",
"refresh_token": "J7rxTiWOHMoSC1isKZKBZWizoRXjkQzig5C6jFgCVJ9bUnsUfGMinKBDLZWP9BgR"
}
3.Boxファイル情報取得(アクセス例)
以下のように、リクエストヘッダに Authorization: Bearer YOUR_ACCESS_TOKEN
をセットして
送信することで、各種操作が可能となります。
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
https://api.box.com/2.0/files/123456789
まとめ
次回は、BOXのAPIを使って、EXCEL(VBA)から呼び出して見たいと思います。