php - 如何授权 cron 作业 php 文件访问 Xero?

标签 php oauth-2.0 xero-api

我正在迁移到 Xero,并希望使用 cron 作业将发票流程设置为每月在特定时间运行一次,我可以启动 cron 作业,并且我已经设置了一个基于 https://github.com/XeroAPI/xero-php-oauth2-app 的 php 页面。我可以手动运行它并且运行完美。

我也用过https://github.com/XeroAPI/xoauth检索 token 并将它们存储在钥匙串(keychain)中,我可以看到它们在那里。

我对 xoauth 所说的“将 access_token、id_token 和 refresh_token 通过管道传输到 stdout,以便您可以在脚本工作流程中使用它们”感到有点迷失

我希望有人做过类似的事情,可以为我指明正确的方向,或者更好地给我一个例子,因为我在网上找不到。

我假设我缺少两个传输 token 值的示例之间的链接。

当 cron 运行时,我收到以下错误

“ fatal error :未捕获 BadMethodCallException:未传递所需参数:/Applications/MAMP/htdocs/vendor/league/oauth2-client/src/Tool/RequiredParameterTrait.php:35 中的“refresh_token”

这并不令人意外,因为据我所知,我没有给它一个刷新 token 。

我在 Mac 上使用 localhost 作为开发环境。

我从更有经验的开发人员那里看到了许多与此相关的问题,但没有答案。

谢谢戈登

最佳答案

谢谢你的提问。我们已经得到了很多这个,所以我用它作为 XeroAPI community-corner 视频的基础,我很快就会在这里分享该视频,该视频逐步介绍从 xoauth 获取访问/刷新 token 、进行 api 调用、并刷新以获得新的 token 集。

回答

您想要做的是在使用 xoauth 生成访问 token 之后 repo 。在您的 PHP 脚本中 - 插入 access_token 和 xero-tenant-id (as 2 headers in your api call) .

Authorization: "Bearer " + access_token
xero-tenant-id: tenantId

确保 API 调用返回您的数据。然后在脚本中创建一个函数,在将来的 API 调用之前执行以下操作

  1. 刷新新的 token_set
  2. 将新的 token_set 保存到数据库或静态文件
  3. 使用该 token_set“access_token”进行 Invoice API 调用
  4. 至少每 60 天重复一次步骤 (1-3)

NOTE: you will need some kind of persistence to store the continually refreshed token_set.

希望这能为您澄清。我会尽快发回视频以进行深入的演练。

OAuth2.0背景:

Essentially our move to simplify and standardize our API authentication came with some challenges in how to setup longstanding API connections for use cases that didn’t need to onboard an increasing number of new users. For instance, a lot of small businesses and accounting firms setup custom processes to batch import/export invoices.

The use case often did not have the need for an application user interface, so standing one up just to get a valid access token was a lot of extra work if the integration only needed to connect to single ‘admin’ type user for a specific Xero Organisation.

关于php - 如何授权 cron 作业 php 文件访问 Xero?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61161791/

相关文章:

PHP:获取空白页

ruby-on-rails - 使用 React 前端和 Rails 后端的带有 google calendar api 的客户端 OAuth

java - Xero 配置中 config.json 的路径

shopify - 按订单号检索 "Shopify Payments"费用以与会计 + 银行存款进行对账?

php - 如何在子文件夹中设置索引页

php - 在 SQL Server 中将 Varchar 值转换为整数/小数值

ruby - OAuth2 与 intridea ruby gem

spring-boot - 包含的重定向 URI 无效

java - 在 Maven Java 应用程序中使用 xero API 时,如何从 Asset 中检索 AssetType?

PHP - 在 mysql 错误上创建文本文件