gpt4 book ai didi

c++ - STS获取调用者身份C++

转载 作者:行者123 更新时间:2023-12-02 10:22:32 25 4
gpt4 key购买 nike

在命令行上,我可以运行此AWS CLI命令以获取在本地计算机上使用的AWS UserId

$ aws sts get-caller-identity
{
"UserId": "123456789:john.doe",
"Account": "123456789",
"Arn": "arn:aws:sts::123456789:federated-user/john.doe"
}

现在,我需要从适用于C++的AWS开发工具包获取相同的信息(尤其是UserId)。我一直在尝试像这样使用 aws/sts/STSClient.h
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/sts/STSClient.h>
#include <aws/sts/model/GetAccessKeyInfoRequest.h>
#include <aws/sts/model/GetCallerIdentityRequest.h>

#include <array>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdexcept>

#include <unistd.h>

void test() {

Aws::STS::STSClient stsClient;

Aws::STS::Model::GetCallerIdentityRequest getCallerIdentityRequest;

Aws::STS::Model::GetCallerIdentityOutcome callerIdentityOutcome =
stsClient.GetCallerIdentity(getCallerIdentityRequest);

if (callerIdentityOutcome.IsSuccess())
{
Aws::STS::Model::GetCallerIdentityResult callerIdentityResult = callerIdentityOutcome.GetResult();

Aws::String const &userIdAWSString = callerIdentityResult.GetUserId();
std::string userId(userIdAWSString.c_str(), userIdAWSString.size());

std::cout << "userId: " << userId << std::endl;
}
else
{
std::cout << "callerIdentityOutcome: Failed to retrieve STS GetCallerIdentityRequest" << std::endl;
}
}

但是我在调​​用 Aws::STS::STSClient stsClient;时总是遇到异常

external/aws/aws-cpp-sdk-core/source/config/AWSProfileConfigLoader.cpp:498: Aws::Config::Profile Aws::Config::GetCachedConfigProfile(const Aws::String &): Assertion `s_configManager' failed.



有人知道我在做什么错吗?

更新:

我什至可以使用适用于AWS的Python SDK的Boto3库做到这一点
>>> import boto3
>>> client = boto3.client('sts')
>>> response = client.get_caller_identity()
>>> print(response)
{u'Account': '123456789', u'UserId': '123456789:john.doe', 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '80e471ba-384a-4b6a-a5b8-f754f16c3a23', 'HTTPHeaders': {'x-amzn-requestid': '80e471ba-384a-4b6a-a5b8-f754f16c3a23', 'date': 'Fri, 27 Dec 2019 17:13:27 GMT', 'content-length': '431', 'content-type': 'text/xml'}}, u'Arn': 'arn:aws:sts::123456789:federated-user/john.doe'}

为什么在C++中很难做到这一点?

最佳答案

您是否正确初始化了SDK?这是一个骨架:

#include <aws/core/Aws.h>
int main(int argc, char** argv)
{
Aws::SDKOptions options;
Aws::InitAPI(options);

// make your SDK calls here

Aws::ShutdownAPI(options);
return 0;
}

关于c++ - STS获取调用者身份C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com