繁体   English   中英

STS 获取来电者身份 C++

[英]STS Get Caller Identity C++

在命令行上,我可以运行此 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;时不断收到异常Aws::STS::STSClient stsClient;

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

有谁知道我做错了什么?

更新:

我什至可以使用适用于 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM