local function transform(txt,target)
local xhr = cc.XMLHttpRequest:new()
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING
local url = "https://tmt.tencentcloudapi.com/"
local timestamp = ServerTimeUtil.getServerTime()
local nonce = math.random(10,1000)
local SecretId = "自己的id"
local SecretKey = "自己的key"
local singParam = "Action=TextTranslate&Nonce="..nonce.."&ProjectId=1300328761&Region=ap-guangzhou&SecretId="..SecretId.."&Source=auto&SourceText="..txt.."&Target="..target.."&Timestamp="..timestamp.."&Version=2018-03-21"
local param = "?"..singParam
-- local signStr = "GET".."&"..string.urlencode(url).."&"..string.urlencode(singParam)
local signStr = "GETtmt.tencentcloudapi.com/?"..singParam
local simpleParam = "ProjectId=1300328761&Source=auto&SourceText="..txt.."&Target="..target
local simpleParam = '{"SourceText":"hello","Source":"auto","Target":"zh","ProjectId":1300328761}'
print("before sign:",signStr)
local md5 = require("Game.Net.md5")
local sha1 = require('Game.Net.sha1.init')
local sha2 = require('Game.Net.sha1.sha2')
local hmac = sha2.hmac
-- local signMid = encodeBase64(sha1.hmac(SecretKey, signStr))
-- local signMid = encodeBase64(md5.sumhexa(signStr))
-- local signMid = md5.sumhexa(signStr)
local signMid = hmac(sha2.sha256, SecretKey, simpleParam)
--string.urlencode(signMid)
print("after sign:",signMid)
print("transform tecent:",url..param.."&Signature="..signMid)
xhr:open("GET", url..param.."&Signature="..signMid)
--设置消息头
xhr:setRequestHeader("Content-Type", "application/json")
local function onReadyStateChange()
print("onReadyStateChange",xhr.readyState,xhr.status)
if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
local statusString = "Http Status Code:"..xhr.statusText
print("请求返回状态码"..statusString)
local xhrStr = xhr.response
print("xhrStr:",xhrStr)
local xhrObj = json.decode(xhrStr)
xhr:abort()
else
xhr:abort()
end
end
xhr:registerScriptHandler(onReadyStateChange)
xhr:send()
end
相似问题