PowerShell调用百度翻译API 2


function BDTranslate{
param(
[string]$q,
[string]$from='zh',
[string]$to='en'

)

$appid = "你的AppId"
$sign = '你的App密码'

$salt = Get-Random
$signtoken = "{0}{1}{2}{3}" -f $appid,$q,$salt,$sign
$signtoken = Get-Hash -string $signtoken  -algorithm MD5
$response = Invoke-RestMethod http://api.fanyi.baidu.com/api/trans/vip/translate -Method Post -Body @{
q=$q
from="zh"
to="en"
appid=$appid
salt=$salt
sign = $signtoken.ToLower()
}
$response
}

调用:

BDTranslate -q 'PowerShell中文博客'

输出示例:

from to trans_result 
---- -- ------------ 
zh en {@{src=PowerShell中文博客; dst=PowerShell Chinese Blog}}
×用微信扫描并分享
本文链接: https://www.pstips.net/7f271.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

2 条评论 “PowerShell调用百度翻译API

  • 我心飞扬

    本人照葫芦画瓢,按格式写了一下,但是无法得到结果,大神能指点一下江山吗?万分感谢:
    下面的参数都是正确的,我用postman,测试可以成功!
    图片的格式是base64的,不包含头,不能有换行,我不知道怎么用函数转,就直接用在线的转码工具转的

    $qaccess_token=’24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329′
    $language=’language_type=CHN_ENG’
    $probability=’probability=true’
    $detect_direction=’detect_direction=true’
    $image=’iVBORw0KGgoAAAANSUhEUgAAAUMAAABACAYAAACA0BUdAAAG9ElEQVR4nO3dvVbqShiH8ZdzLWDh8griFYDNqWx3h6U0dpZ2NlhCZ2tlI1yBXIHLQriX7GRAMhMmk5APmGQ/v7Vy1sINgQmTP5PJm5xeGBEA+Mf9d+4PAAA+IAwBQAhDAFAIQwAQwhAAFMIQAOSsYbiUu15PevFyt6x53Rt5ud6t+/oletQVXWuX1p6D5U6O6xVaf0ov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wG4VHXuAIGRUgAAAABJRU5ErkJggg==’

    $response = Invoke-RestMethod https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic -Method Post -Body @{
    $qaccess_token
    $image
    $language
    $probability
    $detect_direction
    }

    $response