ios swift AWS Cognito 和 Facebook 身份验证

ios swift AWS cognito and Facebook Authentication

我正在尝试使用 Facebook 凭据登录 AWS。我已经使用 Facebook 作为我在 Amazon Cognito 上的社交身份提供商设置了一个联合身份,但由于某种原因它没有进行身份验证。亚马逊上的文档不是最新的,所以我不确定该怎么做。

下面是来自 ios 应用程序的一些 swift 代码:

let token = FBSDKAccessToken.currentAccessToken().tokenString

    let provider = MyProvider(tokens: [AWSIdentityProviderFacebook: token])


    let credentialsProvider = AWSCognitoCredentialsProvider(regionType: region, identityPoolId: poolId, identityProviderManager: provider)


    let configuration = AWSServiceConfiguration(region: region, credentialsProvider:credentialsProvider)

    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

    let id = credentialsProvider.getIdentityId()

这是 MyProvider class:

import Foundation
import AWSCognitoIdentityProvider

class MyProvider:NSObject, AWSIdentityProviderManager{
     var tokens : [NSString : NSString]?
     init(tokens: [NSString : NSString]) 
     {
         self.tokens = tokens
     }
    @objc func logins() -> AWSTask {

        return AWSTask(result: tokens)
    }
}

我能够从 Facebook 获取令牌并登录,但是当我将此令牌传递给 Cognito 时,出现类似 "NotAuthorizedException" "Token is not from a supported provider of this identity pool." 的错误,我传递给 AWSCognitoCredentialsProvider 的池 ID 和区域匹配在 Amazon Cognito 上,我在 MyProvider 中定义的方法被调用。如果有人让它工作,请分享你做了什么让它工作。我将不胜感激任何建议和帮助!

您的登录映射中的键应该是 graph.facebook.com。尝试使用 AWSCognitoLoginProviderKey.Facebook.rawValue 而不是 AWSIdentityProviderFacebook

以某种方式解决了我的问题。我所做的只是删除了我的身份池和 Facebook 应用程序并创建了新的。它现在工作正常。

我正在尝试进行 Facebook 身份验证和 Cognito 登录。我做了几乎和你一样的代码。下面是我的代码。

@IBAction func SigninWithFaceBook(_ sender: Any) {
    let loginManager: FBSDKLoginManager = FBSDKLoginManager()
    loginManager.logIn(withReadPermissions: ["public_profile", "email", "user_birthday", "user_gender"], from: self) { (result, error) in
        if error != nil {
             print("error")
        } else if (result?.isCancelled)! {
            print("Cancelled")
        } else {
            let loginResult: FBSDKLoginManagerLoginResult = result!
            if loginResult.grantedPermissions != nil {
                if loginResult.grantedPermissions.contains("email") {
                    if FBSDKAccessToken.current() != nil {
                        let tokenString = FBSDKAccessToken.current().tokenString
                        print("FBSDKAccessToken.current() \(FBSDKAccessToken.current()) string \(String(describing: tokenString)) )")
                        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email, gender, birthday "]).start(completionHandler: { (connection, result, error) -> Void in
                            if error == nil {
                                let json = result as! [String : Any]
                                 print("FBSDKGraphRequest \(json)")
                                let email = json["email"] as! String
                                let id = json["id"] as! String
                                let name = json["name"] as! String
                                let gender = json["gender"] as! String
                                let birthdate = json["birthday"] as! String
                                print("facebook login suceseeded email \(email)) id \(id) id \(name) gender \(gender) birthData \(birthdate)  ")

                            //// Sync Cognito UserPool for facebook login //////////////////
                                let customcedentialProvider = CustomIdentityProvider(tokens: ["graph.facebook.com" : tokenString!])
                                let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoIdentityUserPoolRegion,
                                                                                        identityPoolId: CognitoFederatedIdentityUserPoolID,
                                                                                        unauthRoleArn: CognitoFedratedIAMRoleUnauthARN,
                                                                                        authRoleArn: CognitoFedratedIAMRoleAuthARN,
                                                                                        identityProviderManager: customcedentialProvider)
                                let configuration = AWSServiceConfiguration(region:CognitoIdentityUserPoolRegion, credentialsProvider:credentialsProvider)
                                AWSServiceManager.default().defaultServiceConfiguration = configuration
                                credentialsProvider.getIdentityId()
                                print("credentialsProvider.getIdentityId() \(credentialsProvider.getIdentityId())")

                                self.navigationController?.popToRootViewController(animated: true)
                                self.dismiss(animated: true, completion: nil)
                                print("didCompleteFBLogin  ~~~~~ No Error ----- and dismiss")

                            }else{
                                print("FBSDKGraphRequest error")
                            }
                        })
                    } else {
                        print("Token is nil")
                    }
                    loginManager.logOut()
                } else {
                    print("It can not get user email from this Facebook user")
                }
            } else {
                print("Permission is not granted")
            }
        }
    }
}

我不确定 Cognito 登录是否有效。 打印我们的 "credentialsProvider.getIdentityId()" 已完成如下,但没有在 FederatedAppAuthRole 的 Cognito 联合身份池中的 Cognito 用户中注册用户池。

credentialsProvider.getIdentityId() AWS任务:0x1c5e78d00;完成=是;已取消 = 否;故障 = 否;结果 = ap-northeast-1:2fd1e4aa-97f4-XXXX-XXXX-XXXXXXXXXXXX

请让我知道您的建议。