get_access_token 在 Microsoft Translator API 迁移后的 perl 中
get_access_token in perl after Microsoft Translator API migration
Microsoft DataMarket platform retired on April 30, 2017 并将 Microsoft Translator API 移至 Azure。
他们在 C# here
中提供了如何获取新令牌的完整示例
然而,我们的旧流程是 perl,我之前的经验为零。它在下面的代码中使用 post
获取了令牌:
if (!$token or time > $expire - 5) {
$token = '';
console_log("Getting a new access token.") if ($debug);
my $response = $ua->post(
"https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/",
[
client_id => $clientid,
client_secret => $clientsecret,
scope => 'http://api.microsofttranslator.com',
grant_type => 'client_credentials',
],
);
if ($response->is_success and $response->content =~ /^\{"token_type":".+?","access_token":"(.+?)","expires_in":"(\d+?)","scope":".+?"\}$/) {
$token = uri_escape("Bearer ");
$expire = time + ;
if ($fh) {
seek($fh, 0,0);
print $fh "expire:$expire\n";
print $fh "token:$token\n";
truncate($fh, tell($fh));
}
} else {
console_log("Failed to get Access Token.") if ($debug);
}
}
close $fh if($fh);
return $token;
}
我在想这可能就像更改为更新的 url 并抓取旧的 client_id 和客户端机密一样简单,就像这样:
my $response = $ua->post(
"https://api.cognitive.microsoft.com/sts/v1.0/issueToken",
[
//Ocp-Apim-Subscription-Key => newazurekey
],
);
不过我有两个顾虑
1) The documentation sites I have found and read, one says
Ocp-Apim-Subscription-Key is a header, the other a parameter is the code Ocp-Apim-Subscription-Key => newazurekey
ok? does Perl allow dashes?
2) The response recieved testing the API doesn't seem to have the
token_type:
access_token:
expires_in:
or scope:
like in the old
code
我指的是这种情况
if ($response->is_success and $response->content =~ /^\{"token_type":".+?","access_token":"(.+?)","expires_in":"(\d+?)","scope":".+?"\}$/)
响应看起来像这样
我是否在正确获取令牌的正确轨道上,还是需要进行更多更改?
添加
# Sample code uses five minutes
use constant DEFAULT_TOKEN_LIFETIME => 5 * 60;
粗略看来,以下可能有效:
if (!$token or time > $expire - 5) {
$token = '';
console_log("Getting a new access token.") if ($debug);
my $response = $ua->post(
"https://api.cognitive.microsoft.com/sts/v1.0/issueToken",
[
'Ocp-Apim-Subscription-Key' => $newazurekey,
],
);
if ($response->is_success) {
$token = "Bearer " . $response->decoded_content;
$expire = time + DEFAULT_TOKEN_LIFETIME;
if ($fh) {
seek($fh, 0,0);
print $fh "expire:$expire\n";
print $fh "token:$token\n";
truncate($fh, tell($fh));
}
} else {
console_log("Failed to get Access Token.") if ($debug);
}
}
close $fh if($fh);
return $token;
}
这个 comment 说令牌的生命周期是十分钟,但是代码示例使用了五分钟的间隔:
// Use a duration of 5 minutes, which is less than the actual token lifetime of 10 minutes.
private static readonly TimeSpan TokenCacheDuration = new TimeSpan(0, 5, 0);
Microsoft DataMarket platform retired on April 30, 2017 并将 Microsoft Translator API 移至 Azure。
他们在 C# here
中提供了如何获取新令牌的完整示例然而,我们的旧流程是 perl,我之前的经验为零。它在下面的代码中使用 post
获取了令牌:
if (!$token or time > $expire - 5) {
$token = '';
console_log("Getting a new access token.") if ($debug);
my $response = $ua->post(
"https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/",
[
client_id => $clientid,
client_secret => $clientsecret,
scope => 'http://api.microsofttranslator.com',
grant_type => 'client_credentials',
],
);
if ($response->is_success and $response->content =~ /^\{"token_type":".+?","access_token":"(.+?)","expires_in":"(\d+?)","scope":".+?"\}$/) {
$token = uri_escape("Bearer ");
$expire = time + ;
if ($fh) {
seek($fh, 0,0);
print $fh "expire:$expire\n";
print $fh "token:$token\n";
truncate($fh, tell($fh));
}
} else {
console_log("Failed to get Access Token.") if ($debug);
}
}
close $fh if($fh);
return $token;
}
我在想这可能就像更改为更新的 url 并抓取旧的 client_id 和客户端机密一样简单,就像这样:
my $response = $ua->post(
"https://api.cognitive.microsoft.com/sts/v1.0/issueToken",
[
//Ocp-Apim-Subscription-Key => newazurekey
],
);
不过我有两个顾虑
1) The documentation sites I have found and read, one says Ocp-Apim-Subscription-Key is a header, the other a parameter is the code
Ocp-Apim-Subscription-Key => newazurekey
ok? does Perl allow dashes?
2) The response recieved testing the API doesn't seem to have the
token_type:
access_token:
expires_in:
orscope:
like in the old code
我指的是这种情况
if ($response->is_success and $response->content =~ /^\{"token_type":".+?","access_token":"(.+?)","expires_in":"(\d+?)","scope":".+?"\}$/)
响应看起来像这样
我是否在正确获取令牌的正确轨道上,还是需要进行更多更改?
添加
# Sample code uses five minutes
use constant DEFAULT_TOKEN_LIFETIME => 5 * 60;
粗略看来,以下可能有效:
if (!$token or time > $expire - 5) {
$token = '';
console_log("Getting a new access token.") if ($debug);
my $response = $ua->post(
"https://api.cognitive.microsoft.com/sts/v1.0/issueToken",
[
'Ocp-Apim-Subscription-Key' => $newazurekey,
],
);
if ($response->is_success) {
$token = "Bearer " . $response->decoded_content;
$expire = time + DEFAULT_TOKEN_LIFETIME;
if ($fh) {
seek($fh, 0,0);
print $fh "expire:$expire\n";
print $fh "token:$token\n";
truncate($fh, tell($fh));
}
} else {
console_log("Failed to get Access Token.") if ($debug);
}
}
close $fh if($fh);
return $token;
}
这个 comment 说令牌的生命周期是十分钟,但是代码示例使用了五分钟的间隔:
// Use a duration of 5 minutes, which is less than the actual token lifetime of 10 minutes. private static readonly TimeSpan TokenCacheDuration = new TimeSpan(0, 5, 0);