AWS Cognito中的Identity Id会更改



我在客户端上使用Facebook Login和AWS Cognito建立了一个身份验证流程。我需要一个用户在dynambodb表中的facebook id的引用,并验证API调用实际上有一个有效的facebook id,这个facebook id与AWS Cognito id匹配。我在这个答案中找到了一个解决方案:AWS Cognito, Lambda, DynamoDB中的用户凭据

但是我在同一台设备上多次登录Cognito后,我发现控制台有几个不同的身份id。是这样的:

屏幕截图

我认为对于具有特定Facebook id的特定设备,身份id是唯一的。我犯了什么错误吗?如果身份id发生变化,如何在dynamoDB中存储Facebook用户的数据?

这是我的代码:

void Start()
    {
        InitCognito ();
    }
    public void InitCognito()
    {
        UnityInitializer.AttachToGameObject (this.gameObject);
        credentials = new CognitoAWSCredentials (
            identity_pool_id, // Identity Pool ID
            region // Region
        );
        Debug.Log ("identity_pool_id = " + identity_pool_id + " region = " + region);
        credentials.GetIdentityIdAsync(delegate(AmazonCognitoIdentityResult<string>
            result) {
            if (result.Exception != null) {
                Debug.LogError(result.Exception.ToString());
            }
            string identityId = result.Response;
            Debug.Log("identityId = "+identityId);
            FBInit();
        });

    }

    public void FBInit()
    {
        FB.Init(this.OnInitComplete, this.OnHideUnity);
        Debug.Log( "FB.Init() called with " + FB.AppId);
    }
    public void FBLogin()
    {
        FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, this.HandleResult);
    }

    private void OnInitComplete()
    {
        Debug.Log( "Success - Check log for details");
        Debug.Log("Success Response: OnInitComplete Calledn");
        Debug.Log( string.Format(
            "OnInitCompleteCalled IsLoggedIn='{0}' IsInitialized='{1}'",
            FB.IsLoggedIn,
            FB.IsInitialized));
        if (AccessToken.CurrentAccessToken != null)
        {
            Debug.Log("Access token = "+AccessToken.CurrentAccessToken.ToString());
        }
        FBLogin ();
    }
    private void OnHideUnity(bool isGameShown)
    {
        Debug.Log( "Success - Check log for details");
        Debug.Log(string.Format("Success Response: OnHideUnity Called {0}n", isGameShown));
        Debug.Log("Is game shown: " + isGameShown);
    }
    protected void HandleResult(IResult result)
    {
        if (result == null)
        {
            Debug.Log("Null Responsen");
            return;
        }

        // Some platforms return the empty string instead of null.
        if (!string.IsNullOrEmpty(result.Error))
        {
            Debug.Log( "Error - Check log for details");
            Debug.Log( "Error Response:n" + result.Error);
        }
        else if (result.Cancelled)
        {
            Debug.Log ("Cancelled - Check log for details");
            Debug.Log( "Cancelled Response:n" + result.RawResult);
        }
        else if (!string.IsNullOrEmpty(result.RawResult))
        {
            Debug.Log ("Success - Check log for details");
            Debug.Log ("Success Response:n" + result.RawResult);
            Debug.Log ("Access Token = "+AccessToken.CurrentAccessToken);
            Debug.Log ("Access Token = "+AccessToken.CurrentAccessToken.TokenString);
            Debug.Log ("Access User Id =" + AccessToken.CurrentAccessToken.UserId);
            credentials.AddLogin ("graph.facebook.com", AccessToken.CurrentAccessToken.TokenString);
            if (credentials.CurrentLoginProviders.Length > 0) {
                Debug.Log (credentials.CurrentLoginProviders[0]);
            }
            Debug.Log (credentials.GetCachedIdentityId());
        }
        else
        {
            Debug.Log ( "Empty Responsen");
        }

    }

当执行InitCognito()方法时,我得到一个未经授权的身份Id(一旦我在同一设备上重新安装这个应用程序,未经授权的身份Id就会改变)。我可以成功获取Facebook用户id和令牌。在执行了credentials.AddLogin()方法之后,Debug.Log (credentials.GetCachedIdentityId())显示身份Id与未经授权的身份Id相同,而不是引用Facebook Id的特定Id。我是否以错误的方式使用凭据。addlogin () ?

谢谢!

显示多个身份的原因是它们未经过身份验证。你发出了很多请求,却没有给予facebook令牌。任何带有facebook令牌的请求都将为您提供相同的身份验证id(您上面提到的那个),但您必须继续提供该令牌才能获得它。这些将映射到一个唯一的id,但是没有id的请求不会。

最新更新