Asana Events API 在 Apps Script 中始终返回无效的同步令牌。

huangapple go评论76阅读模式
英文:

Asana Events API always returning invalid sync token in Apps Script

问题

我尝试调用Asana事件API - https://developers.asana.com/reference/getevents

这个端点使用一个"Sync"令牌来跟踪您上次调用它的时间,每次调用都会返回一个新的同步令牌,然后可以将其存储并在下一次调用中使用,以仅检索自上次调用以来的事件。

如果您不提供同步令牌,API将返回一个"无效令牌"响应,并提供一个新的令牌供您开始使用。

然而,我似乎无法在初始的"无效令牌"调用之后继续,当我在以下调用中使用令牌时,它仍然返回"无效令牌"。

这是使用Google的Apps Script,它基本上只是一些内置的Google函数的JavaScript。

这是我的代码:

这个函数调用没有"同步"参数,使用muteHttpExceptions获取完整的响应文本,并将同步令牌设置为Apps Script中的属性(只是键/值对)。

var asanaBaseUrl = 'https://app.asana.com/api/1.0';

function getSyncToken() {
  var url = asanaBaseUrl + '/projects/' + asanaProject + '/events';

  var headers = {
    'Authorization': 'Bearer ' + asanaPAT
  }

  var options = {
    'method': 'GET',
    'headers': headers,
    'muteHttpExceptions': true
  }

  var r = UrlFetchApp.fetch(url, options)
  var data = JSON.parse(r.getContentText())

  var newSyncToken = data.sync
  userProps.setProperty('syncToken', newSyncToken)
}

这个函数使用属性服务来提取在前一个调用中设置的同步令牌,并使用同步参数进行新的调用。

function asanaEventCall() {
  var url = asanaBaseUrl + '/projects/' + asanaProject + '/events';
  var syncToken = userProps.getProperty('syncToken')

  var headers = {
    'Authorization': 'Bearer ' + asanaPAT
  }

  var options = {
    'method': 'GET',
    'headers': headers,
    'sync': syncToken,
    'muteHttpExceptions': true
  }
  var r = UrlFetchApp.fetch(url, options)
  var data = JSON.parse(r.getContentText())
  return data
}

第二个函数始终返回以下内容:

{sync=[SYNC TOKEN HERE], errors=[{message=同步令牌无效或太旧如果您正在尝试保持资源同步您现在必须获取此查询的完整数据集并在下一次同步中使用新的同步令牌}]}

我通过日志确认了令牌的正确设置、检索和发送。

我通过手动使用URL来确认上述流程有效,就像这样:

  • 转到https://app.asana.com/api/1.0/projects/[PROJECT GID]/events
  • 复制同步令牌
  • 转到https://app.asana.com/api/1.0/projects/[PROJECT GID]/events?sync=[SYNC TOKEN]
  • 事件正常显示

我寻找了其他答案,但没有找到任何有助的具体信息,并且在Asana论坛上提问,但几天没有得到答复。

我确信我在这里漏掉了一些非常明显的东西,但只是看不到它!

英文:

I am trying to call the Asana Events API - https://developers.asana.com/reference/getevents

This endpoint uses a 'Sync' token to track when you last called it, each call returns a new sync token which can then be stored and used for the next call to only retrieve events since the last call.

If you do not provide a sync token the API will return an 'invalid token' response and give you a new token to get started with.

However, I cannot seem to get it beyond the initial 'invalid token' call, when I used the token in the following call it still returns 'invalid token'

This is using Google's Apps Script, which is basically just JavaScript with some built-in Google functions

Here is my code:

This function makes a call without the ‘sync’ parameter, uses muteHttpExceptions to get the full response text, and sets the sync token as a property in Apps Script (just a key/value pair)

var asanaBaseUrl = 'https://app.asana.com/api/1.0'

function getSyncToken() {
  var url = asanaBaseUrl + '/projects/' + asanaProject + '/events'

  var headers = {
    'Authorization': 'Bearer ' + asanaPAT
  }

  var options = {
    'method': 'GET',
    'headers': headers,
    'muteHttpExceptions': true
  }

  var r = UrlFetchApp.fetch(url, options)
  var data = JSON.parse(r.getContentText())


  var newSyncToken = data.sync
  userProps.setProperty('syncToken', newSyncToken)

}

This function uses the properties service to pull the sync token set in the previous call and makes a new call with the sync parameter

function asanaEventCall() {
  var url = asanaBaseUrl + '/projects/' + asanaProject + '/events'
  var syncToken = userProps.getProperty('syncToken')

  var headers = {
    'Authorization': 'Bearer ' + asanaPAT
  }

  var options = {
    'method': 'GET',
    'headers': headers,
    'sync': syncToken,
    'muteHttpExceptions': true
  }
  var r = UrlFetchApp.fetch(url, options)
  var data = JSON.parse(r.getContentText())
  return data
}

The second function always returns the following

{sync=[SYNC TOKEN HERE], errors=[{message=Sync token invalid or too old. If you are attempting to keep resources in sync, you must fetch the full dataset for this query now and use the new sync token for the next sync.}]}

I’ve confirmed that the token is being set, retrieved, and sent correctly using the logs.

I’ve confirmed the flow above works by manually using the URL, like this:

I've looked for other answers but haven't found anything specific that helped, and I asked on the Asana forum but haven't got an answer in a few days.

I'm sure I'm missing something painfully obvious here but just can't see it!

答案1

得分: 1

从“前往https://app.asana.com/api/1.0/projects/[项目GID]/events?sync=[同步令牌]”到“我已经确认上面的流程可以通过手动使用URL来工作,就像这样:”的部分,在你的脚本中,似乎需要将“'sync': syncToken,”添加为查询参数。那么,如何修改你的asanaEventCall()如下?

修改后的脚本:

function asanaEventCall() {
  var syncToken = userProps.getProperty('syncToken');
  var url = asanaBaseUrl + '/projects/' + asanaProject + '/events' + '?sync=' + syncToken;
  var headers = {
    'Authorization': 'Bearer ' + asanaPAT
  };
  var options = {
    'method': 'GET',
    'headers': headers,
    'muteHttpExceptions': true
  };
  var r = UrlFetchApp.fetch(url, options);
  var data = JSON.parse(r.getContentText());
  return data;
}

注意:

  • 在这个修改中,假设你的asanaProjectasanaPATsyncToken的值是有效的。请注意这一点。
英文:

From Go to https://app.asana.com/api/1.0/projects/[PROJECT GID]/events?sync=[SYNC TOKEN] of I’ve confirmed the flow above works by manually using the URL, like this:, in your script, it seems that it is required to add 'sync': syncToken, as the query parameter. So, how about modifying your asanaEventCall() as follows?

Modified script:

function asanaEventCall() {
  var syncToken = userProps.getProperty('syncToken')
  var url = asanaBaseUrl + '/projects/' + asanaProject + '/events' + '?sync=' + syncToken;
  var headers = {
    'Authorization': 'Bearer ' + asanaPAT
  }
  var options = {
    'method': 'GET',
    'headers': headers,
    'muteHttpExceptions': true
  }
  var r = UrlFetchApp.fetch(url, options)
  var data = JSON.parse(r.getContentText())
  return data
}

Note:

  • In this modification, it supposes that your values of asanaProject, asanaPAT and syncToken are valid values. Please be careful about this.

huangapple
  • 本文由 发表于 2023年2月18日 12:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定