阿里云的RPC风格接口asp版代码(机器翻译通用版asp写的)
今天接了一个小活,用asp写阿里云的机器翻译通用版api接口,本以为是小活,没想到搞一天,阿里云的api很变态,说说过程:
1、先看了《《RPC 调用机制》公共请求参数组装方案》,学会阿里云 RPC OpenAPI 向开发者提供的HTTP接口,来构造 HTTP 请求调用对应的 OpenAPI
2、又去了《阿里云 OpenAPI 开发者门户》找到API 版本
3、又去《RAM 控制台》搞AccessKeyId
4、又研究如何按照ISO8601标准表示,并需要使用 UTC时间去搞Timestamp
5、又研究HMAC-SHA1加密方式
6、然后在研究签名机制
7、又绕回去研究URL接口请求构造
8、为了适应他请求参数进行规范化编码。又学了参数编码方式,使用 UTF-8 字符集按照RFC3986规则进行编码
9、构造签名字符串又学了按照RFC2104的定义,用Base64() 为编码计算函数,HMAC_SHA1() 为 HMAC_SHA1 签名函数,返回值为 HMAC_SHA1 加密后原始字节,而非16进制字符串,UTF_8_Encoding_Of() 是 UTF-8 字符编码
10、通过各种文档,被他绕来绕去,终于成功写好:机器翻译通用版
搞了一整天,现在都快哭了,不说了,上代码吧
<%
'公共请求参数
'RPC风格接口,公共请求参数:https://help.aliyun.com/zh/sdk/product-overview/rpc-mechanism#sectiondiv-qq5-utu-r7a
Action = "TranslateGeneral"'https://help.aliyun.com/document_detail/158244.html?spm=a2c4g.457286.0.0.7a7b2056hIFWX1
Version = "2018-10-12"'https://next.api.aliyun.com/product/alimt
Format = "JSON"'https://help.aliyun.com/zh/sdk/product-overview/rpc-mechanism#sectiondiv-qq5-utu-r7a
AccessKeyId = "LTAI5tM9m77Q29fxnwjxfTfg"'https://ram.console.aliyun.com/manage/ak?spm=a2c4g.11186623.0.0.39553e76BFOaL7
SignatureNonce = MakeRandStr(32)
Timestamp = GetDateTime
SignatureMethod = "HMAC-SHA1"
SignatureVersion = "1.0"
RegionId = "cn-hangzhou"
'下面这个不在公共请求参数之内
AccessKeySecret = "eFlnvrYMHjWxQV0kgwxPDmciHCJOUK"
%>
<%
'OpenAPI 文档中的请求参数
FormatType = "text"
Scene = "general"
SourceLanguage = "zh"
SourceText = "你好"
TargetLanguage = "en"
%>
<%
Arr = Array("Action="&SpecialUrlEncode(Action),"Version="&SpecialUrlEncode(Version),"Format="&SpecialUrlEncode(Format),"AccessKeyId="&SpecialUrlEncode(AccessKeyId),"SignatureNonce="&SpecialUrlEncode(SignatureNonce),"Timestamp="&SpecialUrlEncode(Timestamp),"SignatureMethod="&SpecialUrlEncode(SignatureMethod),"SignatureVersion="&SpecialUrlEncode(SignatureVersion),"FormatType="&SpecialUrlEncode(FormatType),"Scene="&SpecialUrlEncode(Scene),"SourceLanguage="&SpecialUrlEncode(SourceLanguage),"SourceText="&SpecialUrlEncode(SourceText),"TargetLanguage="&SpecialUrlEncode(TargetLanguage),"RegionId="&SpecialUrlEncode(RegionId))
Arr2 = kSort(Arr)
CanonicalizedQueryString = ToUrlParams(arr2)
url = "http://mt.cn-hangzhou.aliyuncs.com/api/translate/web/general?Signature="&Sign(CanonicalizedQueryString)&"&"&CanonicalizedQueryString
Result = Get_url(url)
%>
<%
set obj = getjson(Result)
if isobject(obj) Then
Response.Write(obj.Code)
Response.Write("<br>")
Response.Write(obj.data.Translated)
Response.Write("<br>")
Response.Write(obj.RequestId)
Response.Write("<br>")
End If
set obj = Nothing
%>