OAuth is an open-standard authorization protocol that creates a platform for unlinked servers and services to allow authenticated access to data sources without sharing private credentials. OAuth is used in a wide variety of applications for user authentication. For more information on OAuth and types of credentials, refer to the OAuth Authorization topic.
QuickBooks Online authorization is slightly different compared to other providers provided by C1DataConnector. In QuickBooks Online, the DataConnector APIs do not automatically generate OAuthAccessToken and OAuthRefreshToken. After fetching OAuthAccessToken and OAuthRefreshToken from OAuth 2.0 Playground using the OAuthClientId and OAuthClientSecret you can complete the authentication to provide secured access to fetch data from the resource server. To connect to QuickBooks Online provide the following properties:
CS |
Copy Code |
---|---|
static string CompanyId = "**********"; static string OAuthClientId = @"**********"; static string OAuthClientSecret = @"*******"; static string OAuthAccessToken = @"*******"; static string OAuthRefreshToken = @"*********"; static string OAuthTokenEndpoint = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"; |
In the following code, the OAuthClient Id, OAuthClientSecret, and other attributes are set to fetch the data using properties of the C1QuickBooksOnlineConnectionstringBuilder class. UseSandbox is a Boolean that indicates if you are using a Sandbox account. Its default value is false.
CS |
Copy Code |
---|---|
LoadAuthentication(); //Configure Connection string C1QuickBooksOnlineConnectionStringBuilder builder = new C1QuickBooksOnlineConnectionStringBuilder { OAuthClientId = OAuthClientId, OAuthClientSecret = OAuthClientSecret, OAuthTokenEndpoint = OAuthTokenEndpoint, OAuthAccessToken = OAuthAccessToken, OAuthRefreshToken = OAuthRefreshToken, CompanyId = CompanyId, UseSandbox = true }; //Setup Connection using (C1QuickBooksOnlineConnection conn = new C1QuickBooksOnlineConnection(builder.ConnectionString)) { conn.OAuthTokenRefreshed += OAuthTokenRefreshed; //Tried fetching data from two different tables C1QuickBooksOnlineCommand comm = new C1QuickBooksOnlineCommand(conn, "Select * from Customers"); C1QuickBooksOnlineDataAdapter adapter = new C1QuickBooksOnlineDataAdapter(comm); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); //Display fetched data foreach (DataRow row in dataTable.Rows) { Console.WriteLine("{0}\t{1}\t{2}", row["CompanyName"], row["DisplayName"], row["Active"]); } Console.WriteLine("Connection created and read operation completed !!!"); } |
In the following code, the LoadAuthetication method saves the OAuthAccessToken and OAuthRefreshToken to be loaded in the connection string when the new access token is generated. The OAuthTokenRefreshed event is called to refresh the access token which has a validity of one hour using a valid OAuthRefreshToken.
CS |
Copy Code |
---|---|
private static void LoadAuthentication() { try { var arrAuth = File.ReadAllText(@"Authentication.txt").Split(';'); if (arrAuth[0] == CompanyId) { OAuthAccessToken = arrAuth[1]; OAuthRefreshToken = arrAuth[2]; } } catch { } } private static void OAuthTokenRefreshed(object sender, EventArgs e) { var conn = sender as C1QuickBooksOnlineConnection; var strAuthen = $"{conn.CompanyId};{conn.OAuthToken.AccessToken};{conn.OAuthToken.RefreshToken}"; File.WriteAllText(@"Authentication.txt", strAuthen); } static void Main(string[] args) { Console.WriteLine("Connecting to QuickBooksOnline !!!"); Connection(); } |