Getting 401 Unauthorized Xero oAuth 2.0 -

685 views Asked by At

I am trying to create a new contact using Xero oAuth 2.0 and getting this error:

[401] Client error: `PUT https://api.xero.com/api.xro/2.0/Contacts?summarizeErrors=true` resulted in a `401 Unauthorized` response: 

I have got my scopes as:

'scopes'   => [
            'openid',
            'email',
            'profile',
            'offline_access',
            'accounting.settings',
            'accounting.contacts',
            'accounting.reports.read',
        ],

I am able to connect to Xero (using this code) and I get message The App is connected to Xero but when I use the following code:-

public function xeroTest(OauthCredentialManager $xeroCredentials) {

        $apiInstance  = resolve(\XeroAPI\XeroPHP\Api\AccountingApi::class);
        $contact = resolve( \XeroAPI\XeroPHP\Models\Accounting\Contact::class);
        $contact->setName('Abcdef');
        $contact->setContactId('6d42f03b-181f-43e3-93fb-2025c0hjhe92');

        $apiResponse = $apiInstance->createContacts($xeroCredentials->getTenantId() ,$contact,true);
        $message = 'New Contact Name: ' . $apiResponse->getContacts()[0]->getName();

        return $message;
    }

Error: [401] Client error: PUT https://api.xero.com/api.xro/2.0/Contacts?summarizeErrors=true resulted in a 401 Unauthorized response:

1

There are 1 answers

2
SerKnight On

I don't see all your code but you likely just have to set the valid access_token on the PHP client.

https://github.com/XeroAPI/xero-php-oauth2#callbackphp