- 論壇徽章:
- 0
|
本帖最后由 aef25u 于 2019-03-22 13:08 編輯
perl6 使用百度翻譯API遇到"{"error_code":"54001","error_msg":"Invalid Sign"}
請大家幫忙找一下問題。
判斷問題出在:
- my $sign = $appid~$q~$salt~$secretKey;
- my $d=Digest::MD5.new;
- $sign =$d.md5_hex($sign.encode("ascii"));
復制代碼 附代碼:
- use Digest::MD5;
- use URI::Escape;
- use HTTP::UserAgent;
- translation({say "翻譯:"},'"Doing multiple things at the same time"');
-
- sub translation(&code-ref,$q,$fromLang="zh", $toLang="en"){
- my $appid = '**********' ;#你的appid(這里是必填的, 從百度 開發者信息一覽獲取)
- my $secretKey = '*********'; #你的密鑰(這里是必填的, 從百度 開發者信息一覽獲取)
- my $url='http://api.fanyi.baidu.com/api/trans/vip/translate';
- my $salt = (32768.. 65536).rand.Int;
- my $sign = $appid~$q~$salt~$secretKey;
- my $d=Digest::MD5.new;
- #{error_msg : Invalid Sign}==>$sign =$d.md5_hex($sign.encode("ascii"));
- $sign =$d.md5_hex($sign);
- $url = $url~'?appid='~$appid~'&q='~uri_escape($q)~'&from='~$fromLang~'&to='~$toLang~'&salt='~$salt~'&sign='~$sign;
- say $url;
-
- my $ua = HTTP::UserAgent.new;
- $ua.timeout = 10;
- my $resp=$ua.get($url);
- if $resp.is-success {
- my $par=$resp.content;
- say $par;
- } else {
- die $resp.status-line;
- }
- }
復制代碼 代碼參考這編文章改寫https://www.jianshu.com/p/be93c26c7d5f(python3實現)
|
|