Skip to content

Using Authentication methods

Henrique Tedeschi edited this page Jun 11, 2020 · 1 revision

Details about how to obtain access tokens using OAuth 2.0

Implementation

For all the account-related usages, make sure you include the Account Model namespace:

using Tradier.Client.Models.Authentication;

Create an Access Token

Access tokens are the keys used for API access. These tokens should be protected like passwords! You can obtain an access token by exchanging an authorization code. This call is authenticated using Basic Authentication implemented in HTTP specification. Your application consumer key will serve as your username and the consumer secret the password.

Due to the OAuth specification, this API endpoint uses HTTP Basic Authentication. You can learn more about HTTP Basic Authentication on Wikipedia or directly reference the specification.

Token authentication = await client.Authentication.CreateAccessToken("<CODE>");

Exchange a Refresh Token

Refresh tokens can be exchanged for access tokens without a customer reauthorizing the application. These tokens should be protected like passwords! You will obtain a refresh token in the same response as an access token.

Due to the OAuth specification, this API endpoint uses HTTP Basic Authentication. You can learn more about HTTP Basic Authentication on Wikipedia or directly reference the specification.

Token authentication = await client.Authentication.RefreshAccessToken("<TOKEN>");


Example

using System.Threading.Tasks;
using Tradier.Client;
using Tradier.Client.Models.Authentication;

namespace MyConsoleApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            TradierClient client = new TradierClient("<TOKEN>");

            Token authentication = await client.Authentication.CreateAccessToken("<CODE>");
            Token authentication2 = await client.Authentication.RefreshAccessToken("<TOKEN>");

        }
    }
}