Xero Accounting API GetContactsAsync未授权+联系人不包含针对“ GetEnumerator”错误的公共实例定义

时间:2020-02-29 15:56:39

标签: c# asp.net-mvc asp.net-core xero-api

我一直在尝试学习如何使用Account API,并决定下载并使用文档中提供的XeroOAuth2Sample。 (https://github.com/XeroAPI/xero-netstandard-oauth2-samples

因此,在HomeController.cs中,有一个示例HTTPGet方法,该方法用于从Xero文档提供的API中检索发票的总数。代码如下:

[HttpGet]
        [Authorize]
        public async Task<IActionResult> OutstandingInvoices()
        {
            var token = await _tokenStore.GetAccessTokenAsync(User.XeroUserId());

            var connections = await _xeroClient.GetConnectionsAsync(token);

            if (!connections.Any())
            {
                return RedirectToAction("NoTenants");
            }

            var data = new Dictionary<string, int>();

            foreach (var connection in connections)
            {
                var accessToken = token.AccessToken;
                var tenantId = connection.TenantId.ToString();

                var organisations = await _accountingApi.GetOrganisationsAsync(accessToken, tenantId);
                var organisationName = organisations._Organisations[0].Name;

                var outstandingInvoices = await _accountingApi.GetInvoicesAsync(accessToken, tenantId, statuses: new List<string>{"AUTHORISED"}, where: "Type == \"ACCREC\"");

                data[organisationName] = outstandingInvoices._Invoices.Count;
            }

            var model = new OutstandingInvoicesViewModel
            {
                Name = $"{User.FindFirstValue(ClaimTypes.GivenName)} {User.FindFirstValue(ClaimTypes.Surname)}",
                Data = data
            };

            return View(model);
        }

因此,我一直在尝试通过制作一个最终会从API端点调用联系人的页面来练习和探索API。我创建了一个看起来像这样的Contact.cs Model类:

public class Contact
    {
        public string ContactID { get; set; }
        public string ContactStatus { get; set; }
        public string Name { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
        public string SkypeUserName { get; set; }
        public string BankAccountDetails { get; set; }
        public string TaxNumber { get; set; }
        public string AccountsReceivableTaxType { get; set; }
        public string AccountsPayableTaxType { get; set; }
        public List<Address> Addresses { get; set; }
        public List<Phone> Phones { get; set; }
        public DateTime UpdatedDateUTC { get; set; }
        public bool IsSupplier { get; set; }
        public bool IsCustomer { get; set; }
        public string DefaultCurrency { get; set; }
    }

然后,我创建了一个ContactViewModel.cs,该属性具有稍后要在我的Razor View页面上显示的属性,并带有以下代码:

public class ContactViewModel
    {
        public string ContactID { get; set; }
        public string ContactStatus { get; set; }
        public string Name { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool IsSupplier { get; set; }
        public bool IsCustomer { get; set; }
        public string DefaultCurrency { get; set; }
    }

然后,我有我的ContactsViewModel,它是ContactViewModel中的联系人列表:

public class ContactsViewModel
    {
        public List<ContactViewModel> Contacts { get; set; }
    }

因此,当我尝试为联系人创建/调用HTTPGet请求时,出现了我的问题,代码如下:

[HttpGet]
        [Authorize]
        public async Task<IActionResult> Contacts()
        {
            var token = await _tokenStore.GetAccessTokenAsync(User.XeroUserId());

            var connections = await _xeroClient.GetConnectionsAsync(token);

            if (!connections.Any())
            {
                return RedirectToAction("NoContacts");
            }   

            foreach (var connection in connections)
            {
                var accessToken = token.AccessToken;
                var tenantId = connection.TenantId.ToString();

                var contactList = await _accountingApi.GetContactsAsync(accessToken, tenantId);

                List<ContactsViewModel> contacts = new List<ContactsViewModel>();

                foreach (var contact in contactList)
                {
                    contacts.Add(new ContactViewModel
                    {
                        ContactID = contact.ContactID,
                        ContactStatus = contact.ContactStatus,
                        Name = contact.Name,
                        FirstName = contact.FirstName,
                        LastName = contact.LastName,
                        IsSupplier = contact.IsSupplier,
                        IsCustomer = contact.IsCustomer

                    });
                }
                contacts.AddRange(contactList);
            }
            var model = new ContactsViewModel()
            {
                //  Contacts = contacts
            };
            return View(model);

        }

第一个错误如下:

ApiException: Error calling GetContacts: {"title":"Unauthorized","status":401,"detail":"AuthorizationUnsuccessful","instance":"354ff497-d29f-468b-9e1c-4345e9ce8123"}

从“ GetContactsAsync”方法返回哪个:

var contactList = await _accountingApi.GetContactsAsync(accessToken, tenantId);

我不确定是否缺少我需要传递的特定值,这会导致此错误?我在Xero文档中找不到与此有关的任何内容。尽管将鼠标悬停在GetcontactsAsync上会显示此信息以获取更多信息:

(awaitable) Task<Xero.NetStandard.OAuth2.Model.Contacts> IAccountingApiAsync.GetContactsAsync(string accessToken, stringXeroTenantId, [System.DateTime? ifModifiedSince = null], [string where = null], [string order = null], [List<System.Guid> iDs = null], [int? page = null], [bool? includeArchived = null])

最后,正如标题中关于“ GetEnumerator”的提示所示,contactList似乎引发了错误,并且当使用AddRange将联系人添加到contactsList时,将显示该错误

cannot convert from 'Xero.NetStandard.OAuth2.Model.Contacts' to 'System.Collections.Generic.IEnumerable<XeroOAuth2Sample.Models.ContactsViewModel>'

我显然应该从GetContactsAsync中缺少一些应该存在的东西吗?预先感谢您的阅读和帮助。

2 个答案:

答案 0 :(得分:3)

除了示例中要求的默认范围外,联系人端点还需要其他OAuth2.0范围。您可以在此处查看示例使用的范围集:https://github.com/XeroAPI/xero-netstandard-oauth2-samples/blob/master/XeroOAuth2Sample/XeroOAuth2Sample/Startup.cs#L105L106

您可以在我们的文档中查看以下范围的完整范围:https://developer.xero.com/documentation/oauth2/scopes

对于您的情况,如果最终要更新/创建联系人,则需要使用accounting.contacts或accounting.contacts.read范围来读取联系人,或者使用accounting.contacts范围。

编辑:如果您只想读取联系人,则需要请求并获得用户对Accounting.contacts.read范围的同意。如果要更新/创建联系人,则需要请求并让用户同意Accounting.contacts范围。 accounting.contacts范围将允许读写访问

答案 1 :(得分:1)

因此,在向startup.cs添加更多选项之后:

options.Scope.Add("accounting.contacts");
options.Scope.Add("accounting.contacts.read");

根据建议,我再次经历了xero注册过程,以同意新的范围。我认为已纠正了HomeController中我的Contacts()HTTPGet方法的语法和一些轻微的逻辑。

 [HttpGet]
        [Authorize]
        public async Task<IActionResult> Contacts()
        {
            var token = await _tokenStore.GetAccessTokenAsync(User.XeroUserId());

            var connections = await _xeroClient.GetConnectionsAsync(token);

            ContactsViewModel contacts = new ContactsViewModel();
            contacts.Contacts = new List<ContactViewModel>();

            if (!connections.Any())
            {
                return RedirectToAction("NoContacts");
            }   

            foreach (var connection in connections)
            {
                var accessToken = token.AccessToken;
                var tenantId = connection.TenantId.ToString();

                Contacts contactList = await _accountingApi.GetContactsAsync(accessToken, tenantId);
                contacts.Contacts.AddRange(contactList._Contacts.Select(contact => new ContactViewModel()
                {
                    ContactID = contact.ContactID.ToString(),
                    ContactStatus = contact.ContactStatus.ToString(),
                    Name = contact.Name,
                    FirstName = contact.FirstName,
                    LastName = contact.LastName,
                    IsSupplier = contact.IsSupplier.Value,
                    IsCustomer = contact.IsCustomer.Value
                }).ToList());
            }

            return View(contacts);
        }