In this article, we will learn how to fetch access token from Quickbooks Online in ASP.NET MVC Web application using SDK.
if you didn’t know how to integrate Quickbooks online using SDK, then you can find it here.
- Define variables as below in controller,
public static string RealmId = ""; public static string Access_token = ""; public static string Refresh_token = ""; public static string clientid = ConfigurationManager.AppSettings["clientid"]; public static string clientsecret = ConfigurationManager.AppSettings["clientsecret"]; public static string redirectUrl = ConfigurationManager.AppSettings["redirectUrl"]; public static string environment = ConfigurationManager.AppSettings["appEnvironment"]; public static string QboBaseUrl = ConfigurationManager.AppSettings["QboBaseUrl"];
- Create and configure an OAuth2Client object that defines these parameters as below, in controller
//Instantiate object public static OAuth2Client auth2Client = new OAuth2Client(clientid, clientsecret, redirectUrl, environment);
- Now we have to create one method that will prepare URL to initiate the authentication and authorization process and redirect the user to Intuit’s OAuth 2.0 server.
- This step is required when your application first needs to access the user’s data.
- we have to pass scopes depends on our application need, for accessing QuickBooks Online API we need to use “Accounting” scope.
public ActionResult InitiateAuth() { List<OidcScopes> scopes = new List<OidcScopes>(); scopes.Add(OidcScopes.Accounting); string authorizeUrl = auth2Client.GetAuthorizationURL(scopes); return Redirect(authorizeUrl); }
- This method will redirect us to Quickbooks online server for authentication and authorization.
- After Successfully login user will be redirected to “redirectUrl” which we define in Web.config. this “redirectUrl” also had to be added on Quickbooks online Redirect URIs.
- We have to make one method for redirectUrl, which will get a response and generate an access token.
- In response, we will get code and realmId, by using code in auth2Client.GetBearerTokenAsync(code) we can get access token, refresh token, and then we can store it in a global variable or anywhere you want to.
- redirectUrl method code is as below
public async Task<ActionResult> QboCallBack() { string code = Request.QueryString["code"] ?? "none"; string realmId = Request.QueryString["realmId"] ?? "none"; if (code != "none" && realmId != "none") { RealmId = realmId; var tokenResponse = await auth2Client.GetBearerTokenAsync(code); if (!string.IsNullOrWhiteSpace(tokenResponse.AccessToken)) { Access_token = tokenResponse.AccessToken; } if (!string.IsNullOrWhiteSpace(tokenResponse.RefreshToken)) { Refresh_token = tokenResponse.RefreshToken; } } return RedirectToAction("index", "Home"); }
Now we can use access token and realmId for using QuickBooks Online API and refresh token for refreshing/requesting a new access token.
I store access token, realmId and refresh token in global variables, we can store it in session, cookies or database according to your need, and then use it as needed for using QuickBooks Online API.
You can find the more detailed documentation here.
Thanks, this was very helpful!
what are the nuget libraries used in here?
you can use IppDotNetSdkForQuickBooksApiV3.
i wouldnot like to use OAuth2Client, so how to connect qbo
You have to use OAuth2Client for
1. Authorization using authOAuth 2.0
2. Access all Quickbooks API using SDK.
another option is authOAuth 1.0 but Quickbooks discontinued all support for OAuth 1.0. After December 17th, 2019.
You can try with Rest API instead of SDK