The code sample included in the article shows how to do this. You have to dig around it a bit to figure out how it all comes together, but in a nutshell they use a WebRequest
with a method of "POST" and pass the 4 required values in that POST. I suggest looking at the sample code, but to distill it down to its basic elements it might look something like this:
string request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(clientSecret));
WebRequest webRequest = WebRequest.Create("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13");
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(request);
webRequest.ContentLength = bytes.Length;
using (Stream outputStream = webRequest.GetRequestStream())
{
outputStream.Write(bytes, 0, bytes.Length);
}
using (WebResponse webResponse = webRequest.GetResponse())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken));
//Get deserialized object from JSON stream
AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
return token;
}
The bulk of the above code is in private AdmAccessToken HttpPost(string DatamarketAccessUri, string requestDetails)
method in the sample.
The AdmAccessToken
is a class defined in the sample that has properties for the returned values:
access_token token_type expires_in scope
It's a very comprehensive sample, and takes a small amount of digging to get the relevant parts, but its all there as near as I can tell. The snippet I posted simply combined a couple of things that were done elsewhere in the sample for simplicity.