webfox / laravel-xero-oauth2

A Laravel integration for Xero using the Oauth 2.0 spec
MIT License
50 stars 32 forks source link

Unable to create contact in xero by using this package while Account creation works perfectly #19

Closed bilalswiftsolutions closed 4 years ago

bilalswiftsolutions commented 4 years ago

Whenever I try to create Contact it giver 401 Authorization error

XeroAPI \ XeroPHP \ ApiException (401) [401] Client error: PUT https://api.xero.com/api.xro/2.0/Contacts?summarizeErrors=true resulted in a 401 Unauthorized response: {"Type":null,"Title":"Unauthorized","Status":401,"Detail":"AuthorizationUnsuccessful","Instance":"ece8ffa7-a581-4638-886 (truncated...)

but when I try to create Account it work perfectly

Contact Code


       $apiInstance  = resolve(\XeroAPI\XeroPHP\Api\AccountingApi::class);

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

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

Account creating code

$apiInstance  = resolve(\XeroAPI\XeroPHP\Api\AccountingApi::class);
       $account = new \XeroAPI\XeroPHP\Models\Accounting\Account;
        $account->setCode(11010);
        $account->setName("My Contact");
        $account->setType("EXPENSE");
        $account->setDescription("Hello World");
        $result = $apiInstance->createAccount($xeroCredentials->getTenantId(), $account); 
hailwood commented 4 years ago

Hi @bilalswiftsolutions this package deals with the initial Oauth handshakes, Of you can create an account then this library has successfully connected you, but once you've got the instance of the AccountingApi you're dealing directly with the api from Xero - https://github.com/XeroAPI/xero-php-oauth2

My guess is you're missing a required scope for dealing with the contacts, can you please show me which scopes you're using in the config file?

bilalswiftsolutions commented 4 years ago

Can you please tell where i will find this scope ? because i thought that i dont need to mess with these things after using this library please guide me where i will find this scope, so i can show you here.

hailwood commented 4 years ago

That's a god point, I really need to improve the docs :)

If you publish the package config file it will create a xero.php file in your config directory, there is a scopes key there.

The default scopes are

            'openid',
            'email',
            'profile',
            'offline_access',
            'accounting.settings',

Which is why creating an account works (accounting.settings is required for the authorization to work hence why we add it by default), but there's no contact scopes so that won't work out of the box. You should just need to add the accounting.contacts scope.

bilalswiftsolutions commented 4 years ago

ok let me check

bilalswiftsolutions commented 4 years ago

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

I added the scope there and also restart server.. but still getting this error

bilalswiftsolutions commented 4 years ago

sorry now i am getting this error

XeroAPI \ XeroPHP \ ApiException (400)
[400] Client error: `PUT https://api.xero.com/api.xro/2.0/Contacts?summarizeErrors=false` resulted in a `400 Bad Request` response: { "ErrorNumber": 14, "Type": "PostDataInvalidException", "Message": "JSON for post data was invalid,Error conve (truncated...)
hailwood commented 4 years ago

I just updated the docs to make the scope stuff clearer, thanks for bringing that to my attention.

You'll probably need to clear your cache php artisan cache:clear so the app fetches a new token using your new scopes, presently it will be using the old token with the default scopes.

bilalswiftsolutions commented 4 years ago

done.this time error was in my code . it worked that @hailwood Yo saved me i spent whole previouse day on it thank you so much ..

hailwood commented 4 years ago

Glad it's all working for you now. I'm going to close this issue now :)

kunallibra commented 3 years ago

sorry now i am getting this error

XeroAPI \ XeroPHP \ ApiException (400)
[400] Client error: `PUT https://api.xero.com/api.xro/2.0/Contacts?summarizeErrors=false` resulted in a `400 Bad Request` response: { "ErrorNumber": 14, "Type": "PostDataInvalidException", "Message": "JSON for post data was invalid,Error conve (truncated...)

hi,

I am stuck at the same point. can u please help me how did you safe this?

I am getting exactly same as your error.

Thanks.