LoginSignup
2
1

More than 5 years have passed since last update.

Azure Invoice APIで請求書取得

Last updated at Posted at 2017-04-27

本日Azureにログインしたところ、以下のポップアップがでてきました。
image

Billing Readerという課金情報が取得できるとロールとInvoice APIが使えるということらしい。
毎月請求書をアカウントセンターというところから取得しているのですが、これで自動的に取れるかもということで
試してみました。(メールで請求書送ってくれるけど、クレジットカードの請求と合わせて処理しているので、1個前のやつが欲しい)
以下APIドキュメント
https://docs.microsoft.com/en-us/rest/api/billing/invoices
list, getができるみたいです。

有効化

デフォではこの機能が無効になっているので、有効化する。
image
image

テスト

とりあえず最新のを取得できるかテスト。

billing readerロールでアカウントを作成。

az ad sp create-for-rbac -n billing --role "billing reader"

アクセストークンを取得

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=client_credentials&resource=https://management.core.windows.net/&client_id=$appid&client_secret=$secret" https://login.microsoftonline.com/$tenant/oauth2/token?api-version=1.0

請求書最新版ダウンロード

 curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $token" https://management.azure.com/subscriptions/$subscriptionid/providers/Microsoft.Billing/invoices/latest\?api-version\=2017-02-27-preview

すると以下のようなレスポンスが。

{
  "id": "/subscriptions/さぶすくid/providers/Microsoft.Billing/invoices/201705-217671190373201",
  "type": "Microsoft.Billing/invoices",
  "name": "201705-217671190373201",
  "properties": {
    "downloadUrl": {
      "expiryTime": "2017-04-27T08:39:57Z",
      "url": "https://billinginsightsstore08.blob.core.windows.net/invoices/さぶすくid-217671190373201.pdf?sv=2014-02-14&sr=b&sig=NEK1Qfvgg85Xds878h3Dh34dQ%2B7gqXMvXgQ0s0nsSI%3D&se=2017-04-27T08%3A39%3A57Z&sp=r"
    },
    "invoicePeriodEndDate": "2017-03-18",
    "invoicePeriodStartDate": "2017-02-19"
  }
}

sas付きのblobのurlがかえってくるので、ここからpdfがダウンロードできます(一応sasちょっと変えてます)。

これでAzure Functionで毎月請求書取ってきて、blobかどっかのストレージに格納するか、Logic Appsからメールで送ればはかどりそうですね。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1