public static string GetTokenExplained()
{
//data to post to the token endpoint
var fields = new Dictionary<string, string>
{
{ "scope", "bimtoolkitapi" },
{ "grant_type", "client_credentials" }
};
//setup basic authentication
string creds = String.Format("{0}:{1}", _clientId, _clientSecret);
byte[] bytes = Encoding.ASCII.GetBytes(creds);
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(bytes));
//configure client
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = header;
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["Authority"] + "/connect/token");
//get response and parse as json
var response = client.PostAsync(string.Empty, new FormUrlEncodedContent(fields)).Result;
string raw = response.Content.ReadAsStringAsync().Result;
var json = JObject.Parse(raw);
//try to get the access token from the json response
JToken token;
if (json.TryGetValue("access_token", out token))
{
return token.ToString();
}
return "";
}