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

标签 c++ amazon-web-services aws-sdk aws-cli aws-sts

在命令行上,我可以运行此 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/

相关文章:

c++ - 使用 gtest EXPECT_CALL 时出现竞争条件段错误,而另一个期望正在执行相同的方法

amazon-web-services - AWS Alert监控一个key是否定期创建一个bucket

aws-sdk - aws cdk ecs任务调度指定现有安全组

node.js - 尝试上传到 s3 时出现 InvalidAccessKeyId

javascript - 通过 AWS IOT sdk javascript 配置 AWS IOT 设备

c++ - 如何在windows下确定删除文件(文件锁问题)?

python - SWIG C++ 预编译 DLL

c++ - 在循环中增加动态分配数组的大小

amazon-web-services - 通过 cloudformation 使用 aws `cdk synth` 输出

amazon-web-services - 如何通过 cfn 模板中的策略控制访问?