Java amazoncognito中的开发者身份验证

Java amazoncognito中的开发者身份验证,java,android,amazon-web-services,authentication,amazon-cognito,Java,Android,Amazon Web Services,Authentication,Amazon Cognito,我试着跟随和指导,但我不确定这是否是应该如何做到的。我使用的是CognitoSync,当我尝试启动同步服务时,我遇到了“标识id和数据集名称不唯一”的异常 在Amazon控制台上,我创建了一个IdentityPool,并将服务器端类的包名命名为DeveloperProvider,即“com.leversystems.devauth”,并将其用于服务器类以及Android应用程序。 在服务器类中 map.put(“com.leversystems.devauth”,“someUniqueId”)

我试着跟随和指导,但我不确定这是否是应该如何做到的。我使用的是CognitoSync,当我尝试启动同步服务时,我遇到了“标识id和数据集名称不唯一”的异常

在Amazon控制台上,我创建了一个IdentityPool,并将服务器端类的包名命名为DeveloperProvider,即“com.leversystems.devauth”,并将其用于服务器类以及Android应用程序。 在服务器类中

map.put(“com.leversystems.devauth”,“someUniqueId”)

在Android应用程序中

logins.put(“com.leversystems.devauth”,cognitoProvider.getToken())

这是我的密码

Java服务器端

package com.leversystems.devauth;
import java.util.HashMap;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient;
import com.amazonaws.services.cognitoidentity.model.GetOpenIdTokenForDeveloperIdentityRequest;
import com.amazonaws.services.cognitoidentity.model.GetOpenIdTokenForDeveloperIdentityResult;

public class DevAuth {

    private BasicAWSCredentials credentials = null;

    private String myAwsAccessKey = "myaccesskey";
    private String myAwsSecretKey = "mysecretkey";
    private String identityPoolID = "identityPoolid";
    private String authARN = "arn:aws:iam::782936514542:role/DefaultRole";

    private String identityId;
    private String token;

    public DevAuth()
    {
        identityId = "No id has been set yet!";
        token = "No token has been set yet!";
        initializeSecurity();
    }

    public String getToken()
    {
        return this.token;
    }

    public String getIdentityId()
    {
        return this.identityId;     
    }

    public void initializeSecurity(){

        credentials = new BasicAWSCredentials(myAwsAccessKey , myAwsSecretKey);
        AmazonCognitoIdentityClient client = 
          new AmazonCognitoIdentityClient(credentials);
        GetOpenIdTokenForDeveloperIdentityRequest tokenRequest = 
          new GetOpenIdTokenForDeveloperIdentityRequest();
        tokenRequest.setIdentityPoolId(identityPoolID);
        HashMap<String, String> map = new HashMap<String, String>();

        map.put("com.leversystems.devauth", "nameid.number@provider.com");

        //Duration of the generated OpenID Connect Token
        tokenRequest.setLogins(map);

        tokenRequest.setTokenDuration(1000l);

        GetOpenIdTokenForDeveloperIdentityResult result 
           = client.getOpenIdTokenForDeveloperIdentity(tokenRequest);
        this.identityId = result.getIdentityId();
        this.token = result.getToken();
    }}
工作流程

  • 在android应用程序中,当单击按钮时,getCredentials()被调用,它从Java服务器类获取令牌和id
  • 令牌和Id然后被传递到BYOIProvider类以更新它们
  • 调用syncData(),初始化CognitoCredentialProvider和BYOIProvider变量
  • CognitoSyncManager使用CognitoCredentialProvider的对象初始化
  • 在此之后,我收到一个异常错误
  • 列标识\ id和数据集\名称不唯一

    在这行代码上

    dataset=client.openOrCreateDataset(“MyData”)

    更新-1(2014年11月11日)

    好的,我现在已经将refresh()的返回字符串更改为token变量,这是stacktrace

    11-11 12:58:52.196: I/View(29237): Touch down dispatch to android.widget.Button{4186d880 VFED..C. ........ 206,342-417,438 #7f080003 app:id/button1}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=76.0, y[0]=34.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=228482273, downTime=228482273, deviceId=2, source=0x1002 }
    11-11 12:58:52.213: D/GraphicBuffer(29237): create handle(0x5ed83e60) (w:720, h:1280, f:1)
    11-11 12:58:52.222: I/SurfaceTextureClient(29237): [STC::queueBuffer] (this:0x5d12eb78) fps:0.10, dur:20282.80, max:20162.90, min:119.90
    11-11 12:58:52.222: I/SurfaceTextureClient(29237): [STC::queueBuffer] this:0x5d12eb78, api:1, last queue time elapsed:20162.90
    11-11 12:58:52.326: I/View(29237): Touch up dispatch to android.widget.Button{4186d880 VFED..C. ...P.... 206,342-417,438 #7f080003 app:id/button1}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=76.0, y[0]=34.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=228482404, downTime=228482273, deviceId=2, source=0x1002 }
    11-11 12:58:52.327: V/Provider/Settings(29237):  from settings cache , name = sound_effects_enabled , value = 0
    11-11 12:58:52.328: D/dalvikvm(29237): create interp thread : stack size=128KB
    11-11 12:58:52.328: D/dalvikvm(29237): create new thread
    11-11 12:58:52.328: D/dalvikvm(29237): new thread created
    11-11 12:58:52.328: D/dalvikvm(29237): update thread list
    11-11 12:58:52.329: D/dalvikvm(29237): threadid=15: interp stack at 0x60115000
    11-11 12:58:52.329: D/dalvikvm(29237): threadid=15: created from interp
    11-11 12:58:52.329: D/dalvikvm(29237): start new thread
    11-11 12:58:52.329: D/dalvikvm(29237): threadid=15: notify debugger
    11-11 12:58:52.329: D/dalvikvm(29237): threadid=15 (RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@419852e8): calling run()
    11-11 12:58:52.330: I/System.out(29237): httpget:http://192.168.1.112:8080/AuthenticationService/services/auth/gctbda
    11-11 12:58:52.331: I/System.out(29237): http://192.168.1.112:8080/AuthenticationService/services/auth/gctbda
    11-11 12:58:52.331: D/dalvikvm(29237): create interp thread : stack size=128KB
    11-11 12:58:52.331: D/dalvikvm(29237): create new thread
    11-11 12:58:52.332: D/dalvikvm(29237): new thread created
    11-11 12:58:52.332: D/dalvikvm(29237): update thread list
    11-11 12:58:52.332: D/dalvikvm(29237): threadid=16: interp stack at 0x60235000
    11-11 12:58:52.332: D/dalvikvm(29237): threadid=16: created from interp
    11-11 12:58:52.332: D/dalvikvm(29237): start new thread
    11-11 12:58:52.333: D/dalvikvm(29237): threadid=16: notify debugger
    11-11 12:58:52.333: D/dalvikvm(29237): threadid=16 (pool-3-thread-1): calling run()
    11-11 12:58:52.336: I/System.out(29237): [socket][1] connection /192.168.1.112:8080;LocalPort=35830(10000)
    11-11 12:58:52.336: I/System.out(29237): [CDS]connect[/192.168.1.112:8080] tm:10
    11-11 12:58:52.336: D/Posix(29237): [Posix_connect Debug]Process com.leversystems.authserver :8080 
    11-11 12:58:52.358: I/System.out(29237): [socket][/192.168.1.136:35830] connected
    11-11 12:58:52.358: I/System.out(29237): [CDS]rx timeout:10000
    11-11 12:58:52.358: I/System.out(29237): [CDS]SO_SND_TIMEOUT:0
    11-11 12:58:52.360: I/System.out(29237): >doSendRequest
    11-11 12:58:52.361: I/System.out(29237): <doSendRequest
    11-11 12:58:53.259: I/AmazonWebServiceClient(29237): {cognito-sync, us-east-1} was not found in region metadata, trying to construct an endpoint using the standard pattern for this region: 'cognito-sync.us-east-1.amazonaws.com'.
    11-11 12:58:53.259: D/CognitoCachingCredentialsProvider(29237): Identity id is changed
    11-11 12:58:53.259: D/CognitoCachingCredentialsProvider(29237): Saving identity id to SharedPreferences
    11-11 12:58:53.260: I/CognitoSyncManager(29237): identity change detected
    11-11 12:58:53.271: W/System.err(29237): Exception in OnSuccess: columns identity_id, dataset_name are not unique (code 19)
    
    更新-3(2014年11月13日)

    好的,我在IAM控制台上创建了一个新角色。更新-2中的异常现在已清除。下一个问题与第一个问题相同。我在AsyncHttpClient内部收到的确切异常是

    11-13 15:40:41.738: I/AmazonWebServiceClient(23921): {cognito-sync, us-east-1} was not found in region metadata, trying to construct an endpoint using the standard pattern for this region: 'cognito-sync.us-east-1.amazonaws.com'.
    11-13 15:40:41.739: D/CognitoCachingCredentialsProvider(23921): Identity id is changed
    11-13 15:40:41.739: D/CognitoCachingCredentialsProvider(23921): Saving identity id to SharedPreferences
    11-13 15:40:41.740: I/CognitoSyncManager(23921): identity change detected
    11-13 15:40:41.749: W/System.err(23921): Exception in OnSuccess: columns identity_id, dataset_name are not unique (code 19)
    
    更新-4(2014年11月14日)

    我的更新代码和StackTrace

    代码:

    堆栈跟踪:


    正如Yangfan提到的,确保BYOIProvider.refresh()返回标记而不是null是很重要的。CognitoCachingCredentialsProvider在内部调用它,并使用该令牌。理想情况下,与服务器的通信将在BYOIProvider类中进行,因为来自该类的方法调用将在CognotCachingCredentialsProvider中使用

    那个更新电话很重要。它是通过并触发适当的身份更改处理(可能是导致问题的原因)和更新令牌的过程。确实需要调用它,并在执行此操作时具有适当的令牌和标识ID。如果在所有适当的时间使用getCredentials()调用,就足以确保它返回令牌,但是如果要将与服务器通信的部分代码移动到刷新调用(更新上方)内部并更新返回,它将自行处理

    如果这不能解决您的问题,您可以发布stacktrace吗

    编辑:

    好吧,我看到了另一种可能性。AWSAbstractCognitoIdentityProvider实际上跟踪identityId和令牌-这些是凭据提供程序内部使用的。这可能意味着凭据提供程序没有获取您正在设置的标识ID和令牌。这些可以通过getter和setter访问

    此外,在setLogins()/withLogins()的文档中,它指出在向凭据提供程序添加登录名后,应手动调用凭据提供程序上的刷新,因为您的身份id可能已更改。用户的身份id在从未经身份验证变为经过身份验证后会发生很大变化

    编辑2: 当一个标识发生变化时,会发生一些事情——其中之一是本地存储在数据库中的数据集,需要从链接到旧标识id的位置删除并添加到新标识id中。激活身份更改的侦听器时会触发该操作。身份更改侦听器在初始化时注册到凭据提供程序,但在来自身份提供程序的更新调用时激活。这就是为什么顺序必须是它所做的,因此数据集使用适当的标识id保存,并且避免了此错误

    错误本身可能由以下工作流引起: 1) 使用经过身份验证的标识b保存数据集a 2) 使用标识id c保存数据集a(很可能未经身份验证) 3) 身份验证返回到标识id b。这会将保存的数据集从c重新设置为b的父级,并导致出现非唯一错误


    因此,基本上,您必须确保在保存时使用适当的标识id进行保存,以便对其进行适当处理。

    请共享stacktrace好吗?代码中有一个小问题:BYOIProvider.refresh()应该返回刷新的令牌,而不是null。请检查有问题的更新。谢谢。不幸的是,它似乎只有几行与Cognito相关,而且它们是信息性的,与错误没有太大关系。你还在犯同样的错误吗?如果是这样,您是否有权访问堆栈跟踪中显示错误的部分?在您更新这些内容后,是否可以尝试擦除应用程序数据?您所面临的问题可能不会随着这些修复而消失,除非您这样做,因为它已保存到本地数据库中。谢谢,我从一开始就一直在擦除数据,请检查更新-2现在我已经更新了-3,还更新了mainActivity中的synchData()。请检查这些。该错误只会出现多个保存-您确定要清除所有保存吗?导致工作流的示例:1)使用经过身份验证的标识b保存数据集a 2)使用标识c保存数据集a(很可能未经身份验证)3)通过身份验证返回标识b。这会将保存的数据集从c重新设置为b的父级,并导致出现非唯一错误。
    11-11 12:58:52.196: I/View(29237): Touch down dispatch to android.widget.Button{4186d880 VFED..C. ........ 206,342-417,438 #7f080003 app:id/button1}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=76.0, y[0]=34.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=228482273, downTime=228482273, deviceId=2, source=0x1002 }
    11-11 12:58:52.213: D/GraphicBuffer(29237): create handle(0x5ed83e60) (w:720, h:1280, f:1)
    11-11 12:58:52.222: I/SurfaceTextureClient(29237): [STC::queueBuffer] (this:0x5d12eb78) fps:0.10, dur:20282.80, max:20162.90, min:119.90
    11-11 12:58:52.222: I/SurfaceTextureClient(29237): [STC::queueBuffer] this:0x5d12eb78, api:1, last queue time elapsed:20162.90
    11-11 12:58:52.326: I/View(29237): Touch up dispatch to android.widget.Button{4186d880 VFED..C. ...P.... 206,342-417,438 #7f080003 app:id/button1}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=76.0, y[0]=34.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=228482404, downTime=228482273, deviceId=2, source=0x1002 }
    11-11 12:58:52.327: V/Provider/Settings(29237):  from settings cache , name = sound_effects_enabled , value = 0
    11-11 12:58:52.328: D/dalvikvm(29237): create interp thread : stack size=128KB
    11-11 12:58:52.328: D/dalvikvm(29237): create new thread
    11-11 12:58:52.328: D/dalvikvm(29237): new thread created
    11-11 12:58:52.328: D/dalvikvm(29237): update thread list
    11-11 12:58:52.329: D/dalvikvm(29237): threadid=15: interp stack at 0x60115000
    11-11 12:58:52.329: D/dalvikvm(29237): threadid=15: created from interp
    11-11 12:58:52.329: D/dalvikvm(29237): start new thread
    11-11 12:58:52.329: D/dalvikvm(29237): threadid=15: notify debugger
    11-11 12:58:52.329: D/dalvikvm(29237): threadid=15 (RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@419852e8): calling run()
    11-11 12:58:52.330: I/System.out(29237): httpget:http://192.168.1.112:8080/AuthenticationService/services/auth/gctbda
    11-11 12:58:52.331: I/System.out(29237): http://192.168.1.112:8080/AuthenticationService/services/auth/gctbda
    11-11 12:58:52.331: D/dalvikvm(29237): create interp thread : stack size=128KB
    11-11 12:58:52.331: D/dalvikvm(29237): create new thread
    11-11 12:58:52.332: D/dalvikvm(29237): new thread created
    11-11 12:58:52.332: D/dalvikvm(29237): update thread list
    11-11 12:58:52.332: D/dalvikvm(29237): threadid=16: interp stack at 0x60235000
    11-11 12:58:52.332: D/dalvikvm(29237): threadid=16: created from interp
    11-11 12:58:52.332: D/dalvikvm(29237): start new thread
    11-11 12:58:52.333: D/dalvikvm(29237): threadid=16: notify debugger
    11-11 12:58:52.333: D/dalvikvm(29237): threadid=16 (pool-3-thread-1): calling run()
    11-11 12:58:52.336: I/System.out(29237): [socket][1] connection /192.168.1.112:8080;LocalPort=35830(10000)
    11-11 12:58:52.336: I/System.out(29237): [CDS]connect[/192.168.1.112:8080] tm:10
    11-11 12:58:52.336: D/Posix(29237): [Posix_connect Debug]Process com.leversystems.authserver :8080 
    11-11 12:58:52.358: I/System.out(29237): [socket][/192.168.1.136:35830] connected
    11-11 12:58:52.358: I/System.out(29237): [CDS]rx timeout:10000
    11-11 12:58:52.358: I/System.out(29237): [CDS]SO_SND_TIMEOUT:0
    11-11 12:58:52.360: I/System.out(29237): >doSendRequest
    11-11 12:58:52.361: I/System.out(29237): <doSendRequest
    11-11 12:58:53.259: I/AmazonWebServiceClient(29237): {cognito-sync, us-east-1} was not found in region metadata, trying to construct an endpoint using the standard pattern for this region: 'cognito-sync.us-east-1.amazonaws.com'.
    11-11 12:58:53.259: D/CognitoCachingCredentialsProvider(29237): Identity id is changed
    11-11 12:58:53.259: D/CognitoCachingCredentialsProvider(29237): Saving identity id to SharedPreferences
    11-11 12:58:53.260: I/CognitoSyncManager(29237): identity change detected
    11-11 12:58:53.271: W/System.err(29237): Exception in OnSuccess: columns identity_id, dataset_name are not unique (code 19)
    
    Exception in onSuccess: Not authorized to perform sts:AssumeRoleWithWebIdentity (Service: AWSSecurityTokenService; Status Code: 403; Error Code: AccessDenied; Request ID: *SomeID*)
    
    11-13 15:40:41.738: I/AmazonWebServiceClient(23921): {cognito-sync, us-east-1} was not found in region metadata, trying to construct an endpoint using the standard pattern for this region: 'cognito-sync.us-east-1.amazonaws.com'.
    11-13 15:40:41.739: D/CognitoCachingCredentialsProvider(23921): Identity id is changed
    11-13 15:40:41.739: D/CognitoCachingCredentialsProvider(23921): Saving identity id to SharedPreferences
    11-13 15:40:41.740: I/CognitoSyncManager(23921): identity change detected
    11-13 15:40:41.749: W/System.err(23921): Exception in OnSuccess: columns identity_id, dataset_name are not unique (code 19)
    
    credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(), cognitoProvider, null,
                AUTHORIZATION_ARN);
    
    cognitoProvider.refresh();
    
    11-14 11:25:01.357: I/AmazonWebServiceClient(31084): {cognito-sync, us-east-1} was not found in region metadata, trying to construct an endpoint using the standard pattern for this region: 'cognito-sync.us-east-1.amazonaws.com'.
    11-14 11:25:01.358: D/CognitoCachingCredentialsProvider(31084): Identity id is changed
    11-14 11:25:01.358: D/CognitoCachingCredentialsProvider(31084): Saving identity id to SharedPreferences
    11-14 11:25:01.358: I/CognitoSyncManager(31084): identity change detected
    11-14 11:25:01.367: W/System.err(31084): Exception in OnSuccess: columns identity_id, dataset_name, key are not unique (code 19)