[問題] microsoft translator API

作者: kobfm005 (Billy)   2015-11-20 10:13:45
這是微軟的翻譯API網址https://goo.gl/eCr0Dp
最近想了解翻譯API,微軟給的程式碼貼上執行卻是錯的,會跑出
http://i.imgur.com/3gYPEjG.jpg
但我有加入system.dll,也是跑不出來
之後我再把最後一段原始程式碼http://i.imgur.com/dB2xHLZ.jpg
改成
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;
try {
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;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
執行畫面為http://i.imgur.com/pcGhbTF.jpg
請問我應該要如何改,腦袋已經打結了,希望大神們可以給我建議,謝謝。
作者: ryan10328 (大鳥)   2015-11-20 12:41:00
你要不要用post man先試試整個request的送出和檢視回頷你要不要用post man先試試整個request的送出和檢視回應的內容,再用程式來送看看request,建議也瞭解一下Oauth驗證流程,比較好一點。上面多回一次,手機有點怪怪的不好意思。我打開網址一看,他應該是走OAuth的驗證流程,因此你會需要申請一組client_id,client_secret等等的資訊來向伺服器交換一些資訊,讓你有權限能夠訪問他的API。如果上面這些你已經申請,然後程式裡面有塞,就可能是Request內容丟的有問題,或是其他原因,所以上面我有建議你使用PostMan(chrome外掛)來手動先做一次Request,確認你送的內容有得到正確回應,再使用程式來送,才不會抓不到到底問題在哪裡。
作者: kobfm005 (Billy)   2015-11-20 18:44:00
謝謝你,等等我再來試試

Links booklink

Contact Us: admin [ a t ] ucptt.com