実現したいこと
定期的にLINE広告のレポートを取得し、スプレットシートに反映させたい。
前提
https://teratail.com/questions/jh62abgfnivhpm こちらの内容を参考に、
コードを作成しました。
コード作成直後は正しく動いていたのですが、
コードを繰り返し実行していたところ、数時間後にはエラーを返すようになり、原因を特定できずにいます。
発生している問題・エラーメッセージ
{"errors":[{"reason":"Unauthorized","message":"JWT signature does not match locally computed signature. JWT validity cannot be asserted and should not be trusted."}]}
エラーコードは、401 を返しています。
該当のソースコード
function importAdsReport() { const today = new Date(); const access_key = "access_key"; const secretKey = "secretKey"; const accountId = "accountId"; const header = `{"alg":"HS256","kid":"${access_key}","typ":"text/plain"}`; const hex_digest = sha256(""); const content_type = ""; const payload_date = Utilities.formatDate(today, 'GMT', 'yyyyMMdd'); const canonical_url = `/api/v3/adaccounts/${accountId}/campaigns`; const payload = `${hex_digest}\n${content_type}\n${payload_date}\n${canonical_url}`; const inputValue = `${base64(header)}.${base64(payload)}`; const signature = hmacSha256(inputValue, secretKey); const calculatedSignature = `${inputValue}.${signature}`; const request_headers = { "Date": Utilities.formatDate(today, 'GMT', 'E, dd MMM yyyy HH:mm:ss z'), "Authorization": `Bearer ${calculatedSignature}` } const options = { "method": "GET", "headers": request_headers, "muteHttpExceptions": true }; const response = UrlFetchApp.fetch('https://ads.line.me' + canonical_url, options); console.log(response.getResponseCode()); console.log(response.getContentText()); } /** * base64エンコード * @param {string} input - エンコードしたい文字列 * @return {string} - base64エンコードされた文字列 */ function base64(input) { return Utilities.base64Encode(input, Utilities.Charset.UTF_8) } /** * HMAC SHA 256でハッシュ化 * @param {string} text - ハッシュ化する文字列 * @param {string} key - ハッシュ化するシークレットキー * @return {string} - ハッシュ値 */ function hmacSha256(text, key) { const rowHash = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, text, key); return Utilities.base64Encode(rowHash); }; /** * SHA 256でハッシュ化 * @param {string} input - ハッシュ化する文字列 * @return {string} - ハッシュ値 */ function sha256(input) { const rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, input, Utilities.Charset.UTF_8); let txtHash = ''; for (i = 0; i < rawHash.length; i++) { let hashVal = rawHash[i]; if (hashVal < 0) { hashVal += 256; } if (hashVal.toString(16).length == 1) { txtHash += '0'; } txtHash += hashVal.toString(16); } return txtHash }
試したこと・その他補足
・access_key secretKey accountId の再入力
・念の為時間をおいて再度トライを試みましたが、状況は変わりませんでした。
また、以下を参考にした一連のコードは問題なく動いております。
https://teratail.com/questions/7te4auy5i4q1m6
よって、access_key secretKey accountId に誤りはないのかなと考えています。
大量アクセスによる制限かとも考えたのですが、エラーコードは401を返しています。
一方で、繰り返しコードを走らせていた最中で突然エラーが発生したため、困惑している状況です。
もしおわかりの方がいらっしゃればお知恵をお貸しいただけますと幸いです。

あなたの回答
tips
プレビュー