Add Tencent with sign
This commit is contained in:
parent
e6bc578e11
commit
3db228fa08
2 changed files with 66 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
||||||
.idea/
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
SecretKey.csv
|
64
Translator/sign.pl
Normal file
64
Translator/sign.pl
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
#!/usr/bin/perl
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use v5.26;
|
||||||
|
|
||||||
|
use Time::Piece;
|
||||||
|
use Digest::SHA qw(sha256_hex hmac_sha256_hex);
|
||||||
|
|
||||||
|
say "You must provide SecretKey.csv to parent folder!";
|
||||||
|
my $SourceText = "Get Current Time in Seconds Perl";
|
||||||
|
my $payload = qq
|
||||||
|
/{"SourceText":"$SourceText","Source":"en","Target":"zh","ProjectId":0}/;
|
||||||
|
|
||||||
|
my $service = "tmt";
|
||||||
|
my $host = "$service.tencentcloudapi.com";
|
||||||
|
my $region = "ap-shanghai";
|
||||||
|
my $action = "TextTranslate";
|
||||||
|
my $version = "2018-03-21";
|
||||||
|
my $Algorithm = "TC3-HMAC-SHA256";
|
||||||
|
|
||||||
|
my $HTTPRequestMethod = "POST";
|
||||||
|
my $CanonicalURI = "/";
|
||||||
|
my $CanonicalQueryString =
|
||||||
|
( $HTTPRequestMethod eq "POST" ) ? "" : "Limit=10&Offset=0";
|
||||||
|
my $CanonicalHeaders = qq
|
||||||
|
|content-type:application/json; charset=utf-8
|
||||||
|
host:$host
|
||||||
|
x-tc-action:$action|;
|
||||||
|
my $SignedHeaders = "content-type;host;x-tc-action";
|
||||||
|
my $HashedRequestPayload = sha256_hex $payload;
|
||||||
|
my $CanonicalRequest = qq
|
||||||
|
/$HTTPRequestMethod
|
||||||
|
$CanonicalURI
|
||||||
|
$CanonicalQueryString
|
||||||
|
$CanonicalHeaders
|
||||||
|
$SignedHeaders
|
||||||
|
$HashedRequestPayload/;
|
||||||
|
|
||||||
|
my $RequestTimestamp = time();
|
||||||
|
my $Date = localtime->strftime('%Y-%m-%d');
|
||||||
|
my $CredentialScope = "$Date/$service/tc3_request";
|
||||||
|
my $HashedCanonicalRequest = sha256_hex $CanonicalRequest;
|
||||||
|
my $StringToSign = qq
|
||||||
|
/$Algorithm
|
||||||
|
$RequestTimestamp
|
||||||
|
$CredentialScope
|
||||||
|
$HashedCanonicalRequest/;
|
||||||
|
|
||||||
|
open my $in, "<", "../SecretKey.csv" or die $!;
|
||||||
|
my $key;
|
||||||
|
<$in>;
|
||||||
|
$key = <$in>;
|
||||||
|
close($in);
|
||||||
|
my @key = split( ",", $key );
|
||||||
|
my $SecretID = $key[0];
|
||||||
|
my $SecretKey = $key[1];
|
||||||
|
my $SecretDate = hmac_sha256_hex( "TC3$SecretKey", $Date );
|
||||||
|
my $SecretService = hmac_sha256_hex( $SecretDate, $service );
|
||||||
|
my $SecretSigning = hmac_sha256_hex( $SecretService, "tc3_request" );
|
||||||
|
my $Signature = hmac_sha256_hex( $SecretSigning, $StringToSign );
|
||||||
|
|
||||||
|
my $Authorization =
|
||||||
|
"$Algorithm Credential=$SecretID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$Signature";
|
||||||
|
say $Authorization;
|
Loading…
Add table
Add a link
Reference in a new issue