Exemplo n.º 1
0
 private void RenewAccessToken()
 {
     AdmAccessToken newAccessToken = HttpPost(DatamarketAccessUri, this.request);
     //swap the new token with old one
     //Note: the swap is thread unsafe
     this.token = newAccessToken;
     Trace.WriteLine(string.Format("Renewed token for user: {0} is: {1}", this.clientId, this.token.access_token));
 }
Exemplo n.º 2
0
 public AdmAuthentication(string clientId, string clientSecret)
 {
     this.clientId = clientId;
     this.clientSecret = clientSecret;
     //If clientid or client secret has special characters, encode before sending request
     this.request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(clientSecret));
     this.token = HttpPost(DatamarketAccessUri, this.request);
     //renew the token every specfied minutes
     accessTokenRenewer = new Timer(new TimerCallback(OnTokenExpiredCallback), this, TimeSpan.FromMinutes(RefreshTokenDuration), TimeSpan.FromMilliseconds(-1));
 }
Exemplo n.º 3
0
 public AdmAuthentication(string clientId, string clientSecret)
 {
     this.clientId     = clientId;
     this.clientSecret = clientSecret;
     //If clientid or client secret has special characters, encode before sending request
     this.request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(clientSecret));
     this.token   = HttpPost(DatamarketAccessUri, this.request);
     //renew the token every specfied minutes
     accessTokenRenewer = new Timer(new TimerCallback(OnTokenExpiredCallback), this, TimeSpan.FromMinutes(RefreshTokenDuration), TimeSpan.FromMilliseconds(-1));
 }
Exemplo n.º 4
0
        private AdmAccessToken HttpPost(string DatamarketAccessUri, string requestDetails)
        {
            //Prepare OAuth request
            WebRequest webRequest = WebRequest.Create(DatamarketAccessUri);

            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method      = "POST";
            byte[] bytes = Encoding.ASCII.GetBytes(requestDetails);
            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);
            }
        }
Exemplo n.º 5
0
        private string GetAccessToken()
        {
            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(_secret));

            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());

            return(token.access_token);
        }