Xero OAuth 2.0 API-c#dotnet核心-将附件上传到发票时未经授权-其他请求很好

时间:2020-05-02 20:04:33

标签: c# xero-api

我有一个dotnet核心Web应用程序,该应用程序使用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"
  },

1 个答案:

答案 0 :(得分:2)

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

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

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

我是Xero的开发人员,致力于我们的API,因此,如果您不认为这是原因,则可以编辑问题以在响应消息中包含“ guid-here Instance”属性,我可以进行关联日志在我们这边