如何使用 wp_remote_post() 设置 Xero 授权 Header

How to Set Xero Authorization Header Using wp_remote_post()

我正在尝试使用 Xero API 在我的 WordPress 网站上发送发票电子邮件。但我不确定如何设置授权 header 我尝试了以下操作:

$args = array(
    'headers' => array(
        'Content-Type: application/x-www-form-urlencoded',
        'Authorization' => 'Basic ' . base64_encode('myClientID' . ':' . 'myClientSecret')
    ),
);
$response = wp_remote_post('https://api.xero.com/api.xro/2.0/Invoices/2dfa4120-1fd2-4e67-927e-c16ac821226c/Email', $args);

print_r($response);

这给我一个 404 unauthorized 的响应。有什么我遗漏或做错了什么吗?

授权 header 实际上需要设置有效的 access_token,而不是 id / secret 组合。您可以在此处阅读有关获取令牌所需的代码流的更多信息:https://developer.xero.com/documentation/oauth2/auth-flow

If you familiar with PHP you can look through code in the PHP SDK sample app here: https://github.com/XeroAPI/xero-php-oauth2-app


您的目标是将该发票电子邮件发送给动态组织,还是仅从您自己的个人组织发送发票?

幸运的是(或不幸的是)明年初我们将有这个嵌入式选项 access_token - 但如果您想在此期间进行设置,则需要从用户流中生成访问令牌,然后设置后端机制以在使用前刷新它,我在这个 youtube 视频中对此进行了解释:https://www.youtube.com/watch?v=Zcf_64yreVI

More about "machine 2 Machine" integrations aka the client_credentials OAuth2.0 grant https://developer.xero.com/announcements/custom-integrations-are-coming/