使用C#在线创建Quickbooks客户

时间:2013-11-18 18:21:57

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

我需要从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使用者密钥

我正在使用QuickBooks Online REST API v3.0 for .Net

代码如下:

   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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

Intuit Anywhere Developer Playground提供了获取accessToken,accessTokenSecret和realmId的功能。

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

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

确保您已登录Intuit帐户。

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

enter image description here

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

您将获得访问令牌,访问令牌密钥,RealmId(公司ID)和数据源。现在您拥有实现Accounting API的所有密钥。这些密钥将在120000秒后过期。

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

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

答案 2 :(得分:0)

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

}