找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 11159|回复: 0

[问题求助] 转换腾讯翻译从PHP到PowerShell代码遇到错误

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

  242. $translator = [QQTranslator]::new()
  243. $translator.setSecretId("")
  244. $translator.setSecretKey("")
  245. $translator.setProjectId(0)
  246. $translator.setSourceLanguage("auto")
  247. $translator.setTargetLanguage("zh")

  248. $sourceText = "你好"
  249. $translation = $translator.translateString($sourceText)
  250. Write-Host "翻译结果: $translation"
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 22:41 , Processed in 0.017814 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表