DataConnector
認証
ADO.NET provider for QuickBooks Online > 認証

OAuth is an open-standard authorization protocol that creates a platform for unlinked servers and services to allow authenticated access to the 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 OAuth Authorization topic.

QuickBooks Online authorization is slightly different compared to other providers provided by C1DataConnector. In QuickBooks Online, the DataConnector APIs does 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 resource server. To connect to a QuickBooks Online provide the following properties:

CS
コードのコピー
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 C1QuickBooksOnlineConnectionstringBuilder class. UseSandbox is a Boolean that indicates if you are using a Sandbox account. Its default value is false.

CS
コードのコピー
LoadAuthentication();

//接続文字列を設定します。
C1QuickBooksOnlineConnectionStringBuilder builder = new C1QuickBooksOnlineConnectionStringBuilder
{
    OAuthClientId = OAuthClientId,
    OAuthClientSecret = OAuthClientSecret,
    OAuthTokenEndpoint = OAuthTokenEndpoint,
    OAuthAccessToken = OAuthAccessToken,
    OAuthRefreshToken = OAuthRefreshToken,
    CompanyId = CompanyId,
    UseSandbox = true
};

//接続を設定します。
using (C1QuickBooksOnlineConnection conn = new C1QuickBooksOnlineConnection(builder.ConnectionString))
{
    conn.OAuthTokenRefreshed += OAuthTokenRefreshed;

    // 2 つの異なるテーブルからデータを取得しようとしました。
    C1QuickBooksOnlineCommand comm = new C1QuickBooksOnlineCommand(conn, "Select * from Customers");

    C1QuickBooksOnlineDataAdapter adapter = new C1QuickBooksOnlineDataAdapter(comm);
    DataTable dataTable = new DataTable();
    adapter.Fill(dataTable);

    //取得したデータを表示します。
    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
コードのコピー
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();
}