Getting access tokens for integrating Microsoft Translator Services in application

Source: Internet
Author: User
Tags urlencode

I've drawn a picture here to show the business logic

Before we call Microsoft Translator Server, we need to get a token, and this token is valid for 10 minutes. The following table lists the required parameters and the description for the

Parameters Describe
client_id Required, refers to the client ID of the application you registered in Azuzre
Client_secret Required, refers to the client key that you registered the application in Azuzre
Scope required, default use of http://api.microsofttranslator.com
Grant_type Required, use "Client_credentials" by default

What Azure returns to us is also four properties, and returns as JSON, with the following table listing properties and corresponding descriptions

Property Describe
Access_token Verify that you can access the Microsoft translation API access token
Token_type The format of the access token.
Expires_in The number of seconds the access token is invalid
Scope This token is a valid domain. For the Microsoft Translator API, the domain name is http://api.microsofttranslator.com.

The Bing AppID mechanism is deprecated and is no longer supported. As mentioned above, you must obtain an access token to use the Microsoft translation API. Access tokens are more secure, and access tokens can be called by subsequent programs within a specified time

Note the following two points

1. Use the value "Bearer" +Access_toke as the access token

2. The token is valid for 10 minutes and must be re-applied if it expires

Use the class Admaccesstoken below to store the properties returned by Azure

1 [DataContract]2     classAdmaccesstoken3     {4 [DataMember]5          Public stringAccess_token {Get;Set; }6 7 [DataMember]8          Public stringToken_type {Get;Set; }9 Ten [DataMember] One          Public stringexpires_in {Get;Set; } A  - [DataMember] -          Public stringScope {Get;Set; } the  -}

The four properties of this class correspond to the properties returned by Azure, and add [Datacontract][datamember] aspects to the returned JSON serialized object as Admaccesstoken

Next use a class admauthentication, which is used to help us complete the request for the token

 public class Admauthentication {public static readonly string Datamarketaccessuri = "Https://datamarket.access        Control.windows.net/v2/oauth2-13 ";        private string ClientId {get; set;}        private string Clientsecret {get; set;}        Private string Scope {get; set;}        private string Grant_type {get; set;}        Private string Request {get; set;}        Public Admaccesstoken token {get; set;}            Public Admauthentication (String clientId, String clientsecret) {This.clientid = ClientId;            This.clientsecret = Clientsecret; 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); } 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 ()) {datacontractjsonserial                Izer serializer = new DataContractJsonSerializer (typeof (Admaccesstoken)); Get deserialized object from JSON stream Admaccesstoken token = (admaccesstoken) serializer.                ReadObject (WebResponse.GetResponseStream ());            return token; }        }}

OK, now that the token request has been completed, you can then use the token to get the translation service.

Getting access tokens for integrating Microsoft Translator Services in application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.