Note
Note
This step can be skipped if you are running the Hello, World! sample app.
To install the IppDotNetSdkForQuickBooksApiV3 package, use the NuGet console in Visual Studio:
1 | Install-Package IppDotNetSdkForQuickBooksApiV3
|
Intuit.Ipp
entries under References—for example, Intuit.Ipp.DataService
.Note
Note
Skip to the next section if you have already created an app in the developer portal.
View Screenshot
View Screenshot
View Screenshot
View Screenshot
Note
Note
Creating a developer account also creates a QuickBooks test company (also referred to as a sandbox company). You can create up to 5 sandbox companies.
You will use your sandbox company in the following steps to become familiar with the authorization flow.
View Screenshot
View Screenshot
http://localhost:27353/callback
. Set the Redirect URI in the Keys & OAuth tab with this URL.View Screenshot
<appSettings>
section of your Web.config file as shown here. To view a complete web.config
file, go here.
1 2 3 4 5 6 | <appSettings> <add key="clientid" value="Enter value here" /> <add key="clientsecret" value="Enter value here" /> <add key="redirectUrl" value="http://localhost:27353/callback" /> <add key="appEnvironment" value="sandbox" /> </appSettings> |
OAuth2Client
object with clientId, clientsecret, redirectUrl and environment. Using this object, generate the authorization URL to get the OAuth2 authorization code by specifying required scopes. The following example shows how to generate the authorization URL. To see a complete code example, go here.
1 2 3 4 5 6 7 8 9 10 11 12 | //Instantiate OAuth2Client object with clientId, clientsecret, redirectUrl and environment public static OAuth2Client auth2Client = new OAuth2Client(clientid, clientsecret, redirectUrl, environment); //Generate authorize url to get the OAuth2 code public ActionResult InitiateAuth(string submitButton) { List<OidcScopes> scopes = new List<OidcScopes>(); scopes.Add(OidcScopes.Accounting); string authorizeUrl = auth2Client.GetAuthorizationURL(scopes); return Redirect(authorizeUrl); } |
View Screenshot
After they click the Connect button, the request is sent to an Intuit server. When successful, Intuit responds with an authorization code and QuickBooks Company ID (also called Realm ID) on the redirect URI.
1 2 3 4 5 | public ActionResult Index() { string code = Request.QueryString["code"] ?? "none"; string realmId = Request.QueryString["realmId"] ?? "none"; } |
GetBearerTokenAsync
method of OAuth2Client
class to get OAuth2 tokens and expiry details as shown below. For the complete code example, go here.
1 2 3 4 5 6 7 8 9 10 11 | private async Task GetAuthTokensAsync(string code, string realmId) { var tokenResponse = await AppController.auth2Client.GetBearerTokenAsync(code); var access_token = tokenResponse.AccessToken; var access_token_expires_at = tokenResponse.AccessTokenExpiresIn; var refresh_token = tokenResponse.RefreshToken; var refresh_token_expires_at = tokenResponse.RefreshTokenExpiresIn; } |
Once the tokens are received, they can be used to make QuickBooks Online API calls. First, create a ServiceContext
object. A ServiceContext
object is created with an access token along with the realmId
and works as a context for the API request. This ServiceContext
object is then used in QueryService
to query for CompanyInfo
data. QueryService
can be used to execute any QuickBooks Online API supported query as a String in the parameter as shown below. Go here for a complete example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public ActionResult ApiCallService() { var principal = User as ClaimsPrincipal; OAuth2RequestValidator oauthValidator = new OAuth2RequestValidator(principal.FindFirst("access_token").Value); ServiceContext serviceContext = new ServiceContext(realmId, IntuitServicesType.QBO, oauthValidator); serviceContext.IppConfiguration.MinorVersion.Qbo = "23"; serviceContext.IppConfiguration.BaseUrl.Qbo = "https://sandbox-quickbooks.api.intuit.com/"; //This is sandbox Url. Change to Prod Url if you are using production QueryService<CompanyInfo> querySvc = new QueryService<CompanyInfo>(serviceContext); CompanyInfo companyInfo = querySvc.ExecuteIdsQuery("SELECT * FROM CompanyInfo").FirstOrDefault(); string output = JsonConvert.SerializeObject(companyInfo, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return View("ApiCallService", (object)output); } |