2016-09-30 105 views
0

我想使用AWS Cognito Unity SDK通過Facebook登錄認證用戶。如何使用AWS Cognito進行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 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用戶標識和令牌。

遵循Cognito開發者指南,我使用credentials.AddLogin()添加Facebook登錄。但執行此方法後,Debug.Log (credentials.GetCachedIdentityId())顯示身份標識與未經授權的身份標識相同,而不是指向Facebook標識的特定標識,並且AWS Cognito控制檯顯示沒有「鏈接的登錄」。我是否以錯誤的方式使用credentials.AddLogin()

謝謝!

回答

0

設置登錄標記並不一定意味着SDK將其發送到服務器。你可以嘗試運行下面的命令,這應該強制它?我猜你看到相同身份的原因是它沒有將服務器更新到更改。它從Cognito developer guide中拉出。

credentials.GetIdentityIdAsync(delegate(AmazonCognitoIdentityResult<string> result) { 
    if (result.Exception != null) { 
     //Exception! 
    } 
    string identityId = result.Response; 
}); 
相關問題