Created
January 26, 2016 21:02
-
-
Save stackwalker/683da369e91c9df81e0c 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
public string GetBearerToken(string clientID, string clientSecret) | |
{ | |
Uri authUri = new Uri("https://auth.test.test-products.local/v1/oauth2/token"); | |
HttpWebRequest req = HttpWebRequest.CreateHttp(authUri); | |
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"; | |
byte[] bytes = Encoding.ASCII.GetBytes(@"{'grant_type' : 'client_credentials','scope' : '*','expires_in' : '86400'}"); | |
req.ContentLength = bytes.Length; | |
var s = req.GetRequestStream(); | |
s.Write(bytes, 0, bytes.Length); | |
s.Close(); | |
dynamic obj; | |
using (var res = req.GetResponse()) | |
using (var rs = res.GetResponseStream()) | |
using (var rdr = new StreamReader(rs)) | |
{ | |
obj = JsonConvert.DeserializeObject<dynamic>(rdr.ReadToEnd()); | |
} | |
return obj.access_token.ToString(); | |
} | |
public string GetConsumer(string bearer, string consumerId) | |
{ | |
HttpWebRequest req = | |
HttpWebRequest.CreateHttp(new Uri("https://consumer.test.test-products.local/v1/consumers/" + consumerId)); | |
req.Headers.Add("Authorization", bearer); | |
req.Method = "GET"; | |
dynamic obj; | |
using (var res = req.GetResponse()) | |
using (var rs = res.GetResponseStream()) | |
using (var rdr = new StreamReader(rs)) | |
{ | |
obj = JsonConvert.DeserializeObject<dynamic>(rdr.ReadToEnd()); | |
} | |
return obj.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment