GAS エラー「Exception: You do not have permission to call UrlFetchApp.fetch. Required permissions: https://www.googleapis.com/auth/script.external_request」が発生した場合の対処法

GAS エラー「Exception: You do not have permission to call UrlFetchApp.fetch. Required permissions: https://www.googleapis.com/auth/script.external_request」が発生した場合の対処法

GAS(Google Apps Script)で、エラー「Exception: You do not have permission to call UrlFetchApp.fetch. Required permissions: https://www.googleapis.com/auth/script.external_request」が発生した場合の対処法を記述してます。

環境

  • OS windows11 home
  • ブラウザ chrome 120.0.6099.71

エラー全文

以下のchatに通知を送るプログラムで発生

const postGoogleChat = (message) => {
  
  let webhookUrl = "https://chat.googleapis.com/v1/spaces/xxxxx";
 
  message = {
    'text': message
  };
  
  let options = {
    'payload': JSON.stringify(message),
    'myamethod': 'POST',
    'contentType': 'application/json'
  };
  
  let response = UrlFetchApp.fetch(webhookUrl, options);
}

エラーメッセージ

エラー	
Exception: You do not have permission to call UrlFetchApp.fetch. Required permissions: https://www.googleapis.com/auth/script.external_request

原因

appsscript.jsonを編集していたため、oauthScopesに「”https://www.googleapis.com/auth/script.external_request”」を追加する必要がありました。

対処法

「”https://www.googleapis.com/auth/script.external_request”」を追加する

 "oauthScopes": [
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/contacts",
    ...,
    ...,
    ...
  ],