...
Info |
---|
The first thing you will need is a client Id and a client password, which you can request from the IIM team. |
...
Code Block |
---|
language | c# |
---|
title | C# HttpClient Code Example |
---|
linenumbers | true |
---|
collapse | true |
---|
|
static void Main(string[] args)
{
// Create client to get Identity Server Response
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Add required information to retrieve token (you will need to use your credentials)
var content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("scope", "sourceapi"),
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", "your_client_id"),
new KeyValuePair<string, string>("client_secret", "your_client_secret")
});
// Get response from a POST
var response = client.PostAsync("https://warden.data.uwaterloo.ca/connect/token", content).Result;
response.EnsureSuccessStatusCode();
// For our purposes we'll get the return as string (that is valid JSON) and then deserialize into an object
var jsonResponseString = response.Content.ReadAsStringAsync().Result;
var jsonObject = JsonConvert.DeserializeObject<JwtResponseBlock>(jsonResponseString);
client.Dispose();
// Query the API by setting the Authorization header value with the token we got
client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jsonObject.access_token);
// Retrieve Term informatino from API as an example
var termsJsonString = client.GetStringAsync("https://api.data.uwaterloo.ca/api/v1/terms").Result;
}
private class JwtResponseBlock
{
public string access_token { get; set; }
} |
PHP with helper library
For PHP please see this GitHub repository of an OpenID client. Example 4 using Client Credentials is what you are looking to use, you can also see how the implementation is done if you wish to code it yourself.
Ruby
Thank you to James from FEDS for this contribution.
...