簡體   English   中英

如何使用 swift 從 AuthCognitoTokensProvider 檢索 Cognito 令牌

[英]How to retrieve Cognito token from AuthCognitoTokensProvider using swift

我已經成功地將 Amplify 集成到我的項目中。 這幫助我注冊、登錄和退出。 現在我需要一種方法來檢索 Cognito 令牌,以便我可以使用它在我的應用程序中進行 api 調用。 不幸的是,這沒有用,當我嘗試從我的 Amplify.Auth.fetchAuthSession 方法中轉換 session 時,出現以下錯誤。 而且我還能夠獲得 isSignedIn 狀態。 > cast session Optional(AmplifyPlugins.AWSAuthCognitoSession(isSignedIn: true, userSubResult: Swift.Result<Swift.String, Amplify.AuthError>.failure(AuthError: 消息發生意外錯誤:發生未知錯誤恢復建議:這不應該發生。一直報錯可能是有BUG,請查看

錯誤域=com.amazonaws.AWSCognitoIdentityErrorDomain 代碼=5“(空)”用戶信息={__type=InvalidIdentityPoolConfigurationException,消息=身份池配置無效。 檢查為此池分配的 IAM 角色。}), identityIdResult: Swift.Result<Swift.String, Amplify.AuthError>.failure(AuthError: 消息發生意外錯誤:發生未知錯誤恢復建議:這不應該發生。

Amplify.Auth.fetchAuthSession { result in
    do {
      let session = try result.get()
      print(session.isSignedIn)
        let castSession = session as? AWSAuthCognitoSession
        print("cast session ", castSession)
      if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
      let tokens = try cognitoTokenProvider.getCognitoTokens().get()
          print("Token ", tokens)
      }

    } catch {
      print(error.localizedDescription)
    }
}

isSignedIn 值為真。 當我嘗試轉換和檢索 Cognito 令牌時拋出 catch 異常

在我的項目中設置放大時,我只能選擇 Cognito 用戶池。 這解決了我的問題,我能夠檢索到 accessToken 和 rest。希望有一天這能幫助某人干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM