-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestDemo.php
77 lines (65 loc) · 1.94 KB
/
TestDemo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
require 'vendor/autoload.php';
require 'iam-autoloader.php';
use Iam\IamClient;
use Iam\IamException;
//使用租户用户名密码获取临时AK/SK/STS Token
//1.获取AuthToken
//2.获取AK/SK/STS Token
function testAuthToken(){
$client = new IamClient([
//IAM服务地址
'Endpoint' => 'https://iam.myhuaweicloud.com'
]);
try{
$ret = $client->createAuthToken([
'UserName' => '<用户名>',
'Password' => '<用户密码>',
'DomainName' => '<租户名,一般跟用户名相同>'
]);
echo $ret['AuthToken']."\n";
$ret = $client->createCredential([
'AuthToken' => $ret['AuthToken'],
'expires' => '900'
]);
//临时AK
echo $ret['Body']['credential']['access']."\n";
//临时SK
echo $ret['Body']['credential']['secret']."\n";
//临时Token
echo $ret['Body']['credential']['securitytoken']."\n";
}catch (IamException $exception){
echo $exception->getStatusCode() . "\n";
echo $exception->getMessage()."\n";
echo $exception->getExceptionCode()."\n";
echo $exception->getExceptionMessage()."\n";
}
}
//使用租户的永久AK/SK获取临时AK/SK/STS Token
function testAKSK(){
$client = new IamClient([
//IAM服务地址
'Endpoint' => 'https://iam.myhuaweicloud.com'
]);
try{
$ret = $client->createCredential([
//租户的永久AK/SK,定义 临时凭证的过期时间,单位秒
'AK' => '<Your-AK>',
'SK' => '<Your-SK>',
'expires' => '900'
]);
//临时AK
echo $ret['Body']['credential']['access']."\n";
//临时SK
echo $ret['Body']['credential']['secret']."\n";
//临时Token
echo $ret['Body']['credential']['securitytoken']."\n";
}catch (IamException $exception){
echo $exception->getStatusCode() . "\n";
echo $exception->getMessage()."\n";
echo $exception->getExceptionCode()."\n";
echo $exception->getExceptionMessage()."\n";
}
}
testAuthToken();
//testAKSK();