0

我在客户端上使用Facebook登录和AWS Cognito建立了身份验证流程。我需要用户在其dynambodb表中使用其facebook代码的引用,并验证API调用实际上是否有一个有效的Facebook ID,此Facebook ID与AWS Cognito Id匹配。 我在这个答案中找到了一个解决方案:AWS Cognito, Lambda, User credentials in DynamoDBAWS Cognito更改中的身份标识

但我在Cognito登录多次后,在同一台设备上,我发现在控制台中有几个不同的身份标识。像这样: screen shot

我认为身份证对于某个具有某个Facebook ID的设备是唯一的。我犯了一些错误吗?如果身份标识发生变化,我如何将用户的数据存储在dynamoDB中?

这是我的代码:

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 Called\n"); 
     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 Response\n"); 

      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 Response\n"); 
     } 


    } 

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

谢谢!

+1

点击这些身份。他们有登录链接到他们吗? –

+0

其中两个已连接登录。 “链接登录”字段在第一个字段中显示“graph.facebook.com”,在第二个字段中显示“DISABLED us-east-1”。 – DogJunior

回答

1

你有多个身份显示的原因是他们未经过身份验证。您正在提交大量请求而未提供Facebook令牌。任何使用Facebook标记的请求都会给你同样的身份验证标识(上面提到的标识),但是您必须继续提供该标记才能获取它。那些将映射到一个唯一的ID,但没有人的请求不会。

+0

我认为你是对的。但是有时候,经过身份验证的ID与未经身份验证的ID一样。也许我的代码有一些错误。我已经在问题中粘贴了我的代码。你能再帮我一次吗?谢谢! – DogJunior

+0

要继续杰夫的解释,您首先得到未经身份验证的身份标识。当您将FB令牌和idenA提供给服务时,它会将登录链接到此身份,因此idenA现在是经过身份验证的身份。下一次,如果您不提供FB令牌,您将获得一个新的未经身份验证的身份标识符。如果现在为同一用户提供FB令牌,则会返回idenA。如果FB令牌属于不同的用户,idenB现在将链接到这个新用户。 –

+0

对于随后的Lambda调用,是否会再次传递FB令牌?或者将[提供] .. FB标记和idenA给[??????]服务“返回一个会话标记,然后发回给呼叫者,并且期望呼叫者将这个标记发送给未来的呼叫? 如果这太n00b,请随时指出我实际解释这一点 –

相关问题