Fork me on GitHub

在Application中集成Microsoft Translator服务之获取访问令牌

访问令牌类似于web中的cookie

我在这里画了一张图来展示业务逻辑

在我们调用microsoft translator server之前需要获得令牌,而且这个令牌的有效期为10分钟。下表列出所需的参数和对于的说明    

参数 描述
client_id 必须的,指你在Azuzre注册应用程序的客户端ID
client_secret 必须的,指你在Azuzre注册应用程序的客户端密钥
scope   必须的,默认使用http://api.microsofttranslator.com     
grant_type 必须的,默认使用"client_credentials" 

 

 

 

 

 

 

Azure返回给我们的也是四个属性,并且以json的形势返回,下表列出属性和对应的描述

属性 描述
access_token 验证您可以访问 Microsoft 翻译 API 访问令牌
token_type 访问令牌的格式。
expires_in 访问令牌无效的秒数
scope 此标记为有效的域。对于微软翻译 API,域名是http://api.microsofttranslator.com

 

 

 

 

 

Bing AppID 机制已废弃,不再受支持。如上文所述,您必须获取访问令牌来使用微软翻译 API。访问令牌是更安全,并且访问的令牌在规定时间内可以被后续程序调用

注意以下两点

1.使用"Bearer"+access_toke的值作为访问令牌

2.令牌有效期为10分钟,过期了需要重新申请

下面来使用类AdmAccessToken,使用它来存储Azure返回的属性

 1 [DataContract]
 2     class AdmAccessToken
 3     {
 4         [DataMember]
 5         public string access_token { get; set; }
 6 
 7         [DataMember]
 8         public string token_type { get; set; }
 9 
10         [DataMember]
11         public string expires_in { get; set; }
12 
13         [DataMember]
14         public string scope { get; set; }
15 
16     }

这个类的四个属性的Azure返回的属性相对应,并加入[DataContract][DataMember]方面对返回的json序列化成AdmAccessToken的对象

接下来使用一个类AdmAuthentication,使用它来帮助我们完成令牌的请求

 public class AdmAuthentication
    {
        public static readonly string DatamarketAccessUri = "https://datamarket.accesscontrol.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())
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken));
                //Get deserialized object from JSON stream
                AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
                return token;
            }

        }
}

  好了,现在完成了令牌的请求,接下来可以利用令牌获取翻译服务了

 

posted @ 2016-10-02 15:43  王起帆  阅读(750)  评论(0编辑  收藏  举报