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