Board logo

标题: [问题求助] 转换腾讯翻译从PHP到PowerShell代码遇到错误 [打印本页]

作者: 小白龙    时间: 2023-8-1 07:31     标题: 转换腾讯翻译从PHP到PowerShell代码遇到错误

腾讯翻译的后台, 没有找到那种简单几个参数发post表单的调用方式, 我找到了下面链接的php代码, 我尝试将其转为powershell , 但是不成功, 请求路过大佬支招

https://gitee.com/uanaoeng/QQTra ... er/QQTranslator.php

腾讯翻译API后台, 注册帐号每月500万字符免费额度:
https://console.cloud.tencent.co ... ction=TextTranslate

我用gpt转的powershell代码如下:
  1. class QQTranslator {
  2. # =========================================================================
  3. # 成员变量
  4. # =========================================================================
  5. $secretId = ""
  6. $secretKey = ""
  7. $projectId = 0
  8. $sourceLanguage = "auto"
  9. $targetLanguage = "zh"
  10. $region = "ap-hongkong"
  11. # =========================================================================
  12. # 公共成员函数
  13. # =========================================================================
  14. # 设置 secret Id
  15. function setSecretId($id)
  16. {
  17. $this.secretId = $id
  18. }
  19. # 设置 secret key
  20. function setSecretKey($key)
  21. {
  22. $this.secretKey = $key
  23. }
  24. # 设置 project id
  25. function setProjectId($id)
  26. {
  27. $this.projectId = $id
  28. }
  29. # 指定源语言
  30. function setSourceLanguage($str)
  31. {
  32. $this.sourceLanguage = $str
  33. }
  34. # 指定目标语言
  35. function setTargetLanguage($str)
  36. {
  37. $this.targetLanguage = $str
  38. }
  39. # 设置所在地域的服务器
  40. function setRegion($str)
  41. {
  42. $this.region = $str
  43. }
  44. # 一次翻译一个句子
  45. function translateString($sourceText)
  46. {
  47. $secretId = $this.secretId
  48. $secretKey = $this.secretKey
  49. $projectId = $this.projectId
  50. $region = $this.region
  51. $sourceLanguage = ""
  52. $targetLanguage = ""
  53. # 取得传入参数的数组
  54. $count = $args.Length
  55. switch ($count)
  56. {
  57. 1 {
  58. # 用户未指定源语言和目标语言
  59. $sourceLanguage = $this.sourceLanguage
  60. $targetLanguage = $this.targetLanguage
  61. }
  62. 2 {
  63. # 用户未指定目标语言
  64. $sourceLanguage = $this.sourceLanguage
  65. $targetLanguage = $args[1]
  66. }
  67. 3 {
  68. # 用户同时指定了源语言和目标语言
  69. $sourceLanguage = $args[1]
  70. $targetLanguage = $args[2]
  71. }
  72. }
  73. $params = @{
  74. 'Action' = 'TextTranslate'
  75. 'Region' = $region
  76. 'ProjectId'  = $projectId
  77. 'SecretId'   = $secretId
  78. 'Source' = $sourceLanguage
  79. 'Target' = $targetLanguage
  80. 'SourceText' = $sourceText
  81. 'Timestamp'  = [System.Convert]::ToString((Get-Date).ToUniversalTime().Subtract((Get-Date "1970-01-01")).TotalSeconds)
  82. 'Nonce'      = [System.Convert]::ToString((Get-Random).Next(1000000))
  83. 'Version'    = '2018-03-21'
  84. 'Signature'  = ''
  85. }
  86. $params['Signature'] = $this.getReqSign($params, $secretKey)
  87. $url = 'https://tmt.tencentcloudapi.com/'
  88. $response = $this.doHttpPost($url, $params)
  89. $responseArray = $response | ConvertFrom-Json
  90. if ($responseArray.Response.TargetText)
  91. {
  92. return $responseArray.Response.TargetText
  93. }
  94. elseif ($responseArray.Response.Error)
  95. {
  96. $errorCode = $responseArray.Response.Error.Code
  97. $errorMessage = $responseArray.Response.Error.Message
  98. return "获取翻译失败。错误代码:$errorCode,错误描述:$errorMessage"
  99. }
  100. else
  101. {
  102. return "获取翻译失败。未知错误。"
  103. }
  104. }
  105. # 一次翻译多个句子
  106. function translateStrings($sourceTextList)
  107. {
  108. $secretId = $this.secretId
  109. $secretKey = $this.secretKey
  110. $projectId = $this.projectId
  111. $region = $this.region
  112. $sourceLanguage = ""
  113. $targetLanguage = ""
  114. # 取得传入参数的数组
  115. $count = $args.Length
  116. switch ($count)
  117. {
  118. 1 {
  119. $sourceLanguage = $this.sourceLanguage
  120. $targetLanguage = $this.targetLanguage
  121. }
  122. 2 {
  123. $sourceLanguage = $this.sourceLanguage
  124. $targetLanguage = $args[1]
  125. }
  126. 3 {
  127. $sourceLanguage = $args[1]
  128. $targetLanguage = $args[2]
  129. }
  130. }
  131. $params = @{
  132. 'Action'    = 'TextTranslateBatch'
  133. 'Region'    = $region
  134. 'ProjectId' = $projectId
  135. 'SecretId'  = $secretId
  136. 'Source'    = $sourceLanguage
  137. 'Target'    = $targetLanguage
  138. 'Timestamp' = [System.Convert]::ToString((Get-Date).ToUniversalTime().Subtract((Get-Date "1970-01-01")).TotalSeconds)
  139. 'Nonce'     = [System.Convert]::ToString((Get-Random).Next(1000000))
  140. 'Version'   = '2018-03-21'
  141. 'Signature' = ''
  142. }
  143. $counter = 0
  144. foreach ($sourceText in $sourceTextList)
  145. {
  146. $itemName = "SourceTextList.$counter"
  147. $params[$itemName] = $sourceText
  148. $counter += 1
  149. }
  150. $params['Signature'] = $this.getReqSign($params, $secretKey)
  151. $url = 'https://tmt.tencentcloudapi.com/'
  152. $response = $this.doHttpPost($url, $params)
  153. $responseArray = $response | ConvertFrom-Json
  154. if ($responseArray.Response.TargetTextList)
  155. {
  156. return $responseArray.Response.TargetTextList
  157. }
  158. elseif ($responseArray.Response.Error)
  159. {
  160. $errorCode = $responseArray.Response.Error.Code
  161. $errorMessage = $responseArray.Response.Error.Message
  162. return "获取翻译失败。错误代码:$errorCode,错误描述:$errorMessage"
  163. }
  164. else
  165. {
  166. return "获取翻译失败。未知错误。"
  167. }
  168. }
  169. # =========================================================================
  170. # 私有成员函数
  171. # =========================================================================
  172. # getReqSign :根据接口请求参数和应用密钥计算请求签名
  173. function getReqSign($params, $appkey)
  174. {
  175. # 对参数排序
  176. $params = $params.GetEnumerator() | Sort-Object Name
  177. $signStr = ""
  178. foreach ($param in $params)
  179. {
  180. $signStr += $param.Key + "=" + $param.Value
  181. }
  182. # 拼接签名原文字符串
  183. $signStr = "POSTtmt.tencentcloudapi.com/?" + $signStr
  184. # 生成签名串
  185. $hmacsha1 = New-Object System.Security.Cryptography.HMACSHA1
  186. $byteArray = [System.Text.Encoding]::UTF8.GetBytes($appkey)
  187. $hmacsha1.Key = $byteArray
  188. $signBytes = $hmacsha1.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($signStr))
  189. $sign = [System.Convert]::ToBase64String($signBytes)
  190. return $sign
  191. }
  192. # doHttpPost :执行POST请求,并取回响应结果
  193. function doHttpPost($url, $params)
  194. {
  195. $client = New-Object System.Net.WebClient
  196. $body = [System.Text.Encoding]::UTF8.GetBytes(($params | ConvertTo-Json))
  197. $client.Headers["Content-Type"] = "application/json"
  198. $response = $client.UploadData($url, "POST", $body)
  199. return [System.Text.Encoding]::UTF8.GetString($response)
  200. }
  201. # -------------------------------------------------------------------------
  202. }
  203. $translator = [QQTranslator]::new()
  204. $translator.setSecretId("")
  205. $translator.setSecretKey("")
  206. $translator.setProjectId(0)
  207. $translator.setSourceLanguage("auto")
  208. $translator.setTargetLanguage("zh")
  209. $sourceText = "你好"
  210. $translation = $translator.translateString($sourceText)
  211. Write-Host "翻译结果: $translation"
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2