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,請隨時指出我實際解釋這一點 –

相關問題