想用bing 翻译api做个小程序,ID,密钥什么的也申请好了,从MSDN上弄了个范例程序,结果只能返回一种错误,如下图我无论用MSDN中的哪个范例,或那种方式(SOAP、HTTP)全都是这一个返回,我就真是无了奈了。以下是我的全部源程序,除了认证信息,工程名跟MSDN上的一样(msdn:http://msdn.microsoft.com/en-us/library/ff512437.aspx),使用的话需要引用system.web还有添加 添加服务引用:http://api.microsofttranslator.com/V2/Soap.svc 请各位高手帮忙解答下到底有什么问题,谢谢这句里面是认证信息,ID和密钥
AdmAuthentication admAuth = new AdmAuthentication("66e0402b-1a0a-4487-8e0b-8684f14b9512", "awn3kkoNX5SFP3Apo9C0cMMfnHIIALKaynpg6JpDVOc=");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Web;
using System.ServiceModel.Channels;
using System.ServiceModel;namespace soap
{
class Program
{
static void Main(string[] args)
{
AdmAccessToken admToken;
string headerValue;
//Get Client Id and Client Secret from https://dataet.azure.com/developer/applications/
//Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx)
AdmAuthentication admAuth = new AdmAuthentication("66e0402b-1a0a-4487-8e0b-8684f14b9512", "awn3kkoNX5SFP3Apo9C0cMMfnHIIALKaynpg6JpDVOc=");
try
{
admToken = admAuth.GetAccessToken();
DateTime tokenReceived = DateTime.Now;
// Create a header with the access_token property of the returned token
headerValue = "Bearer " + admToken.access_token;
TranslateMethod(headerValue);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
private static void TranslateMethod(string authToken)
{
// Add TranslatorService as a service reference, Address:http://api.microsofttranslator.com/V2/Soap.svc
ServiceReference1.LanguageServiceClient client = new ServiceReference1.LanguageServiceClient();
//Set Authorization header before sending the request
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Method = "POST";
httpRequestProperty.Headers.Add("Authorization", authToken); // Creates a block within which an OperationContext object is in scope.
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
string sourceText = "<UL><LI>Use generic class names. <LI>Use pixels to express measurements for padding and margins. <LI>Use percentages to specify font size and line height. <LI>Use either percentages or pixels to specify table and container width. <LI>When selecting font families, choose browser-independent alternatives. </LI></UL>";
string translationResult;
//Keep appId parameter blank as we are sending access token in authorization header.
translationResult = client.Translate("", sourceText, "en", "de", "text/html", "general");
Console.WriteLine("Translation for source {0} from {1} to {2} is", sourceText, "en", "de");
Console.WriteLine(translationResult);
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
} [DataContract]
public class AdmAccessToken
{
[DataMember]
public string access_token { get; set; }
[DataMember]
public string token_type { get; set; }
[DataMember]
public string expires_in { get; set; }
[DataMember]
public string scope { get; set; }
} public class AdmAuthentication
{
public static readonly string DataetAccessUri = "https://dataet.accesscontrol.windows.net/v2/OAuth2-13";
private string clientId;
private string cientSecret;
private string request; public AdmAuthentication(string clientId, string clientSecret)
{
this.clientId = clientId;
this.cientSecret = clientSecret;
//If clientid or client secret has special characters, encode before sending request
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));
} public AdmAccessToken GetAccessToken()
{
return HttpPost(DataetAccessUri, this.request);
} private AdmAccessToken HttpPost(string DataetAccessUri, string requestDetails)
{
//Prepare OAuth request
WebRequest webRequest = WebRequest.Create(DataetAccessUri);
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;
}
}
}
}
bingapi
AdmAuthentication admAuth = new AdmAuthentication("66e0402b-1a0a-4487-8e0b-8684f14b9512", "awn3kkoNX5SFP3Apo9C0cMMfnHIIALKaynpg6JpDVOc=");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Web;
using System.ServiceModel.Channels;
using System.ServiceModel;namespace soap
{
class Program
{
static void Main(string[] args)
{
AdmAccessToken admToken;
string headerValue;
//Get Client Id and Client Secret from https://dataet.azure.com/developer/applications/
//Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx)
AdmAuthentication admAuth = new AdmAuthentication("66e0402b-1a0a-4487-8e0b-8684f14b9512", "awn3kkoNX5SFP3Apo9C0cMMfnHIIALKaynpg6JpDVOc=");
try
{
admToken = admAuth.GetAccessToken();
DateTime tokenReceived = DateTime.Now;
// Create a header with the access_token property of the returned token
headerValue = "Bearer " + admToken.access_token;
TranslateMethod(headerValue);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
private static void TranslateMethod(string authToken)
{
// Add TranslatorService as a service reference, Address:http://api.microsofttranslator.com/V2/Soap.svc
ServiceReference1.LanguageServiceClient client = new ServiceReference1.LanguageServiceClient();
//Set Authorization header before sending the request
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Method = "POST";
httpRequestProperty.Headers.Add("Authorization", authToken); // Creates a block within which an OperationContext object is in scope.
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
string sourceText = "<UL><LI>Use generic class names. <LI>Use pixels to express measurements for padding and margins. <LI>Use percentages to specify font size and line height. <LI>Use either percentages or pixels to specify table and container width. <LI>When selecting font families, choose browser-independent alternatives. </LI></UL>";
string translationResult;
//Keep appId parameter blank as we are sending access token in authorization header.
translationResult = client.Translate("", sourceText, "en", "de", "text/html", "general");
Console.WriteLine("Translation for source {0} from {1} to {2} is", sourceText, "en", "de");
Console.WriteLine(translationResult);
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
} [DataContract]
public class AdmAccessToken
{
[DataMember]
public string access_token { get; set; }
[DataMember]
public string token_type { get; set; }
[DataMember]
public string expires_in { get; set; }
[DataMember]
public string scope { get; set; }
} public class AdmAuthentication
{
public static readonly string DataetAccessUri = "https://dataet.accesscontrol.windows.net/v2/OAuth2-13";
private string clientId;
private string cientSecret;
private string request; public AdmAuthentication(string clientId, string clientSecret)
{
this.clientId = clientId;
this.cientSecret = clientSecret;
//If clientid or client secret has special characters, encode before sending request
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));
} public AdmAccessToken GetAccessToken()
{
return HttpPost(DataetAccessUri, this.request);
} private AdmAccessToken HttpPost(string DataetAccessUri, string requestDetails)
{
//Prepare OAuth request
WebRequest webRequest = WebRequest.Create(DataetAccessUri);
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;
}
}
}
}
bingapi
看后半段
这里应该是:
"Bearer" + " " + admToken.access_token;
这里应该是:
"Bearer" + " " + admToken.access_token; 这个没有任何效果。。
要添加一个Service Reference:TranslatorService