Last active
January 27, 2016 21:01
-
-
Save stackwalker/df37ada6c9285b0cb719 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.IO; | |
using System.Text; | |
using System.Net; | |
using Newtonsoft.Json; | |
namespace apisamples | |
{ | |
public class HealthwiseAgent | |
{ | |
public string GetBearerToken(string clientId, string clientSecret) | |
{ | |
var req = HttpWebRequest.Create("https://auth.test.test-products.local/v1/oauth2/token"); | |
string basicToken = string.Format("Basic {0}", | |
Convert.ToBase64String(Encoding.ASCII.GetBytes(clientId + ":" + clientSecret)) | |
); | |
req.Headers.Add("Authorization", basicToken); | |
req.ContentType = "application/json"; | |
req.Method = "POST"; | |
using (var writer = new StreamWriter(req.GetRequestStream())) | |
{ | |
writer.Write(@"{'grant_type' : 'client_credentials','scope' : '*','expires_in' : '86400'}"); | |
} | |
dynamic obj; | |
using (var res = (HttpWebResponse)req.GetResponse()) | |
using (var sr = new StreamReader(res.GetResponseStream())) | |
{ | |
//Should probably use a defined type to deserialize | |
obj = JsonConvert.DeserializeObject<dynamic>(sr.ReadToEnd()); | |
} | |
return obj.access_token.ToString(); | |
} | |
public string GetConsumer(string bearerToken, string consumerId) | |
{ | |
var req = HttpWebRequest.Create("https://consumer.test.test-products.local/v1/consumers/" + consumerId); | |
req.Headers.Add("Authorization", "Bearer " + bearerToken); | |
req.ContentType = "application/json"; | |
req.Method = "GET"; | |
dynamic obj; | |
using (var res = (HttpWebResponse)req.GetResponse()) | |
using (var sr = new StreamReader(res.GetResponseStream())) | |
{ | |
obj = JsonConvert.DeserializeObject<dynamic>(sr.ReadToEnd()); | |
} | |
return obj.ToString(); | |
} | |
} | |
} |
This file contains 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
static void Main(string[] args) | |
{ | |
var clientId = "topsecret"; | |
var clientSecret = "topsecret"; | |
var agent = new HealthwiseAgent(); | |
var bearer = agent.GetBearerToken(clientId, clientSecret); | |
Console.WriteLine(agent.GetConsumer(bearer, "christest1")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment