目次
この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。
1.どうして、EXCEL(VBA)を使うのか
実は、下図のウェブサーバを作れる自信がないのと
出力結果を直接EXCELで加工してしまいたい!と思ったのがキッカケです。


1.1.作成イメージ


1.2.認可コード要求~アクセストークン取得までのイメージ
上記、項1.のウェブサーバを使用している部分を、VBAで実装して見よう。って事です。


2. 実際にどんなVBAコードを使えばよいか(一部)
2.1.BOXから認可コードを取得
InternetExplorer
オブジェクトのNavigate
メソッドを使って、認可コード要求をしてみます。
■以下はサンプルです。
'-- IE を起動する
Dim objBrowser As Object
Set objBrowser = CreateObject("InternetExplorer.Application")
With objBrowser
'-- IEを表示
.Visible = True
'-- 表示位置を設定
.Top = 100 'Y位置(上下)
.Left = 200 'X位置(左右)
.Width = 650 'IEウィンドウの幅
.Height = 800 'IEウィンドウの高さ
'-- 認証画面を表示
.Navigate "https://account.box.com/api/oauth2/authorize?response_type=code&client_id=" & CLIENT_ID"
'-- IEが表示されるまで待機
Do While .Busy = True Or .readyState <> 4
DoEvents
Loop
End With
2.2.アクセストークンを取得
次は、ブラウザを表示する必要がないため、msxml2.xmlhttp
オブジェクトを使って、アクセストークン要求をしてみます。
■以下はサンプルです。
'-- Access Token の取得 (HTTP POST 要求)
Dim objXHttp1 As Object
Set objXHttp1 = CreateObject("msxml2.xmlhttp")
objXHttp1.Open "POST", "https://api.box.com/oauth2/token", False
objXHttp1.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXHttp1.send "grant_type=authorization_code&code=" & strCode & "&client_id=" & CLIENT_ID & "&client_secret=" & CLIENT_SECRET
2.3.JSONをパースする
EXCEL(VBA)でJSONを扱うには、VBA-JSONをダウンロードして使用するのが、オススメです。
※レスポンス結果はUnicodeに変換しないと、文字化けしてしまいます。
■文字コード変換のサンプル
Dim strResult As String
strResult = StrConv(objXHttp2.responsebody, vbUnicode)
■VBA-JSONを使って、パースするサンプル
Dim Folders As Object
Set Folders = JsonConverter.ParseJson(strResult)
3.サンプル
サンプルを作ってみましたので、試してみたい方はご自由にどうぞ
サンプルEXCELをダウンロード(2019.07.29リンク誤り修正)
※VBAのコードは、現時点では非公開
です。
※2017.10.18時点で、実践サンプルも公開中 (その4)、(その5) の記事を確認してみて。


EXCELを表示したときの初期画面


今回は、Get Folder InfoとGet Folder Itemsを実行してみて、EXCEL上に出力してみました。
まとめ
次回は、EXCEL(VBA)から、グループ情報をEXCELに整形して出力してみたいと思います。
管理者向けの管理コンソールから取得できる情報ですが、CSVダウンロード等の機能が現時点(2017.6.23)では無かったので作成してみようと思います。
Boxの販売パートナーもしてます。


お問い合わせは、こちら へ
BOXを使ったのアプリケーション開発も可能です。気軽にご連絡お待ちしております。