在Application中整合Microsoft Translator服務之擷取存取權杖

來源:互聯網
上載者:User

標籤:

我在這裡畫了一張圖來展示商務邏輯

在我們調用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;            }        }}

  好了,現在完成了令牌的請求,接下來可以利用令牌擷取翻譯服務了

 

在Application中整合Microsoft Translator服務之擷取存取權杖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.