0

我尝试通过此 aws Doc https://docs.aws.amazon.com/en_us/cognito/latest/developerguide/amazon-cognito-integrating-user-pools-with-登录后使用身份池访问 aws 服务 身份池.html

但是在将用户池与身份池部分集成时,我无法在 swift 代码中添加令牌

let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET", poolId: "YOUR_USER_POOL_ID")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: "UserPool")
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "YOUR_IDENTITY_POOL_ID", identityProviderManager:pool)

在 javascript 和 android 中都有 credentialsProvider.logins 。

我收到这样的消息:

Message: The security token included in the request is invalid.

或者我需要通过这个文档调用 aws sts api? https://docs.aws.amazon.com/en_us/IAM/latest/UserGuide/id_credentials_temp_request.html

感谢帮助。

4

1 回答 1

0

首先我试试这个

let identity = AWSCognitoIdentity.default()
        let input = AWSCognitoIdentityGetCredentialsForIdentityInput()
        input?.identityId = identityid
        input?.logins = ["cognito-idp.us-east-1.amazonaws.com/us-east-1_XXXXX":identityToken]
        identity.getCredentialsForIdentity(input!).continueWith(block: { (task:AWSTask<AWSCognitoIdentityGetCredentialsForIdentityResponse>) -> Any? in

            if (task.error != nil) {
                print("Error: " + task.error!.localizedDescription)
            }
            else {
                self.accessKey = (task.result?.credentials?.accessKeyId)!
                self.secrectKey = (task.result?.credentials?.secretKey)!
                self.sessionToken = (task.result?.credentials?.sessionToken)
            }
            return task;
        })

但这不起作用,我不确定会发生什么。我使用 https 来获取 aws iot shadow,它将返回 null 值并获取 403 消息。然后我像这样更改官方文档代码

let serviceConfiguration = AWSServiceConfiguration(region: .USEast1,credentialsProvider: nil)
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET", poolId: "YOUR_USER_POOL_ID")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: "UserPool")
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "YOUR_IDENTITY_POOL_ID", identityProviderManager:pool)

然后使用此代码

    credentialsProvider.credentials().continueWith(block: { (task) -> AnyObject? in
        if (task.error != nil) {
            print("Error: " + task.error!.localizedDescription)
        }
        else {
            let credentials = task.result!
            self.accessKey = credentials.accessKey
            self.secrectKey = credentials.secretKey
            self.session  = credentials.sessionKey!           }
        return task;
    })

但是这段代码没有使用我从 cognito 登录获得的 idtoken,也许这只是让我访问服务,然后我可以解析 idtoken 以获取 idtoken 详细消息。

希望这对某人有所帮助。

除此之外,导入的东西是attachPrincipalPolicy你用iot的临时凭证,当你登录cognito时,你会得到凭证identityID,然后用这个identityID来附加。

否则您将得到 403 禁止,并返回空值。

FYR https://github.com/awslabs/aws-sdk-ios-samples/blob/master/IoT-Sample/Swift/IoTSampleSwift/ConnectionViewController.swift

于 2019-03-07T07:22:45.420 回答