Created
March 12, 2014 14:29
-
-
Save hongruiqi/9508078 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
string clientID = "<Your ClientID>"; | |
string clientSecret = "<Your Client Secret>"; | |
String strTranslatorAccessURI = | |
"https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; | |
String strRequestDetails = | |
string.Format("grant_type=client_credentials&client_id={0}&client_secret={1} &scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientID), | |
HttpUtility.UrlEncode(clientSecret)); | |
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(strTranslatorAccessURI); | |
webRequest.ContentType = "application/x-www-form-urlencoded"; | |
webRequest.Method = "POST"; | |
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(strRequestDetails); | |
webRequest.ContentLength = bytes.Length; | |
using (System.IO.Stream outputStream = webRequest.GetRequestStream()) | |
{ | |
outputStream.Write(bytes, 0, bytes.Length); | |
} | |
System.Net.WebResponse webResponse = webRequest.GetResponse(); | |
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new | |
System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(AdmAccessToken)); | |
AdmAccessToken token = | |
(AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream()); | |
string headerValue = "Bearer " + token.access_token; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment