No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Michael Block
Latest commit 71bbe06 May 1, 2018

Readme.md

C# Api Client Library with OAuth2

Features

  • Makes structured calls to the DigiKey API from .NET projects
  • Logs in users using the OAuth2 code flow

Basic Usage

var settings = ApiClientSettings.CreateFromConfigFile();
var client = new DigiKeyClient(settings);
var postResponse = await client.KeywordSearch("P5555-ND");
Console.WriteLine("response is {0}", postResponse);

Project Contents

  • ApiClient - Client Library that contains the code to manage a config file with OAuth2 settings and classes to do the OAuth2 call and an example call to DigiKey's KeywordSearch Api.
  • ApiClient.ConsoleApp - Console app to test out programmatic refresh of access token when needed and also check if access token failed to work and then refresh and try again.
  • OAuth2Service.ConsoleApp - Console app to create the initial access token and refresh token.

Getting Started

  1. Download the zip file containing the solution ApiClientSource
  2. You may need to Register an application in order to create your unique Client ID and Client Secret and OAuth Redirection URL, follow the steps (1 thru 4) from https://api-portal.digikey.com/start
  3. In the solution folder update apiclient.config with the ClientId, ClientSecret, and RedirectUri values from step 2.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="ApiClient.ClientId"" value="YOUR_CLIENT_ID_HERE" />
        <add key="ApiClient.ClientSecret" value="YOUR_CLIENT_SECRET_HERE" />
        <add key="ApiClient.RedirectUri"  value="YOUR_REDIRECT_URI_HERE" />
        <add key="ApiClient.AccessToken" value="" />
        <add key="ApiClient.RefreshToken" value="" />
        <add key="ApiClient.ExpirationDateTime" value="" />
    </appSettings>
</configuration>
  1. Run OAuth2Service.ConsoleApp to set the access token, refresh token and expiration date in webapi.config.
  2. Run ApiClient.ConsoleApp to get results from keyword search.

Asp.Net Example Web Application

This is just a simple Asp.Net application that does the OAuth2 calls to update or create the access and refresh tokens for the given client information.

The user will need to update their applicationhost.config file in .vs/config with the following line that sets the https binding.

<site name="AspNetExample.WebApp" id="5">
    .
    .
    <bindings>
        <binding protocol="https" bindingInformation="*:44300:localhost" />
    </bindings>
</site>

This will allow a https callback for the OAuth2 RedirectUri ex: https://localhost:44300/Home/FinishAuth