我试图使用Google脚本在Clockify中启动一个计时器。脚本不会给出错误(例如400坏请求),但是永远不会启动计时器。
https://clockify.me/developers-api#operation--v1-workspaces--workspaceId--time-entries-post的API文档
function StartClockifyTimer() {
// Starts a timer based on the current sheet
// Step 1: Find PID
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var FileNo = sheet.getSheetName();
var PIDheaders = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
//var PIDpayload = JSON.stringify({'name' : FileNo});
var PIDoptions = {
'muteHttpExceptions' : true,
'method' : 'get',
'headers' : PIDheaders,
//'params' : PIDpayload
};
var PID = UrlFetchApp.getRequest('https://api.clockify.me/api/v1/workspaces/[wid]/projects/', PIDoptions);
for(i in PID) {
Logger.log(i + ": " + PID[i]);
}
//Step 2: Use PID to start timer
timezone = "GMT+" + new Date().getTimezoneOffset()/60;
var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd'T'HH:mm:ss'Z'");
var headers = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
var payload = JSON.stringify({'start' : date, 'projectId' : PID});
var clockifyoptions = {
'muteHttpExceptions' : true,
'method' : 'post',
'headers' : headers,
'payload' : payload
};
var r = UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[wid]/time-entries/', clockifyoptions);
Logger.log(r);
}-编辑--自从发帖以来,感谢收到的建议,我在这个过程中增加了一个新的步骤,通过名字找到PID。但是,我无法使GET请求正确工作。我需要像在FileNo中描述的那样,通过https://clockify.me/developers-api#operation--v1-workspaces--workspaceId--projects-get查询'name‘,但是我无法获得get请求。即使以上带有注释部分的代码也会返回:
19-08-01 11:37:46:024 MDT getAs: function () {/* */} 19-08-01 11:37:46:025 MDT getHeaders:函数getHeaders() {/* */} 19-08-01 11:37:46:025 MDT getContentText:函数getContentText() {/* */} 19-08-01 11:37:46:026 MDT getContent:函数getContent() {/* */} 19-08-01 11:37:46:026 MDT getResponseCode:函数getResponseCode() {/* */} 19-08-01 11:37:46:027 MDT getAllHeaders:函数getAllHeaders() {/* */} 19-08-01 11:37:46:027 MDT toString:函数toString() {/* */} 19-08-01 11:37:46:028 MDT getBlob:函数getBlob() {/* */}
从那时起,我已经确认在步骤2中使用硬编码的ProjectID是有效的,所以我需要帮助的实际上是步骤1-找到PID。有人能帮我格式化GET请求吗?
谢谢一吨。
发布于 2019-08-01 18:18:29
明白了。将代码留给任何试图访问Clockify的API的人。
function StartClockifyTimer() {
// Starts a timer based on the current sheet
// Step 1: Find PID
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var FileNo = sheet.getSheetName();
var url = 'https://api.clockify.me/api/v1/workspaces/[workspaceid]/projects?name='+FileNo
const header = {
"headers": {
"X-Api-Key" : "[apikey]",
"content-type" : "application/json",
"Accept" : "*/*"
}
};
var response = UrlFetchApp.fetch(url, header)
var json = response.getContentText();
var data = JSON.parse(json);
var PID = data[0]["id"];
Logger.log(PID);
//Step 2: Use PID to start timer
timezone = "GMT+" + new Date().getTimezoneOffset();
var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd'T'HH:mm:ss'Z'");
var headers = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
var payload = JSON.stringify({'start' : date, 'projectId' : PID});
var clockifyoptions = {
'muteHttpExceptions' : true,
'method' : 'post',
'headers' : headers,
'payload' : payload
};
UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceid]/time-entries/', clockifyoptions);
}发布于 2019-08-01 16:01:30
你可以试着做几件事情,看看到底出了什么问题:
(1)替换
UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceredacted]/time-entries/', clockifyoptions);
Logger.log(clockifyoptions);有了这个
var r = UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceredacted]/time-entries/', clockifyoptions);
Logger.log(r);这样,日志将显示来自API的响应,您可以使用该响应进行调试。
(2)有效载荷中的projectId可能是错误的。
假设projectID是活动工作表的名称,请尝试替换
var FileNo = ss.getSheetName();使用
var FileNo = sheet.getSheetName();https://stackoverflow.com/questions/57312336
复制相似问题