【问题标题】:Creating Customer in Quickbooks Online from C#从 C# 在 Quickbooks Online 中创建客户
【发布时间】:2013-11-18 18:21:56
【问题描述】:

我需要使用 C# 将新客户添加到 QuickBooks Online。

我正在努力寻找任何有效的端到端示例。基于此: how to add invoice or sales receipt quickbooks rest api v3.0我需要accessToken、accessTokenSecret、consumerKey、consumerKeySecret、realmId。

基于此:IPP .NET SDK for QuickBooks v3.0 Create Invoice Error - Bad Request我需要accessToken、accessTokenSecret、consumerKey、consumerKeySecret、appToken、companyId。

我只有 App Token、Oauth Consumer Key、OAuth consumer Secret,可能还有 realmId(App ID)

在哪里可以找到访问令牌、访问令牌秘密、领域 ID 和/或公司 ID?

我没有发布图片的声誉,但开发者设置页面在 https://developer.intuit.com/Application/Manage/IA?appId=XXX 显示如下信息:

应用 ID:这是什么?领域 ID,公司 ID?

应用令牌、OAuth 消费者密钥、OAuth 消费者密钥

我正在使用适用于 .Net 的 QuickBooks Online REST API v3.0

代码如下:

   var customer = new Customer();

   customer.GivenName = txtFirstName.Text;
   customer.FamilyName = txtLastName.Text;
   customer.MiddleName = txtMiddleName.Text;
   customer.CompanyName = txtCompany.Text;

   customer.PrimaryEmailAddr = new EmailAddress() { Address = txtEmail.Text, Default = true };
   customer.PrimaryPhone = new TelephoneNumber() { FreeFormNumber = txtPrimaryPhone.Text };


   OAuthRequestValidator oauthValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerSecret);
   ServiceContext context = new ServiceContext(accessToken, consumerKey, IntuitServicesType.QBO, oauthValidator);

   DataService service = new DataService(context);
   var c = service.Add(customer);
   // do something with c.Id

【问题讨论】:

    标签: c# intuit-partner-platform quickbooks-online


    【解决方案1】:

    您需要使用 C2QB 按钮在您的应用中实现 3 腿 oauth 流程,以获取访问令牌和密钥。 请查看我们的示例应用程序以了解这是如何实现的。下载示例应用程序,然后在配置文件中配置使用者密钥和秘密。 然后检查 3-legged oauth 是如何发生的。您也可以尝试使用示例应用程序的本地副本调用 V3 API。 https://github.com/IntuitDeveloperRelations/ 另请参考: https://developer.intuit.com/docs/0025_quickbooksapi/0010_getting_started

    【讨论】:

    【解决方案2】:

    Intuit Anywhere Developer Playground 提供了获取 accessToken、accessTokenSecret 和 realmId 的工具。

    点击下面提到的链接转到 QuickBooks 游乐场 -

    https://appcenter.intuit.com/Playground/OAuth/IA

    确保您已经登录到您的 Intuit 帐户。

    填写消费者密钥、消费者秘密和访问令牌持续时间(120000 秒)。

    点击连接到 QuickBooks 图标,如图所示。您将被要求选择公司。如果您有多个公司连接到您的沙盒帐户,请根据您的要求选择任何一个。 然后点击授权按钮。

    您将获得 Access Token、Access Token Secret、RealmId (Company Id) 和 DataSource。现在您拥有了实现会计 API 的所有密钥。这些密钥将在 120000 秒后过期。

    我写了一篇关于如何添加、更新、查找和删除客户的博客。

    https://vivekkumar11432.wordpress.com/2016/06/07/implement-intuits-quickbooks-online-api-in-restful-service-c/

    【讨论】:

      【解决方案3】:

      我是 QuickBooks Online API 的新手,无法让代码为 .NET 5.0 WPF 应用程序工作,所以这里有一些对我有用的示例代码:

      安装所需的QBO依赖:

      • IppDotNetSdkForQuickBooksApiV3
      • IppOAuth2PlatformSdk

      WPF 窗口 C# 代码:

      using Intuit.Ipp.Data;
      
      public partial class MainWindow : Window
      {  
          QuickbooksOnline qbo = new QuickbooksOnline();
      
          public MainWindow()
          {
              qbo.FindCustomerResponse += HandleFindQBOCustomerResponse;
              qbo.FindInvoiceResponse += HandleFindQBOInvoiceResponse;
              qbo.FindCreditMemoResponse += HandleFindQBOCreditMemoResponse;
          }
      
          private void btnTest_Click(object sender, RoutedEventArgs e)
          {
              //Get IDs to test from QBO website URL when accessing a record
      
              string customerId = "737";
              qbo.FindCustomer(customerId);
      
              string invoiceId = "3198";
              qbo.FindInvoice(invoiceId);
      
              string creditMemoId = "3077";
              qbo.FindCreditMemo(creditMemoId);
          }
      
          private void HandleFindQBOCustomerResponse(object sender, Intuit.Ipp.Data.Customer customer)
          {
              if (customer == null)
              {
                  MessageBox.Show("Customer not found");
              }
              else
              {
                  MessageBox.Show(customer.DisplayName);
              }
          }
      
          private void HandleFindQBOInvoiceResponse(object sender, Intuit.Ipp.Data.Invoice invoice)
          {
              if (invoice == null)
              {
                  MessageBox.Show("Invoice not found");
              }
              else
              {
                  MessageBox.Show(invoice.TotalAmt.ToString());
              }
          }
      
          private void HandleFindQBOCreditMemoResponse(object sender, Intuit.Ipp.Data.CreditMemo creditMemo)
          {
              if (creditMemo == null)
              {
                  MessageBox.Show("CreditMemo not found");
              }
              else
              {
                  MessageBox.Show(creditMemo.TotalAmt.ToString());
              }
          }
      }
      

      Quickbooks 在线课程:

      using System;
      using System.Threading.Tasks;
      using Intuit.Ipp.Core;
      using Intuit.Ipp.Data;
      using Intuit.Ipp.Security;
      using Intuit.Ipp.DataService;
      using System.Windows;
      using System.Reflection;
      using Intuit.Ipp.OAuth2PlatformClient;
      
      class QuickbooksOnline
      {
          //Get this information from the OAuth 2.0 Playground: https://developer.intuit.com/app/developer/playground
          private string ClientId = "";
          private string ClientSecret = "";
          private string RedirectURI = "https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl";
          private string RealmId = "";
          private string AccessToken = "";
          private string RefreshToken = "";
          private string QBOBaseURL = "https://quickbooks.api.intuit.com/";
          private string QBOMinorVersion = "55";
      
          private OAuth2Client OAuthClient;
      
          public event EventHandler<Intuit.Ipp.Data.Customer> FindCustomerResponse;
          public event EventHandler<Intuit.Ipp.Data.Invoice> FindInvoiceResponse;
          public event EventHandler<Intuit.Ipp.Data.CreditMemo> FindCreditMemoResponse;
      
          public QuickbooksOnline()
          {
              this.OAuthClient = new OAuth2Client(ClientId, ClientSecret, RedirectURI, "production");
          }
      
          public async void FindCustomer(string customerId)
          {
              OAuth2RequestValidator oAuthValidator = new OAuth2RequestValidator(AccessToken);
              ServiceContext serviceContext = new ServiceContext(RealmId, IntuitServicesType.QBO, oAuthValidator);
              serviceContext.IppConfiguration.BaseUrl.Qbo = QBOBaseURL;
              serviceContext.IppConfiguration.MinorVersion.Qbo = QBOMinorVersion;
              DataService service = new DataService(serviceContext);
      
              Customer customer = new Customer();
              customer.Id = customerId;
      
              service.OnFindByIdAsyncCompleted += HandlePrivateFindByIdResponse;
      
              service.FindByIdAsync(customer);
          }
      
      
          public void FindInvoice(string invoiceId)
          {
              OAuth2RequestValidator oAuthValidator = new OAuth2RequestValidator(AccessToken);
              ServiceContext serviceContext = new ServiceContext(RealmId, IntuitServicesType.QBO, oAuthValidator);
              serviceContext.IppConfiguration.BaseUrl.Qbo = QBOBaseURL;
              serviceContext.IppConfiguration.MinorVersion.Qbo = QBOMinorVersion;
              DataService service = new DataService(serviceContext);
      
              Intuit.Ipp.Data.Invoice invoice = new Intuit.Ipp.Data.Invoice();
              invoice.Id = invoiceId;
      
              service.OnFindByIdAsyncCompleted += HandlePrivateFindByIdResponse;
      
              service.FindByIdAsync(invoice);
          }
      
          public void FindCreditMemo(string creditMemoId)
          {
              OAuth2RequestValidator oAuthValidator = new OAuth2RequestValidator(AccessToken);
              ServiceContext serviceContext = new ServiceContext(RealmId, IntuitServicesType.QBO, oAuthValidator);
              serviceContext.IppConfiguration.BaseUrl.Qbo = QBOBaseURL;
              serviceContext.IppConfiguration.MinorVersion.Qbo = QBOMinorVersion;
              DataService service = new DataService(serviceContext);
      
              CreditMemo creditMemo = new CreditMemo();
              creditMemo.Id = creditMemoId;
      
              service.OnFindByIdAsyncCompleted += HandlePrivateFindByIdResponse;
      
              service.FindByIdAsync(creditMemo);
          }
      
          private async void HandlePrivateFindByIdResponse(object sender, CallCompletedEventArgs<IEntity> entity)
          {
              // The Intuit.Ipp.DataService.AsyncService class is not public, so that to use reflection
              // to get the requestedEntity object and determine the sender type.
              // This is useful when the authToken has expired and after refreshing the tokens
              // the request has to be sent again.
              string entityType = sender.GetType().GetField("requestedEntity", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sender).GetType().FullName;
      
              if (entity.Error != null && entity.Error.Message.Equals("Unauthorized-401"))
              {
                  
                  if (await RefreshTokens())
                  {
                      switch (entityType)
                      {
                          case "Intuit.Ipp.Data.Customer":
                              Customer senderCustomer = (Customer)sender.GetType().GetField("requestedEntity", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sender);
                              FindCustomer(senderCustomer.Id);
                              break;
                          case "Intuit.Ipp.Data.Invoice":
                              Intuit.Ipp.Data.Invoice senderInvoice = (Intuit.Ipp.Data.Invoice)sender.GetType().GetField("requestedEntity", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sender);
                              FindInvoice(senderInvoice.Id);
                              break;
                          case "Intuit.Ipp.Data.CreditMemo":
                              Intuit.Ipp.Data.CreditMemo senderCreditMemo = (Intuit.Ipp.Data.CreditMemo)sender.GetType().GetField("requestedEntity", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sender);
                              FindCreditMemo(senderCreditMemo.Id);
                              break;
                      }
                  }
                  else
                  {
                      MessageBox.Show("Error refreshing tokens.");
                  }
              }
              else if (entity.Entity != null)
              {
                  switch (entity.Entity.GetType().FullName)
                  {
                      case "Intuit.Ipp.Data.Customer":
                          if (FindCustomerResponse != null)
                          {
                              FindCustomerResponse(this, entity.Entity as Intuit.Ipp.Data.Customer);
                          }
                          break;
                      case "Intuit.Ipp.Data.Invoice":
                          if (FindInvoiceResponse != null)
                          {
                              FindInvoiceResponse(this, entity.Entity as Intuit.Ipp.Data.Invoice);
                          }
                          break;
                      case "Intuit.Ipp.Data.CreditMemo":
                          if (FindCreditMemoResponse != null)
                          {
                              FindCreditMemoResponse(this, entity.Entity as Intuit.Ipp.Data.CreditMemo);
                          }
                          break;
                  }
              }
              else if (entity.Entity == null)
              {
                  switch (entityType)
                  {
                      case "Intuit.Ipp.Data.Customer":
                          if (FindCustomerResponse != null)
                          {
                              FindCustomerResponse(this, null);
                          }
                          
                          break;
                      case "Intuit.Ipp.Data.Invoice":
                          if (FindInvoiceResponse != null)
                          {
                              FindInvoiceResponse(this, null);
                          }
                          break;
                      case "Intuit.Ipp.Data.CreditMemo":
                          if (FindCreditMemoResponse != null)
                          {
                              FindCreditMemoResponse(this, null);
                          }
                          break;
                  }
              }
          }
      
          private async Task<bool> RefreshTokens()
          {
              var tokenResp = await OAuthClient.RefreshTokenAsync(RefreshToken);
      
              if (tokenResp.AccessToken != null && tokenResp.RefreshToken != null)
              {
                  this.AccessToken = tokenResp.AccessToken;
                  this.RefreshToken = tokenResp.RefreshToken;
                  return true;
              }
              else
              {
                  return false;
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2014-06-20
        • 1970-01-01
        • 1970-01-01
        • 2014-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多