Authorization

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.

To connect to a JSON server that requires OAuth authentication, provide the following properties:

C#
Copy Code
//JSON server protected by oauth 2.0 authentication
const string TokenEndpoint = @"http://****/****/****";
const string URI = @http://****/****/****;const string ClientId = @"yourid";
const string ClientSecret = @"yoursecretcode";
const string Scope = @"yourscope";
const string Username = @"yourusername";
const string Password = @"yourpassword";

In the following code, the OAuthClientId, OAuthClientSecret, and other attributes are set to fetch the data using properties of the C1JsonConnection class.

C#
Copy Code
static string connectionStringForOAuth = @$OAuth Token Endpoint={TokenEndpoint}; Uri={URI};
       OAuth Client Id={ClientId}; OAuth Client Secret={ClientSecret}; OAuth Scope={Scope};
       Username={Username}; Password={Password};                
static void SelectDocumentWithOAuth()
{
    Console.WriteLine("Query all Books via OAuth...");
    using (var con = new C1CSVConnection(connectionStringForOAuth))
    {
        con.Open();
        var table = con.GetSchema("columns", new string[] { "value" });
        ShowDataTable(table);
        var cmd = con.CreateCommand();
        cmd.CommandText = "Select * From value";
        var reader = cmd.ExecuteReader();
    }
}               
static void ShowDataTable(DataTable table, int length = 25)
{
    foreach (DataColumn col in table.Columns)
    {
        Console.Write("{0,-" + length + "}", col.ColumnName);
    }
    Console.WriteLine();

    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn col in table.Columns)
        {
            if (col.DataType.Equals(typeof(DateTime)))
                Console.Write("{0,-" + length + ":d}", row[col]);
            else if (col.DataType.Equals(typeof(decimal)))
                Console.Write("{0,-" + length + ":C}", row[col]);
            else
                Console.Write("{0,-" + length + "}", row[col]);
        }
        Console.WriteLine();
    }
}