XERO PHP SDK-Createinvoiceattachment引发401未经授权的错误

问题描述

我正在尝试将文档附加到发票上。它引发错误。租户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响应: {“类型”:null,“标题”:“未授权”,“状态”:401,“详细信息”:“未成功授权”,“实例”:“ 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;
            }   
      } 

解决方法

附件需要使用不同的作用域。

在授权连接时,您是否已将'accounting.attachments'范围包括在请求的范围中?