9

我正在开发一个使用 Alexa 语音服务并维护不同用户的应用程序,因此用户需要使用亚马逊 (LWA) 登录。我已经实现了它,就像它写在文档中一样,它完美无缺。

LWA 文档:https ://developer.amazon.com/de/docs/login-with-amazon/use-sdk-ios.html

AMZNAuthorizationManager.shared().authorize(request, withHandler: {(result : AMZNAuthorizeResult?, userDidCancel : Bool, error : Error?) -> () in
            if error != nil {
                // Handle errors from the SDK or authorization server.
            }
            else if userDidCancel {
                // Handle errors caused when user cancels login.
            }
            else {
                // Authentication was successful.
                // Obtain the access token and user profile data.
                self.accessToken = result!.token
                self.user = result!.user!
            }
        })

此外,我需要从使用 Cognito 进行身份验证的 DynamoDB 中检索信息。如文档中所述,应该有一种方法可以将 LWA 的访问令牌传递给 Cognito,但我找不到合适的位置。他们说 LWA 提供了一个 AMZNAccessTokenDelegate,但它没有。委托方法提供 Cognito 需要的 API 结果。下面 Cognito 文档中的链接与我在上面发布的 LWA 文档中的链接完全相同。

Cognito 文档:https ://docs.aws.amazon.com/cognito/latest/developerguide/amazon.html

func requestDidSucceed(apiResult: APIResult!) {
    if apiResult.api == API.AuthorizeUser {
        AIMobileLib.getAccessTokenForScopes(["profile"], withOverrideParams: nil, delegate: self)
    } else if apiResult.api == API.GetAccessToken {
        credentialsProvider.logins = [AWSCognitoLoginProviderKey.LoginWithAmazon.rawValue: apiResult.result]
    }
}

我错过了什么?

[编辑]

今天我翻遍了 LWA 的源代码,直到我终于找到了正确的委托方法。

使用 AIAuthenticationDelegate 而不是 AMZNAccessTokenDelegate

但这让我坐在接下来的两个问题面前:

我。

Value of type 'AWSCognitoCredentialsProvider' has no member 'logins'

也许我必须使用以下内容?

.setValue([AWSCognitoLoginProviderKey.LoginWithAmazon.rawValue: apiResult.result], forKey: "logins")

二、

Use of unresolved identifier 'AWSCognitoLoginProviderKey'

我在这里放什么?也许是我从 LWA 获得的 API 密钥?

[编辑2]

我想尝试一下,但从requestDidSucceed未被调用,即使我成功登录。

4

1 回答 1

0
class CustomIdentityProvider: NSObject, AWSIdentityProviderManager {
    func logins() -> AWSTask<NSDictionary> {
        return AWSTask(result: loginTokens)
    }

    var loginTokens : NSDictionary
    init(tokens: [String : String]) {
        self.loginTokens = tokens as NSDictionary
    }   
}

在下面的授权代码中成功

    AMZNAuthorizationManager.shared().authorize(request) { (result, userDidCancel, error) in
            if ((error) != nil) {
                // Handle errors from the SDK or authorization server.
            } else if (userDidCancel) {
                // Handle errors caused when user cancels login.
            } else {
                let logins = [IdentityProvider.amazon.rawValue: result!.token]
                let customProviderManager = CustomIdentityProvider(tokens: logins)
                guard let apiGatewayEndpoint = AWSEndpoint(url: URL(string: "APIGATEWAYURL")) else {
                    fatalError("Error creating API Gateway endpoint url")
                }
                let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USWest2, identityPoolId: "IDENTITY_ID", identityProviderManager:customProviderManager)
                let configuration = AWSServiceConfiguration(region: .USWest2, endpoint: apiGatewayEndpoint, credentialsProvider: credentialsProvider)
    }
于 2019-11-16T01:11:21.240 回答