Xero OAuth 2.0 API - c# dotnet core - 上传附件到发票时未经授权 - 其他请求正常

Xero OAuth 2.0 API - c# dotnet core - unauthorised when upload attachment to invoice - other requests fine

我有一个 dotnet 核心网络应用程序,它使用 xero OAUTH2 发票来添加发票和查询数据。但是当我尝试上传附件时,我收到了 401 未经授权的响应。这是有问题的代码

if (timeRecordAttachment != null)
{
    try
    {
        string fileName = $"TimeRecord-{exportTotal.ExportCustomer.Code}-H-{exportTotal.ExportHeader.Id}-{exportTotal.ExportHeader.Year:0000}-{exportTotal.ExportHeader.Month:00}.txt";
        await _accountingApi.CreateInvoiceAttachmentByFileNameAsync(xeroToken.AccessToken, _configuration["Xero:TenantId"], (Guid)exportTotal.XeroInvoiceId, fileName, timeRecordAttachment, true).ConfigureAwait(false);
    }
    catch (ApiException ex)
    {
         _logger.LogWarning($"API exception code {ex.ErrorCode} and Message {ex.Message} was thrown when uploading pdf time record - invoice has been created");
    }
    catch
    {
        throw;
    }
}
logger.LogInformation($"Invoice {generatedInvoice.InvoiceID} created with number {generatedInvoice.InvoiceNumber} for total Id {id}");

错误是:

API exception code 401 and Message Xero API error calling CreateInvoiceAttachmentByFileName: {"Type":null,"Title":"Unauthorized","Status":401,"Detail":"AuthorizationUnsuccessful","Instance":"4dbcd6ca-d260-417c-a8d3-4cde373bfdd9","Extensions":{}} was thrown when uploading pdf time record - invoice has been created

上传发票需要一些特殊权限吗?我现在正在使用演示组织进行测试,可以吗?

发票创建在前面几行工作得很好:

Invoice generatedInvoice = new Invoice();
try
{
    var response = await _accountingApi.CreateInvoicesAsyncWithHttpInfo(xeroToken.AccessToken, _configuration["Xero:TenantId"], invoices).ConfigureAwait(false);
    generatedInvoice = response.Data._Invoices.FirstOrDefault();
}

对于那些阅读本文有类似问题的人,我编辑了范围以包括 accounting.attachments

"Xero": {
    "Scopes": "openid profile email accounting.transactions accounting.contacts accounting.settings offline_access accounting.attachments"
  },

通过 OAuth2.0 产生 401 响应的最常见原因是令牌过期或范围不正确。

如果您可以将访问令牌用于某些端点但不能用于其他端点,则可能不是令牌过期问题。

我的猜测是,当您授权连接时,您没有请求 accounting.attachments 范围,因此您的用户不允许您代表他们管理他们的附件。

我是 Xero 的一名开发人员,致力于我们的 API,所以如果您认为这不是原因,您可以编辑您的问题以在您的问题中包含 some-guid-here 实例 属性响应消息,我可以关联到我们这边的日志