Search code examples
javascriptgoogle-apps-scripturlfetchhttp-status-code-405clockify

UrlFetchApp Method does not work and gives 405 error in retrieving data from an API?


I am trying to get the Clockify detailed time report into Google Sheets. When I try to use the URL fetch method, it gives me an error:

Exception: Request failed for https://reports.api.clockify.me returned code 405. Truncated server response: {"code":405, "message": "HTTP 405 Method Not Allowed"} (use muteHttpExceptions option to examine full response)

Here is the documentation - https://docs.clockify.me/#tag/Time-Entry-Report/operation/generateDetailedReport

I am using this code:

function getTimeReport() {
  
  var body = {"dateRangeStart":"2023-10-05T00:00:00.000Z","dateRangeEnd":"2023-05-05T23:59:59.000Z","detailedFilter":{"page":1,"pageSize":1000,"options":{"totals":"EXCLUDE"}}}
  
  var headers = {
    "headers": {
      "x-Api-Key": "XXX",
      "Content-type": "application/json",
      "Application": "Custom",
      "muteHttpExceptions": "true",
      "Method": "POST",
      "payload": body
    }
  };
  
  var workspaceId = "XXX";
  
  var url = `https://reports.api.clockify.me/v1/workspaces/${workspaceId}/reports/detailed`;
  Logger.log(url);
  var response = UrlFetchApp.fetch(url, headers);
  Logger.log(response);

I double-checked the workspace ID and API key both are correct. May I know where I am going wrong?


Solution

  • In your script, when the values of body and the value of x-Api-Key are valid values, how about the following modification?

    Modified script:

    function getTimeReport() {
      var body = { "dateRangeStart": "2023-10-05T00:00:00.000Z", "dateRangeEnd": "2023-05-05T23:59:59.000Z", "detailedFilter": { "page": 1, "pageSize": 1000, "options": { "totals": "EXCLUDE" } } };
    
      var options = {
        "headers": { "x-Api-Key": "XXX" },
        "muteHttpExceptions": "true",
        "Method": "POST",
        "payload": JSON.stringify(body),
        "contentType": "application/json",
      };
    
      var workspaceId = "XXX";
    
      var url = `https://reports.api.clockify.me/v1/workspaces/${workspaceId}/reports/detailed`;
      Logger.log(url);
      var response = UrlFetchApp.fetch(url, options);
      Logger.log(response);
    }
    
    • I thought that the request of this modified script is the same as the specification of the API of your provided URL.

    Reference: