0

how to create new contacts in Xero using API?

i have tried this API - https://api.xero.com/api.xro/2.0/Contacts but i always get this error : "oauth_problem=consumer_key_unknown&oauth_problem_advice=Consumer key was not recognised."

I don't know how to pass client id and client secret or access token in API?

passing parameter in the header is not working -

curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Basic " . base64_encode('75F16AECC7F749E08D1822B32CD110EB:lJ_h1cG7dvE5xIAXDWyFDcmGQPqtZOlHzfAzw0ImdLPKOxL5'), 'Content-Type: application/json' ]);

please help

Andrew
  • 35
  • 1
  • 1
  • 5

1 Answers1

1

Xero uses the OAuth2 authorization flow. Broadly speaking, you'll need to:

  • Send the Xero user to an authorising URL with your client id, which will result in the user being redirected to your specified redirect uri with a code
  • Swap this code for an access token using your client id and secret
  • Use the access token as a bearer token in your authorization header when you're making your request to the contacts endpoint

The full details are described in the Xero Developer docs.

rustyskates
  • 856
  • 4
  • 10
  • i have followed all the steps mentioned in https://developer.xero.com/documentation/oauth2/auth-flow. i got the access token and refresh token. used the access token as a bearer token in your authorization header. also added xero tenant id in header but always showing the same error. – Andrew Feb 14 '20 at 08:19
  • Can you post your updated curl request using the token? Is the error response exactly the same? – rustyskates Feb 14 '20 at 08:27
  • curl_setopt($curl, CURLOPT_HTTPHEADER, [ "Authorization: Bearer 1fbc811357a1...", 'Content-Type: application/json', 'xero_tenant_id: 66f9473f...' ]); – Andrew Feb 14 '20 at 13:47
  • i have used this one but shows the same error. oauth_problem=consumer_key_unknown&oauth_problem_advice=Consumer%20key%20was%20not%20recognised – Andrew Feb 14 '20 at 13:52
  • If you're getting a `consumer_key_unknown` error, your bearer token is not being set properly (and your request is going through to the old OAuth1.0a endpoint, rather than the OAuth2 endpoint). That's the direction you should investigate in. – rustyskates Feb 16 '20 at 20:39
  • i don't know what's wrong. now I'm using Xero SDK and its working fine. anyway, thanks for your suggestions. – Andrew Feb 19 '20 at 14:19