Skip to content
Advertisement

XERO PHP SDK – Createinvoiceattachment throws 401 Unauthorized error

I am trying to attach a document to an invoice. It throws the error. The tenant id, invoice id are correct.

Exception when calling AccountingApi->createInvoiceAttachmentByFileName: [401] Client error: PUT https://api.xero.com/api.xro/2.0/Invoices/b03b18ca-50b2-42fa-a585-aa5674b7ed03/Attachments/2New-Requirment-Oct2020.docx?IncludeOnline=true resulted in a 401 Unauthorized response: {“Type”:null,”Title”:”Unauthorized”,”Status”:401,”Detail”:”AuthorizationUnsuccessful”,”Instance”:”54c33c01-e168-47d5-abe (truncated…)

My Code is:

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

Advertisement

Answer

Attachments require different scopes to use.

Have you included the ‘accounting.attachments’ scope in the scopes requested when authorising the connection?

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement