public static void SetTranslationToken(AdmAccessToken token) { if (token != null) TranslationToken = token; }
private void RaiseAccessTokenComplete(bool isSuccess, AdmAccessToken token) { if(AccessTokenComplete!=null) AccessTokenComplete(this, new TokenServiceCompleteEventArgs(isSuccess, token)); }
private void StartTranslationWithToken(AdmAccessToken token) { string translateUri = string.Format("http://api.microsofttranslator.com/v2/Http.svc/Translate?text={0}&from={1}&to={2}", HttpUtility.UrlEncode(_originalText), HttpUtility.UrlEncode(_sourceLanguage), HttpUtility.UrlEncode(_targetLanguage)); WebRequest translationRequest = HttpWebRequest.Create(translateUri); // We need to put our access token into the Authorization header // with "Bearer " preceeding it. string bearerHeader = "Bearer " + token.access_token; translationRequest.Headers["Authorization"] = bearerHeader; // Finally call our translation service translationRequest.BeginGetResponse(asyncResult => { try { HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); // Read the contents of the response into a string Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); string translationData = streamRead.ReadToEnd(); // Read the XML return from the translator // you can get a preview of this XML if you go to your Azure // account, click on My Data on the left and then on "Use" on // Microsoft Translator. run a trial query and then click the // XML button at the top of the query tool. // You'll need to add "System.XML.Linq" to your project to use XDocument XDocument translationXML = XDocument.Parse(translationData); string translationText = translationXML.Root.FirstNode.ToString(); RaiseTranslationComplete(_originalText, _sourceLanguage, _targetLanguage, translationText); } catch (WebException webExc) { RaiseTranslationFailed(webExc.Status.ToString()); } }, translationRequest); }
public TokenServiceCompleteEventArgs(bool isSuccess, AdmAccessToken token) { IsSuccess = isSuccess; TranslationToken = token; }