Created
August 24, 2019 15:30
-
-
Save mpetrinidev/dca981d2dfbc3ecf21b040829a2c0b8d to your computer and use it in GitHub Desktop.
Call Github Graphql Api using c#
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 async Task Main(string[] args) | |
{ | |
var httpClient = new HttpClient | |
{ | |
BaseAddress = new Uri("https://api.github.com/graphql") | |
}; | |
httpClient.DefaultRequestHeaders.Add("User-Agent", "MyConsoleApp"); | |
string basicValue = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Configs.GithubAccount}:{Configs.PersonalToken}")); | |
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicValue); | |
var queryObject = new | |
{ | |
query = @"query { | |
viewer { | |
login | |
} | |
}", | |
variables = new { } | |
}; | |
var request = new HttpRequestMessage | |
{ | |
Method = HttpMethod.Post, | |
Content = new StringContent(JsonConvert.SerializeObject(queryObject), Encoding.UTF8, "application/json") | |
}; | |
dynamic responseObj; | |
using (var response = await httpClient.SendAsync(request)) | |
{ | |
response.EnsureSuccessStatusCode(); | |
var responseString = await response.Content.ReadAsStringAsync(); | |
responseObj = JsonConvert.DeserializeObject<dynamic>(responseString); | |
} | |
Console.WriteLine(responseObj.data.viewer.login); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment