AccessTokenRequestSample.ConsoleApplication/Program.cs
/// <summary>
/// Gets an Access Token from the given token endpoint URI using the given Client ID and Client Secret
/// </summary>
/// <param name="tokenEndpointUri">Token Endpoint URI</param>
/// <param name="clientId">Client ID</param>
/// <param name="clientSecret">Client Secret</param>
/// <param name="scope">Scope</param>
/// <returns>String</returns>
Gets an Access Token from the given token endpoint URI using the given Client ID and Client Secret
Gets an Access Token from the given token endpoint URI using the given Client ID and Client Secret
private static string RequestAccessToken(string tokenEndpointUri, string clientId, string clientSecret, string scope)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(tokenEndpointUri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
            string encodedClientCredentials = EncodeClientCredentials(clientId, clientSecret);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encodedClientCredentials);
            FormUrlEncodedContent encodedContent = EncodeFormContent(scope);
            CancellationToken cancellationToken = default(CancellationToken);
            HttpResponseMessage response = client.PostAsync(string.Empty, encodedContent, cancellationToken).Result;
            return response.Content.ReadAsStringAsync().Result;
        }
private static string RequestAccessToken ( string tokenEndpointUri , string clientId , string clientSecret , string scope ) { HttpClient client = new HttpClient ( ) ; client . BaseAddress = new Uri ( tokenEndpointUri ) ; client . DefaultRequestHeaders . Accept . Clear ( ) ; client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( " " ) ) ; string encodedClientCredentials = EncodeClientCredentials ( clientId , clientSecret ) ; client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( " " , encodedClientCredentials ) ; FormUrlEncodedContent encodedContent = EncodeFormContent ( scope ) ; CancellationToken cancellationToken = default ( CancellationToken ) ; HttpResponseMessage response = client . PostAsync ( string . Empty , encodedContent , cancellationToken ) . Result ; return response . Content . ReadAsStringAsync ( ) . Result ; }
Gets an Access Token from the given token endpoint URI using the given Client ID and Client Secret
Gets an Access Token from the given token endpoint URI using the given Client ID and Client Secret
// Required for .NET 4.5.1 // SetSecurityProtocol();
None
tokenEndpointUri
string
Token Endpoint URI
Token Endpoint URI
clientId
string
None
clientSecret
string
None
scope
string
None