Created
July 26, 2017 16:52
-
-
Save aramkoukia/0250f8d98aabbb0ef1e38fd82d95b57e to your computer and use it in GitHub Desktop.
Get Hub API Token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var baseUrl = "http://YOUR_HOST_NAME/"; | |
| var httpClientHelper = new HttpClientHelper(); | |
| var tokenRequest = new List<KeyValuePair<string, string>>() | |
| { | |
| new KeyValuePair<string, string>("grant_type","password"), | |
| new KeyValuePair<string, string>("username","YOUR_USER_NAME"), | |
| new KeyValuePair<string, string>("password","YOUR_PASSWORD") | |
| }; | |
| var token = httpClientHelper.PostAsync(baseUrl + "/account/token", "", tokenRequest).Result; | |
| var responseContent = token.Content.ReadAsStringAsync().Result; | |
| dynamic data = JObject.Parse(responseContent); | |
| Console.WriteLine((string)data.access_token); | |
| Console.ReadLine(); | |
| } | |
| } | |
| public class HttpClientHelper | |
| { | |
| private static readonly HttpClient httpClient; | |
| static HttpClientHelper() | |
| { | |
| httpClient = new HttpClient(); | |
| } | |
| public async Task<HttpResponseMessage> PostAsync(string requestUri, string authHeader, List<KeyValuePair<string, string>> content) | |
| { | |
| HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri); | |
| request.Headers.Authorization = new AuthenticationHeaderValue("bearer", authHeader); | |
| request.Content = new FormUrlEncodedContent(content); | |
| var response = await httpClient.SendAsync(request); | |
| return response; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment