问题描述
我正在尝试将SOAP服务与Access Token一起使用。
Hi, I'm trying to use SOAP services with Access Token.
Using client = New MTService.LanguageServiceClient()
Using scope As New OperationContextScope(client.InnerChannel)
Dim header As MessageHeader = MessageHeader.CreateHeader("Authorization",
String.Empty, tokenValue)
OperationContext.Current.OutgoingMessageHeaders.Add(header)
Dim translation = client.Translate(String.Empty, "Come utilizzare
la classe System.IO.File.",
"it", "en", "text/plain", "general")
End Using
End Using
其中:
LanguageServiceClient是Web服务提供客户端;
$
tokenValue是使用GetAppIdToken方法检索的访问令牌(使用appId)
where:
LanguageServiceClient is the web service provy client;
tokenValue is the access token retrieved using GetAppIdToken method (with appId)
当我运行此时代码,我接收异常(FaultException):
When I run this code, I receive an exception (a FaultException):
" ArgumentOutOfRangeException:参数'appId'必须至少包含'16'字符参数名称:appId:ID = 3835.V2_Soap.Translate.3DD69CCA" ;
"ArgumentOutOfRangeException: the parameter 'appId' must contain at least '16' characters Parameter name: appId : ID=3835.V2_Soap.Translate.3DD69CCA"
这是请求的标题:
< s:Envelope xmlns:s =" http://schemas.xmlsoap.org/soap/envelope/ ">
&NBSP; < s:标题>
&NBSP; &NBSP;&NBSP; <授权>访问令牌< /授权>
< / s:标题>
< s:正文>
&NBSP; &NBSP;&NBSP; <翻译xmlns =" ">
&NBSP; &NBSP; &NBSP; &NBSP;&NBSP; < appId />
&NBSP; &NBSP; &NBSP; &NBSP;&NBSP; < text>来自utilizzare la classe System.IO.File。< / text>
&NBSP; &NBSP; &NBSP; &NBSP;&NBSP; < from> it< / from>
&NBSP; &NBSP; &NBSP; &NBSP;&NBSP; < to> zh< / to>
&NBSP; &NBSP; &NBSP; &NBSP;&NBSP; < contentType> text / plain< / contentType>
&NBSP; &NBSP; &NBSP; &NBSP;&NBSP; < category> general< / category>
&NBSP; &NBSP;&NBSP; < /翻译>
< / s:正文>
< / s:信封>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Authorization> access token </Authorization>
</s:Header>
<s:Body>
<Translate xmlns="http://api.microsofttranslator.com/V2">
<appId/>
<text>Come utilizzare la classe System.IO.File.</text>
<from>it</from>
<to>en</to>
<contentType>text/plain</contentType>
<category>general</category>
</Translate>
</s:Body>
</s:Envelope>
标题是否正确?为什么我在appId上收到错误(如果使用访问令牌,appId不是强制性的)?
Is the header correct? Why did I receive the error on the appId (appId isn't mandatory if using access token)?
谢谢你,
Max
Thanks
Max
推荐答案
这是正确的代码:
Try
Dim httpRequestProperty = New HttpRequestMessageProperty()
httpRequestProperty.Method = "POST"
httpRequestProperty.Headers.Add("Authorization", String.Format("Bearer
{0}", AccessToken.Token))
Using myClient = New MTService.LanguageServiceClient()
Using scope = New OperationContextScope(myClient.InnerChannel)
OperationContext.Current.OutgoingMessageProperties(HttpRequestMessageProperty.Name)
= httpRequestProperty
Dim ret = myClient.Translate("", "My first translation.", "en",
"fr", Nothing, Nothing)
End Using
End Using
Catch ex As Exception
End Try
其中AccessToken.Token是访问令牌。
where AccessToken.Token is the Access Token.
谢谢大家。
这篇关于SOAP服务中的访问令牌标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!