首页 > 解决方案 > XERO PHP SDK - Createinvoiceattachment 抛出 401 Unauthorized 错误

问题描述

我正在尝试将文档附加到发票上。它抛出错误。租户 ID、发票 ID 正确。

调用 AccountingApi->createInvoiceAttachmentByFileName 时出现异常:[401] 客户端错误: PUT https://api.xero.com/api.xro/2.0/Invoices/b03b18ca-50b2-42fa-a585-aa5674b7ed03/Attachments/2New-Requirment-Oct2020.docx?IncludeOnline=true 导致401 Unauthorized响应:{"Type":null,"Title":"Unauthorized","Status":401,"Detail":"AuthorizationUnsuccessful","Instance ":"54c33c01-e168-47d5-abe (截断...)

我的代码是:

         if (isset($_FILES["gfile1"]))
         {  
            $target_dir = "uploads/bills/";
            $target_file = $target_dir . basename($_FILES["gfile1"]["name"]);
            $FileType = pathinfo($target_file,PATHINFO_EXTENSION);      
           
            $filename1 = $billid . basename($_FILES["gfile1"]["name"]);
            
            $filename1 = str_replace(" ", "", $filename1);
            $filename1 = str_replace("#", "", $filename1);              
            
            $target_filenew = $target_dir . $filename1; 
            move_uploaded_file($_FILES["gfile1"]["tmp_name"], $target_filenew); 

            $attachmentFile = file_get_contents(base_url() . $target_filenew);

            try {
                        
                $result = $accountingApi->createInvoiceAttachmentByFileName($xeroTenantId, $invid, $filename1, 
                $attachmentFile, true);

            } catch (Exception $e) {
            echo 'Exception when calling AccountingApi->createInvoiceAttachmentByFileName: ',
               $e->getMessage(), PHP_EOL;
            }   
      } 

标签: phpxero-api

解决方案


附件需要使用不同的范围。

您在授权连接时是否在请求的范围中包含了“accounting.attachments”范围?


推荐阅读