Skip to content

Instantly share code, notes, and snippets.

@sebastienros
Created October 9, 2019 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastienros/470c3621cd4fbda640d24502df6b7e37 to your computer and use it in GitHub Desktop.
Save sebastienros/470c3621cd4fbda640d24502df6b7e37 to your computer and use it in GitHub Desktop.
GraphQL query using Client Credentials flow in Orchard Core
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
httpClientHandler.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;
var httpClient = new HttpClient(httpClientHandler);
// https://developers.onelogin.com/openid-connect/api/client-credentials-grant
//curl -XPOST "https://<region>.onelogin.com/oidc/token" \
// -H "Authorization: Basic <base64 encoded client_id:client_secret>" \
// -H "Content-Type: application/x-www-form-urlencoded" \
// -d "grant_type=client_credentials"
string accessToken;
using (var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:5001/connect/token"))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes("theclientid:theclientsecret")));
request.Content = new StringContent("grant_type=client_credentials");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
using (var response = await httpClient.SendAsync(request))
{
var data = await response.Content.ReadAsStringAsync();
var doc = JsonDocument.Parse(data);
accessToken = doc.RootElement.GetProperty("access_token").GetString();
}
}
using (var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:5001/api/graphql?query=query%20MyQuery%20{%20blogPost%20{%20displayText%20}%20}"))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
using (var response = await httpClient.SendAsync(request))
{
var data = await response.Content.ReadAsStringAsync();
Console.WriteLine(data);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment