# マネーフォワードAPIを使う ###### tags: `マネーフォワード` 1. マネーフォワードにアクセスして、請求書画面に行く 2. 右上のマークから開発者用をクリックするとOAuthアプリを新規登録する画面にいく 3. 下記情報を入力して実行 - 名前 - Redirect URI - 認証に使うリダイレクトURIを使う - Scope - readまたはwriteを選択する https://invoice.moneyforward.com/oauth/authorize?client_id=[CLIENT_ID]&redirect_uri=[REDIRECT_URL]&response_type=code&scope=[SCOPE] - Client ID - bkL-ja4dX0QGH3vb1StOorrItZijFSPHGau4nwncfTw - Client Secret - S6DMhtjTVvWVw54yo_IkSwIgtDk3Gvn4HbDmyuzodX0 - スコープ - write - Redirect URI - https://form-sales-bot-staging.herokuapp.com/ 認証に成功したらURIに設定したURLに飛ぶ codeが付与されているのでそれをコピーしておく ``` https://form-sales-bot-staging.herokuapp.com/signIn?code=tSbSFjE1lRa2NrhbVllEKqGVkijTAOswjjWrkckAOZY ``` 次にcurlでアクセストークンを取得する ここでは`CLIENT_ID`と`CLIENT_SECRET`、`REDIRECT_URL`と先ほど取得したCODE(認証コード)を使う ```bash: $ curl -d client_id=[CLIENT_ID] -d client_secret=[CLIENT_SECRET] -d redirect_uri=[REDIRECT_URL] -d grant_type=authorization_code -d code=[認証コード] -X POST https://invoice.moneyforward.com/oauth/token // 下は使うやつ $ curl -d client_id=bkL-ja4dX0QGH3vb1StOorrItZijFSPHGau4nwncfTw -d client_secret=S6DMhtjTVvWVw54yo_IkSwIgtDk3Gvn4HbDmyuzodX0 -d redirect_uri=https://form-sales-bot-staging.herokuapp.com -d grant_type=authorization_code -d code=tSbSFjE1lRa2NrhbVllEKqGVkijTAOswjjWrkckAOZY -X POST https://invoice.moneyforward.com/oauth/token ``` 取得したアクセストークンが下記 アクセストークンの期限は30日 ```bash: { "access_token":"zqzJMZMpZMnSYbDLM5bu4s3UWkDKYfh3IEuBVPvszuc", "token_type":"Bearer", "expires_in":2591999, "refresh_token":"ijBU-o2vQGfCooGCQ3_TFxXEU46AElOdlVMm5oCTjDc", "scope":"write", "created_at":1624010395}% ``` 再発行コマンド ```bash: $ curl -d client_id=[CLIENT_ID] -d client_secret=[CLIENT_SECRET] -d grant_type=refresh_token -d refresh_token=[REFRESH_TOKEN] -X POST https://invoice.moneyforward.com/oauth/token ``` ## 請求書を作成する #### まずは取引先の登録 ```bash: curl -i -H "Authorization: BEARER [ACCESS_TOKEN]" -H "Content-Type: application/json" -d '{ "partner" : { "name" : "サンプル取引先" }}' -X POST https://invoice.moneyforward.com/api/v1/partners // 下記は使ったやつ curl -i -H "Authorization: BEARER zqzJMZMpZMnSYbDLM5bu4s3UWkDKYfh3IEuBVPvszuc" -H "Content-Type: application/json" -d '{ "partner" : { "name" : "サンプル取引先" }}' -X POST https://invoice.moneyforward.com/api/v1/partners ``` これがレスポンス ```bash: { "id":"3RjMc1Fo5HZdI7-_jB6umA", "code":null, "name":"サンプル取引先", "name_kana":null, "name_suffix":"", "memo":null, "created_at":"2021-06-18T19:04:29.000+09:00", "updated_at":"2021-06-18T19:04:29.000+09:00", "departments": [ { "id":"DV_VIsV-02qk_3WYAdMGEQ", "zip":null, "tel":null, "prefecture":"東京都", "address1":null, "address2":null, "person_name":null, "person_title":null, "name":null, "email":null, "cc_emails":null, "created_at":"2021-06-18T19:04:30.000+09:00", "updated_at":"2021-06-18T19:04:30.000+09:00" } ] }% ``` #### そして請求書作成 ```bash: curl -i -H "Authorization: BEARER [ACCESS_TOKEN]" -H "Content-Type: application/json" -d '{ "billing" : { "department_id" : "[DEPARTMENT_ID]" }}' -X POST https://invoice.moneyforward.com/api/v1/billings // 下記は使ったやつ curl -i -H "Authorization: BEARER zqzJMZMpZMnSYbDLM5bu4s3UWkDKYfh3IEuBVPvszuc" -H "Content-Type: application/json" -d '{ "billing" : { "department_id" : "DV_VIsV-02qk_3WYAdMGEQ" }}' -X POST https://invoice.moneyforward.com/api/v1/billings ``` > 下記がレスポンス ```bash: { "id":"0eVxO2I2PbA3x9wNU5qFug", "pdf_url":"https://invoice.moneyforward.com/api/v1/billings/0eVxO2I2PbA3x9wNU5qFug.pdf", "operator_id":"9OimYYJiLbh8ni8zvIdU7A", "partner_id":"3RjMc1Fo5HZdI7-_jB6umA", "department_id":"DV_VIsV-02qk_3WYAdMGEQ", "member_id":"c6f7fTZ_BfttQZ2fBg2eTQ", "member_name":null, "partner_name":"サンプル取引先", "partner_name_suffix":"", "partner_detail":"", "office_name":"坂本雅也", "office_detail":"", "title":null, "excise_price":"0.0", "excise_price_of_five_percent":"0.0", "excise_price_of_eight_percent":"0.0", "excise_price_of_eight_percent_as_reduced_tax_rate":"0.0", "excise_price_of_ten_percent":"0.0", "deduct_price":"0.0","subtotal":"0.0", "subtotal_of_untaxable_excise":"0.0", "subtotal_of_non_taxable_excise":"0.0", "subtotal_of_tax_exemption_excise":"0.0", "subtotal_of_five_percent_excise":"0.0", "subtotal_of_eight_percent_excise":"0.0", "subtotal_of_eight_percent_as_reduced_tax_rate_excise":"0.0", "subtotal_of_ten_percent_excise":"0.0", "memo":null, "payment_condition":"楽天銀行 \n支店 ラテン支店\n支店番号 223\n普通口座 2444641", "total_price":"0.0", "billing_date":"2021-06-18", "due_date":"2021-07-31", "sales_date":"2021-05-31", "billing_number":"4", "note":"誠に恐れ入りますが、振込手数料は貴社にてご負担ください。", "document_name":"請求書", "created_at":"2021-06-18T19:07:14.000+09:00", "updated_at":"2021-06-18T19:07:14.000+09:00", "tags":[], "items":[ { "id":"B5n5FUBfUslD4mj2RWmV6w", "name":null, "code":null, "detail":null, "unit_price":null, "unit":null, "quantity":null, "price":null, "display_order":0, "excise":true, "deduct":false, "created_at":"2021-06-18T19:07:14.000+09:00", "updated_at":"2021-06-18T19:07:14.000+09:00" } ], "status":{ "email":"未送信", "payment":"未設定", "posting":"未郵送", "download":"" } }% ```