Skip to content

Commit

Permalink
Merge pull request #67 from AlibabaCloudLandingZone/solution-ack-sts-…
Browse files Browse the repository at this point in the history
…token/0.0.3

solution-ack-sts-token/0.0.3
  • Loading branch information
wibud authored Jul 5, 2024
2 parents 94230f3 + a5ce1da commit abfcbc9
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 58 deletions.
12 changes: 6 additions & 6 deletions solution/solution-ack-sts-token/code-example/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
</properties>

<dependencies>
<!--2.0 vpc-sdk-->
<!--2.0 sts-sdk-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>vpc20160428</artifactId>
<version>7.8.7</version>
<artifactId>sts20150401</artifactId>
<version>1.1.4</version>
</dependency>
<!--1.0 vpc-sdk-->
<!--1.0 sts-sdk-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vpc</artifactId>
<version>3.2.28</version>
<artifactId>aliyun-java-sdk-sts</artifactId>
<version>3.1.2</version>
</dependency>
<!--oss-sdk-->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import com.aliyuncs.auth.BasicSessionCredentials;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vpc.model.v20160428.DescribeVpcsRequest;
import com.aliyuncs.vpc.model.v20160428.DescribeVpcsResponse;
import com.aliyuncs.sts.model.v20150401.GetCallerIdentityRequest;
import com.aliyuncs.sts.model.v20150401.GetCallerIdentityResponse;

/**
* 通过Credentials工具初始化,使用默认凭据链
Expand All @@ -22,11 +22,10 @@ public static void main(String[] args) throws ClientException {
// 用凭据客户端初始化SDK1.0客户端
IAcsClient client = createAcsClientByCredentials(profile, credentialClient);

// 调用API,以VPC为例
DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest();
describeVpcsRequest.setRegionId("cn-hangzhou");
DescribeVpcsResponse describeVpcsResponse = client.getAcsResponse(describeVpcsRequest);
System.out.println(JSON.toJSONString(describeVpcsResponse));
// 调用API,以GetCallerIdentity获取当前调用者身份信息为例
GetCallerIdentityRequest getCallerIdentityRequest = new GetCallerIdentityRequest();
GetCallerIdentityResponse getCallerIdentityResponse = client.getAcsResponse(getCallerIdentityRequest);
System.out.println(JSON.toJSONString(getCallerIdentityResponse));
}

public static IAcsClient createAcsClientByCredentials(DefaultProfile profile, Client credentialClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.aliyuncs.auth.BasicSessionCredentials;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vpc.model.v20160428.DescribeVpcsRequest;
import com.aliyuncs.vpc.model.v20160428.DescribeVpcsResponse;
import com.aliyuncs.sts.model.v20150401.GetCallerIdentityRequest;
import com.aliyuncs.sts.model.v20150401.GetCallerIdentityResponse;

/**
* 通过Credentials工具初始化,使用OIDCRoleArn
Expand All @@ -37,11 +37,10 @@ public static void main(String[] args) throws ClientException {
// 用凭据客户端初始化SDK1.0客户端
IAcsClient client = createAcsClientByCredentials(profile, credentialClient);

// 调用API,以VPC为例
DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest();
describeVpcsRequest.setRegionId("cn-hangzhou");
DescribeVpcsResponse describeVpcsResponse = client.getAcsResponse(describeVpcsRequest);
System.out.println(JSON.toJSONString(describeVpcsResponse));
// 调用API,以GetCallerIdentity获取当前调用者身份信息为例
GetCallerIdentityRequest getCallerIdentityRequest = new GetCallerIdentityRequest();
GetCallerIdentityResponse getCallerIdentityResponse = client.getAcsResponse(getCallerIdentityRequest);
System.out.println(JSON.toJSONString(getCallerIdentityResponse));
}

public static IAcsClient createAcsClientByCredentials(DefaultProfile profile, Client credentialClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@

import com.alibaba.fastjson2.JSON;
import com.aliyun.credentials.Client;
import com.aliyun.vpc20160428.models.DescribeVpcsRequest;
import com.aliyun.vpc20160428.models.DescribeVpcsResponse;
import com.aliyun.sts20150401.models.GetCallerIdentityResponse;

/**
* 通过Credentials工具初始化,使用默认凭据链
*/
public class CredentialsDefaultSample {
public static void main(String[] args) throws Exception {
// 初始化凭据客户端
Client credentialClient = new Client();
Client credentialsClient = new Client();

// 调用API,以VPC为例
// 调用API,以GetCallerIdentity获取当前调用者身份信息为例
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
config.setCredential(credentialClient);
config.setEndpoint("vpc.aliyuncs.com");
com.aliyun.vpc20160428.Client vpcClient = new com.aliyun.vpc20160428.Client(config);

DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest().setRegionId("cn-hangzhou");
DescribeVpcsResponse describeVpcsResponse = vpcClient.describeVpcs(describeVpcsRequest);
System.out.println(JSON.toJSONString(describeVpcsResponse));
config.setCredential(credentialsClient);
config.setEndpoint("sts.cn-hangzhou.aliyuncs.com");
com.aliyun.sts20150401.Client stsClient = new com.aliyun.sts20150401.Client(config);
GetCallerIdentityResponse getCallerIdentityResponse = stsClient.getCallerIdentity();
System.out.println(JSON.toJSONString(getCallerIdentityResponse));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.alibaba.fastjson2.JSON;
import com.aliyun.credentials.Client;
import com.aliyun.credentials.models.Config;
import com.aliyun.vpc20160428.models.DescribeVpcsRequest;
import com.aliyun.vpc20160428.models.DescribeVpcsResponse;
import com.aliyun.sts20150401.models.GetCallerIdentityResponse;

/**
* 通过Credentials工具初始化,使用OIDCRoleArn
Expand All @@ -26,16 +25,14 @@ public static void main(String[] args) throws Exception {
credentialConfig.setExternalId("<ExternalId>");
// 设置session过期时间
credentialConfig.setRoleSessionExpiration(3600);
Client credentialClient = new Client(credentialConfig);
Client credentialsClient = new Client(credentialConfig);

// 调用API,以VPC为例
// 调用API,以GetCallerIdentity获取当前调用者身份信息为例
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
config.setCredential(credentialClient);
config.setEndpoint("vpc.aliyuncs.com");
com.aliyun.vpc20160428.Client vpcClient = new com.aliyun.vpc20160428.Client(config);

DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest().setRegionId("cn-hangzhou");
DescribeVpcsResponse describeVpcsResponse = vpcClient.describeVpcs(describeVpcsRequest);
System.out.println(JSON.toJSONString(describeVpcsResponse));
config.setCredential(credentialsClient);
config.setEndpoint("sts.cn-hangzhou.aliyuncs.com");
com.aliyun.sts20150401.Client stsClient = new com.aliyun.sts20150401.Client(config);
GetCallerIdentityResponse getCallerIdentityResponse = stsClient.getCallerIdentity();
System.out.println(JSON.toJSONString(getCallerIdentityResponse));
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
from alibabacloud_vpc20160428.client import Client as Vpc20160428Client
from alibabacloud_sts20150401.client import Client as Sts20150401Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_vpc20160428 import models as vpc_20160428_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_credentials.client import Client as CredentialsClient

# 使用默认凭据链
cred = CredentialsClient()

config = open_api_models.Config()
config.endpoint = f'vpc-vpc.cn-hangzhou.aliyuncs.com'
config.endpoint = f'sts.cn-hangzhou.aliyuncs.com'
config.credential = cred
vpcClient = Vpc20160428Client(config)

describeVpcsRequest = vpc_20160428_models.DescribeVpcsRequest(region_id='cn-hangzhou')
stsClient = Sts20150401Client(config)

try:
runtime = util_models.RuntimeOptions()
# 复制代码运行请自行打印 API 的返回值
describeVpcsResponse = vpcClient.describe_vpcs(describeVpcsRequest, runtime)
print(describeVpcsResponse.body.vpcs.vpc)
response = stsClient.get_caller_identity_with_options(runtime)
print(response)
except Exception as error:
print(error)
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import os

from alibabacloud_vpc20160428.client import Client as Vpc20160428Client
from alibabacloud_sts20150401.client import Client as Sts20150401Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_vpc20160428 import models as vpc_20160428_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_credentials.client import Client as CredentialsClient
from alibabacloud_credentials.models import Config
Expand All @@ -26,16 +25,14 @@
cred = CredentialsClient(config)

config = open_api_models.Config()
config.endpoint = f'vpc-vpc.cn-hangzhou.aliyuncs.com'
config.endpoint = f'sts.cn-hangzhou.aliyuncs.com'
config.credential = cred
vpcClient = Vpc20160428Client(config)

describeVpcsRequest = vpc_20160428_models.DescribeVpcsRequest(region_id='cn-hangzhou')
stsClient = Sts20150401Client(config)

try:
runtime = util_models.RuntimeOptions()
# 复制代码运行请自行打印 API 的返回值
describeVpcsResponse = vpcClient.describe_vpcs(describeVpcsRequest, runtime)
print(describeVpcsResponse.body.vpcs.vpc)
response = stsClient.get_caller_identity_with_options(runtime)
print(response)
except Exception as error:
print(error)

0 comments on commit abfcbc9

Please sign in to comment.