ロジレス(LOGILESS)のWEB-APIを利用するまでの手続きを記録します。
1.認証・認可
ロジレスにアプリケーション登録を行い、アクセストークン(アクセス用パスワード)を取得する。
https://app2.logiless.com/developer/documents/authentication
【詳細手順】
(1)ロジレスにサインアップします。
(2)アプリケーション一覧から「 新しいアプリケーションを登録 」 を行います。
(3)アプリケーションの登録が完了すると、クライアントIDとクライアントシークレットが取得できます。
(4)許可コードを取得します。
・一時的なWEBサーバをポート番号33333で起動しておく
(今回は簡単Webサーバというソフトを使いました)
・ブラウザで以下のURLを入力して飛ぶ(client_idは前段で取得した値をセットする)
https://app2.logiless.com/oauth/v2/auth?client_id=xxxxx&response_type=code&redirect_uri=http://localhost:33333
・http://localhost:33333に許可コードが表示されるのでひかえておく
(5)認可コードを使って、アクセストークンとリフレッシュトークンを入手する。
・一時的なWEBサーバをポート番号33333で起動しておく
・ブラウザで以下のURLを入力して飛ぶ(client_id、client_secret、codeは前段で取得した値をセットする)
https://app2.logiless.com/oauth2/token?client_id=xxxx&client_secret=xxxx&code=xxxx&grant_type=authorization_code&redirect_uri=http://localhost:33333
・http://localhost:33333にアクセストークン、リフレッシュトークンが表示されるのでひかえておく
(4)で許可コードを取得後は、30秒以内に(5)を完了させる必要があります。
【このアクセストークンを使って、今後作成するツールからロジレスへ認証します。】
2.PGでリクエスト用のHTMLを投げる
(例)マーチャントID=9999(仮)に対して受注一覧表を要求する。
https://app2.logiless.com/api/v1/merchant/9999/sales_orders"
※リクエストのヘッダにアクセストークンを付与して認証させます。
レスポンスがJSON形式という文字列で帰ってくるので、それを成型して利用します。
実際のソースは、以下の通りです。
Imports Newtonsoft.Json 'nugetパッケージ マネージャーコンソールでInstall-Package Newtonsoft.Json
Imports System.Net
Imports System.IO
Imports System.Text
Dim jsonStr As String
Dim accessToken As String = "xxxx" ← 実際はもっと長くて複雑です
Dim url As String = "https://app2.logiless.com/api/v1/merchant/9999/sales_orders"
url = url + "?limit=100&page=1"
url = url + "&ordered_at_from=2021-01-25 00:00:00&ordered_at_to=2021-01-25 23:59:59"
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 'TLS1.2を有効にする
Using wc As New WebClient
wc.Headers.Add("ContentType", "application/json")
wc.Headers.Add("Authorization", "Bearer " + accessToken)
Using st As Stream = wc.OpenRead(url)
Using sr As StreamReader = New StreamReader(st, Encoding.UTF8)
jsonStr = sr.ReadToEnd()
End Using
End Using
End Using
'Json文字列をJson形式データに復元する
Dim jsonObj As Object = JsonConvert.DeserializeObject(jsonStr)
'総件数を確認する。'1度に最大100件までしか取れないので、それ以上必要な場合は、ページを変えて再リクエストを投げる。
Dim res_cnt As Long = jsonObj("total_count")
'取得したレスポンスデータの中身を確認する
For i As Integer = 0 To res_cnt - 1
Debug.Print(jsonObj("current_page"))
Debug.Print(jsonObj("limit"))
Debug.Print(jsonObj("total_count"))
Debug.Print(jsonObj("data")(i)("id"))
Debug.Print(jsonObj("data")(i)("code"))
Debug.Print(jsonObj("data")(i)("buyer_name1"))
Debug.Print(jsonObj("data")(i)("buyer_name2"))
Debug.Print(jsonObj("data")(i)("lines")(0)("id"))
Debug.Print(jsonObj("data")(i)("lines")(0)("article_name"))
Debug.Print(jsonObj("data")(i)("outbound_deliveries")(0)("id"))
Debug.Print(jsonObj("data")(i)("outbound_deliveries")(0)("code"))
Debug.Print(jsonObj("data")(i)("outbound_deliveries")(0)("delivery_method"))
Debug.Print(jsonObj("data")(i)("outbound_deliveries")(0)("lines")(0)("id"))
Debug.Print(jsonObj("data")(i)("outbound_deliveries")(0)("lines")(0)("article_name"))
Debug.Print(jsonObj("data")(i)("outbound_deliveries")(0)("warehouse"))
Debug.Print(jsonObj("data")(i)("outbound_deliveries")(0)("store"))
Debug.Print(jsonObj("data")(i)("store")("id"))
Debug.Print(jsonObj("data")(i)("store")("name"))
Next