DataConnector | ComponentOne
ADO.NET provider for QuickBooks Online / Authentication
In This Topic
    Authentication
    In This Topic

    OAuth is an open-standard authentication 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 authentication 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();
    }

    Supported Authentication methods for QuickBooks provider

    QuickBooks Online supports OAuth 2.0 authentication for connecting to external applications or services. This authentication method provides a secure and standardized way for applications to gain authorized access to QuickBooks Online resources without exposing sensitive user credentials. To authenticate using OAuth 2.0 with QuickBooks Online, you'll need to provide the OAuth client ID and client secret, along with the token endpoint for authentication. Additionally, you'll use the OAuth access token and refresh token for subsequent requests to access the user's data. This method ensures secure and seamless integration between QuickBooks Online and external applications while maintaining user privacy and security.

    To authenticate with QuickBooks Online using OAuth 2.0, you'll need to follow these steps:

    Connection properties used on each type of authentication

    OAuth2 Authentication

    1. OAuthClientId: Specifies the OAuth client ID for authentication.
    2. OAuthClientSecret: Specifies the OAuth client secret for authentication.
    3. OAuthTokenEndpoint: Specifies the OAuth token endpoint for authentication.
    4. OAuthAccessToken: Specifies the
    5. OAuthRefreshToken: Specifies the OAuth refresh token for authentication.
    6. CompanyId: Specifies the ID of the QuickBooks Online company to connect to.
    7. UseSandbox: Specifies whether to use the QuickBooks Online sandbox environment.