【问题标题】:How do I get my AWS Cognito access token for updating user attributes?如何获取用于更新用户属性的 AWS Cognito 访问令牌?
【发布时间】:2018-01-05 21:39:33
【问题描述】:

我已经设置了更新用户属性的结构,在这种情况下,首选用户名用作登录的别名。

var attributes = [AWSCognitoIdentityUserAttributeType]()

let prefUsername = AWSCognitoIdentityUserAttributeType();
prefUsername?.name = "preferred_username";
prefUsername?.value = usernameField.text!;
attributes.append(prefUsername!);

let attributesRequest = AWSCognitoIdentityProviderUpdateUserAttributesRequest();
attributesRequest.userAttributes = attributes;

idProvider?.updateUserAttributes(attributesRequest)

唯一我不知道该怎么做的就是获取访问令牌。我已经查看了我能想到的尽可能多的文档,但我没有找到获得访问令牌的地方。

【问题讨论】:

    标签: swift amazon-web-services amazon-cognito


    【解决方案1】:

    您必须先进行身份验证才能与 Cognito 用户池建立会话。该会话将包含一个访问令牌,然后您可以将其传递给每个后续请求。我看到您正在使用低级 SDK 方法。这是一个快速登录的示例:

    https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample/Swift

    【讨论】:

    • 令牌在哪里?
    【解决方案2】:

    您可以使用api发起auth并从AuthenticationResult中获取AccessToken。

    https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html

    【讨论】:

      【解决方案3】:
          /// Function to retreive the current token for the logged in user.
          ///
          /// - Parameter completion: A completion block with an error or the token. Called back on the main thread.
          public func getJWT(completion: @escaping((_ error: Error?, _ token: AWSCognitoIdentityUserSessionToken?) -> Void)) {
              guard let user = self.pool.currentUser() else {
                  let nsError = NSError(domain: "JWT Error", code: 500, userInfo: ["message": "No Logged in user"])
                  completion(nsError, nil)
                  return
              }
              user.getSession().continueWith { (task) -> Any? in
                  DispatchQueue.main.async {
                      if let error = task.error {
                          completion(error, nil)
                      }else if let token = task.result?.idToken {
                          completion(nil, token)
                      }else {
                          completion(nil, nil)
                      }
                  }
              }
          }
      

      self.pool 是您希望正确设置的AWSCognitoIdentityUserPool

      【讨论】:

        猜你喜欢
        • 2017-11-03
        • 2023-03-19
        • 2018-08-10
        • 2018-06-07
        • 1970-01-01
        • 2020-11-20
        • 2021-11-27
        • 2016-09-23
        • 2020-09-28
        相关资源
        最近更新 更多