Rest Client example execute api POST and DELETE oauth url generate token

public class HttpClientHelper
{
private static HttpClient httpClient = new HttpClient();
private static RestClient client = new RestClient(“https://login.microsoftonline.com/c1eb5112-7946-4c9d-bc57-40040cfe3a91/”);

public static AuthenticationResultData GenerateToken(string clientId, string clientSecret, string resourceId)
{
string accessToken = string.Empty;
AuthenticationResultData result = new AuthenticationResultData();
try
{
//var client = new RestClient(“https://login.microsoftonline.com/{tenant-id}/”);

var request = new RestRequest(“oauth2/token”, Method.POST);

request.AddHeader(“Content-Type”, “application/x-www-form-urlencoded”);
request.AddParameter(“grant_type”, “client_credentials”);
request.AddParameter(“resource”, resourceId);
request.AddParameter(“client_id”, clientId);
request.AddParameter(“client_secret”, clientSecret);

IRestResponse response = client.Execute(request);
var content = response.Content;

if (response.IsSuccessful)
{
var jsonresult = JObject.Parse(response.Content);
result = JsonConvert.DeserializeObject(response.Content);
var token = (string)jsonresult[“access_token”];
accessToken = result.access_token;
}

}
catch (Exception ex)
{
TelemetryClient telemetry = new TelemetryClient();
telemetry.TrackTrace(“Exception occur GenerateToken ” + ex.Message);
accessToken = ex.Message;
}

return result;
}

public static async Task ExecuteApi(HttpMethod httpMethod, string apiUrl, string accessToken, string requestData)
{
HttpResponseMessage response = new HttpResponseMessage();
try
{
//HttpClient httpClient = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(httpMethod, apiUrl);

var httpContent = new StringContent(requestData, Encoding.UTF8, “application/json”);

request.Content = httpContent;

request.Headers.Authorization = new AuthenticationHeaderValue(“Bearer”, accessToken);

response = await httpClient.SendAsync(request);

}
catch (Exception ex)
{
TelemetryClient telemetry = new TelemetryClient();
telemetry.TrackTrace(“Exception occur ExecuteApi ” + ex.Message);
}
return response;
}

public static async Task DeleteDeviceApi(HttpMethod httpMethod, string apiUrl, string accessToken)
{
TelemetryClient telemetry = new TelemetryClient();
HttpResponseMessage response = new HttpResponseMessage();
try
{
HttpClient httpClient = new HttpClient();

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(“Bearer”, accessToken);

HttpRequestMessage request = new HttpRequestMessage(httpMethod, apiUrl);

request.Headers.Authorization = new AuthenticationHeaderValue(“Bearer”, accessToken);

response = await httpClient.DeleteAsync(apiUrl);
telemetry.TrackEvent(“Delete Device Succuess ” + apiUrl);
}
catch (Exception ex)
{

telemetry.TrackTrace(“Exception occur ExecuteApi ” + ex.Message);
}
return response;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s