如何使用AWS Cognito对Facebook Id进行身份验证?

时间:2016-09-30 11:28:17

标签: facebook amazon-web-services unity3d aws-sdk amazon-cognito

我想使用AWS Cognito Unity SDK通过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()方法时,我会获得一个unAuthorized Identity Id(一旦我在同一设备上重新安装此应用程序,unAuthorized Identity Id就会更改)。然后我就可以成功获得Facebook用户ID和令牌。

根据Cognito开发人员指南,我使用credentials.AddLogin()添加Facebook登录信息。但是在执行此方法之后,Debug.Log (credentials.GetCachedIdentityId())显示Identity Id与unAuthorized Identity Id相同,而不是引用Facebook Id的特定id,AWS Cognito控制台显示没有“Linked Login” ”。我是否以错误的方式使用credentials.AddLogin()

谢谢!

1 个答案:

答案 0 :(得分:0)

设置登录令牌并不一定意味着SDK正在将其发送到服务器。你能尝试运行下面的命令吗?我猜你看到相同的身份的原因是它没有更新服务器的变化。它来自Cognito developer guide

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