QuickBooks Online

How To Fetch Access Token From Quickbooks Online Using C#

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.

Faisal Pathan

Faisal Pathan is a founder of TheCodeHubs, .NET Project Manager/Team Leader, and C# Corner MVP. He has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET Core, ASP.NET MVC, AngularJS, Angular, React, NodeJS, Amazon S3, Web API, EPPlus, Amazon MWS, eBay Integration, SQL, Entity Framework, JavaScript, eCommerce Integration like Walmart, Tanga, Newegg, Group-on Store, etc. and Windows services.

View Comments

Share
Published by
Faisal Pathan

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago