GetAppIdToken Method

Returns a tokenized AppID which can be used as the AppID parameter in any method (except GetAppIdToken()). A tokenized AppID should be used when the appID must be kept secret.

Syntax

URI

http://api.microsofttranslator.com/V2/Http.svc/GetAppIdToken

Parameters

Parameter Description

appId

A string containing AppID.

minRatingRead

An int to define the minimum rating of the translations to be returned. The recommended minRatingRead is 5 as this will only include machine translations and authority approved translations.

maxRatingWrite

An int to define the maximum rating that a user can write with using this token. The recommended maxRatingWrite is from 1 to 4 for anonymous users, and from 6 to 10 for authoritative users whom you trust. Translations with ratings of 5 or higher will replace automatic translations when calling Translate() or TranslateArray() with the same appId. Translations with ratings lower then 5 will only be returned when calling GetTranslations() or GetTranslationsArray().

expireSeconds

An int that defines the duration in seconds from now that the token is valid. The value can be between 1 and 86400 (24 hours).

Return value

A string representing the tokenized identification for the calling application.

Example
                                 
         public static void GetAppIdToken()
        {
            string appId = "";//go to http://msdn.microsoft.com/en-us/library/ff512386.aspx to obtain AppId.

            string uri =
                "http://api.microsofttranslator.com/v2/Http.svc/GetAppIdToken?appId="
                        + appId + "&minRatingRead=5&maxRatingWrite=3&expireSeconds=300";

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);

            WebResponse response = null;
            try
            {
                response = httpWebRequest.GetResponse();

                using (Stream stream = response.GetResponseStream())
                {
                    System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                    string tokenizedAppId = (string)dcs.ReadObject(stream);
                    Console.WriteLine("The tokenized AppId is: '" + tokenizedAppId + "'.");
                }
            }
            catch (WebException e)
            {
                ProcessWebException(e, "Failed to get AppId token languages");
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }
        }

        private static void ProcessWebException(WebException e, string message)
        {
            Console.WriteLine("{0}: {1}", message, e.ToString());

            // Obtain detailed error information
            string strResponse = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)e.Response)
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
                    {
                        strResponse = sr.ReadToEnd();
                    }
                }
            }
            Console.WriteLine("Http status code={0}, error message={1}", e.Status, strResponse);
        }