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)); }
public SpeakGetter() { AdmAuthentication admAuth = new AdmAuthentication(); try { admToken = admAuth.GetAccessToken(); // Create a header with the access_token property of the returned token strToken = "Bearer " + admToken.access_token; } catch (WebException e) { WebServicesUtils.ProcessWebException(e); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
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; Console.WriteLine(string.Format("Renewed token for user: {0} is: {1}", this.clientId, this.token.access_token)); }