0

I am trying to use the Xero API to send an Invoice Email on my WordPress site. But I am unsure how to set the authorization header I have attempted the following:

$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);

This gives me a response of 404 unauthorized. Is there something I'm missing or doing wrong?

Reece
  • 2,581
  • 10
  • 42
  • 90

1 Answers1

1

the Authorization header actually requires a valid access_token be set, not the id / secret combo. You can read more about the code flow required to get a token here: 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


Is your goal to send that invoice email to a dynamic organisation, or simply send an invoice from your own personal org?

Fortunately (or unfortunately) early next year we will have the option for this embedded access_token - but if you wanted to set this up in the interim you will need to generate an access token from a user flow, and then setup the backend mechanism to have it be refreshed prior to usage which I explain in this youtube vid: 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/

SerKnight
  • 2,502
  • 1
  • 16
  • 18
  • Thanks for the help. My goal: I have a function that creates a WooCommerce order and sends the invoice to Xero. I then have a Xero hook that watches for new invoices. Once one is created I want to access the api inside the hook function and send the invoice to the user for them to pay. Does the payload from the hook contain any of the authorisation credentials or do I still have to manually do this? – Reece Nov 19 '20 at 11:58
  • Managed to figure it out by watching your tutorial and following the Postman tutorial. Thanks – Reece Nov 19 '20 at 17:15
  • Always glad to hear. Hope rest of dev goes smooth. Drop us a line on twitter once you've got everything running! – SerKnight Nov 19 '20 at 20:26