首页 > 解决方案 > 获取 401 Unauthorized Xero oAuth 2.0 -

问题描述

我正在尝试使用 Xero oAuth 2.0 创建一个新联系人并收到此错误:

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

我的范围为:

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

我能够连接到 Xero(使用此代码)并收到消息The App is connected to Xero但是当我使用以下代码时:-

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;
    }

错误:[401] 客户端错误:PUT https://api.xero.com/api.xro/2.0/Contacts?summarizeErrors=true导致401 Unauthorized响应:

标签: phplaraveloauth-2.0xero-api

解决方案


我没有看到您的所有代码,但您可能只需要在 PHP 客户端上设置有效的access_token 。

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


推荐阅读