Skip to content

Commit

Permalink
腾讯云翻译 小调整
Browse files Browse the repository at this point in the history
  • Loading branch information
imba-tjd committed Feb 12, 2023
1 parent a7869f0 commit b475cc0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
3 changes: 1 addition & 2 deletions TranslatorLibrary/CommonFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public static string EncryptString(string str)
/// <returns></returns>
public static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
return DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
}

/// <summary>
Expand Down
18 changes: 6 additions & 12 deletions TranslatorLibrary/TencentOldTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ public async Task<string> TranslateAsync(string sourceText, string desLang, stri
errorInfo = "Param Missing";
return null;
}

// 原文
string q = HttpUtility.UrlEncode(sourceText);

string retString;

string salt = CommonFunction.RD.Next(100000).ToString();
string ts = CommonFunction.GetTimeStamp().ToString();

string url = "https://tmt.tencentcloudapi.com/?";

Expand All @@ -49,14 +45,11 @@ public async Task<string> TranslateAsync(string sourceText, string desLang, stri
.Append("&Source=").Append(srcLang)
.Append("&SourceText=").Append(sourceText)
.Append("&Target=").Append(desLang)
.Append("&Timestamp=").Append(CommonFunction.GetTimeStamp())
.Append("&Timestamp=").Append(ts)
.Append("&Version=2018-03-21");
string req = sb.ToString();

HMACSHA1 hmac = new HMACSHA1()
{
Key = Encoding.UTF8.GetBytes(SecretKey)
};
HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(SecretKey));
byte[] data = Encoding.UTF8.GetBytes("GETtmt.tencentcloudapi.com/?" + req);
var result = hmac.ComputeHash(data);
hmac.Dispose();
Expand All @@ -69,13 +62,14 @@ public async Task<string> TranslateAsync(string sourceText, string desLang, stri
.Append("&Region=ap-shanghai")
.Append("&SecretId=").Append(SecretId)
.Append("&Source=").Append(srcLang)
.Append("&SourceText=").Append(q)
.Append("&SourceText=").Append(HttpUtility.UrlEncode(sourceText))
.Append("&Target=").Append(desLang)
.Append("&Timestamp=").Append(CommonFunction.GetTimeStamp())
.Append("&Timestamp=").Append(ts)
.Append("&Version=2018-03-21")
.Append("&Signature=").Append(HttpUtility.UrlEncode(Convert.ToBase64String(result)));
req = sb.ToString();

string retString;
var hc = CommonFunction.GetHttpClient();
try
{
Expand Down

0 comments on commit b475cc0

Please sign in to comment.