简体   繁体   English

Amazon Cognito:找不到身份

[英]Amazon cognito: identity not found

I am working on developer authenticated project.And I am trying to get credentials using this below code.But it is giving me following error. 我正在开发开发人员验证的项目。并且我正在尝试使用下面的代码获取凭据。但这给了我以下错误。

I have pasted IdentityId and Token manually into this code from server code: 我已经从服务器代码中手动将IdentityId和Token粘贴到此代码中:

    Caused by: com.amazonaws.services.cognitoidentity.model.ResourceNotFoundException: Identity 'ap-northeast-1:fe81cd76-e9d4-4416-99ea-b684b78743c8' not found. (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: 2ac65fe8-d41a-11e5-8674-677eefdb5331)
                                                 at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:709)
                                                 at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:385)
                                                 at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:196)
                                                 at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(AmazonCognitoIdentityClient.java:533)
                                                 at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getCredentialsForIdentity(AmazonCognitoIdentityClient.java:406)
                                                 at com.example.sandesh.aws.MainActivity$network.doInBackground(MainActivity.java:101)
                                                 at com.example.sandesh.aws.MainActivity$network.doInBackground(MainActivity.java:52)
                                                 at android.os.AsyncTask$2.call(AsyncTask.java:292)

Here is my code: MainActivity.java 这是我的代码:MainActivity.java

    public class MainActivity extends AppCompatActivity {

Button button;

protected static CognitoCachingCredentialsProvider credentialsProvider = null;
private GetCredentialsForIdentityResult credentialsForIdentityResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            network net = new network();
            net.execute();
        }
    });
}
public class network extends AsyncTask<Void,Void,String>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Void... params) {

        Authentication developerProvider = new Authentication(
                null,
                "ap-northeast-1:XXXXXXXXXXXXXXXXXX",
                Regions.AP_NORTHEAST_1);

        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(),
                developerProvider,
                Regions.AP_NORTHEAST_1);


        HashMap<String, String> loginsMap = new HashMap<String, String>();
        loginsMap.put(developerProvider.getProviderName(), "7386372772");
        credentialsProvider.setLogins(loginsMap);
        credentialsProvider.refresh();
        GetCredentialsForIdentityRequest credentialsForIdentityRequest = new GetCredentialsForIdentityRequest();

        credentialsForIdentityRequest.setIdentityId(developerProvider.getIdentityId());
        credentialsForIdentityRequest.setLogins(loginsMap);
        AmazonCognitoIdentityClient cognitoIdentityClient = new AmazonCognitoIdentityClient(credentialsProvider);
        credentialsForIdentityResult = cognitoIdentityClient.getCredentialsForIdentity(credentialsForIdentityRequest);

        Log.d("access_key",credentialsForIdentityResult.getCredentials().getAccessKeyId());
        return credentialsForIdentityResult.getCredentials().getAccessKeyId();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }
}}

Authentication.java Authentication.java

   public class Authentication extends AWSAbstractCognitoDeveloperIdentityProvider {


private static final String PROVIDERNAME = "login.blupinch.app";
public String response = " ";
String line = " ";
public Authentication(String accountId, String identityPoolId, Regions region) {
    super(accountId, identityPoolId, region);
}

@Override
public String getProviderName() {
    return PROVIDERNAME;
}
public String refresh() {

    setToken(null);

    if (getProviderName() != null &&
            !this.loginsMap.isEmpty() &&
            this.loginsMap.containsKey(getProviderName())) {

        update(identityId, token);
        return "eyJraWQiOiJhcC1ub3J0aGVhc3QtMTEiLCJ0eXAiOiJKV1MiLCJhbGciOiJSUzUxMiJ9.eyJzdWIiOiJhcC1ub3J0aGVhc3QtMTpmZTgxY2Q3Ni1lOWQ0LTQ0MTYtOTllYS1iNjg0Yjc4NzQzYzgiLCJhdWQiOiJhcC1ub3J0aGVhc3QtMTphODcxZmE1Zi0yM2EyLTQ4MGQtYmFhNi1iNGVkMzE0MzcyNDQilCJhbXIiOlsiYXV0aGVudGljYXRlZCIsImxvZ2luLmJsdXBpbmNoLmFwcCIsImxvZ2luLmJsdXBpbmNoLmFwcDphcC1ub3J0aGVhc3QtMTphODcxZmE1Zi0yM2EyLTQ4MGQtYmFhNi1iNGVkMzE0MzcyNDQ6NzM4NjM3Mjg3MiJdLCJpc3MiOiJodHRwczovL2NvZ25pdG8taWRlbnRpdHkuYW1hem9uYXdzLmNvbSIsImV4cCI6MTQ1NTU5NTM5NywiaWF0IjoxNDU1NTU5Mzk3fQ.fHHH6aeCn5EaJGxGD6tw7bWyQpPHuYcW8SZLRGVn-3cbamJrWEPmUnNvcLJ-D6nL8AvMQy7-s1LGQ5MNaiuIH7QF6W8aWt2OMALmA_Y7eqpGQ6iQXVma_jTZSpiyBe2cPNggWgeslPtFxomwE90vW0rzS1sY3D5Y3UbnrIHNdiPKIzzP9JaQo1IsTJMKEpQM-jzWP6stV1radDuIzWQroBVQseOQSD-MXV_-cgWWSx0eQmtFbjJW6RP_nACgh0uTbGmMuOi2iKXKQAdGlYWO-PHlShbiHT-WLQoZNWuh95Hh9dMldv-mNdnYSblqYyqptLA3kObioI08XXkTqwaaAw";


    } else {
        this.getIdentityId();
        return null;
    }
}
public String getIdentityId() {

    identityId = "ap-northeast-1:XXXXXXXXXXXXXXXXXXXXXXXX";

    if (identityId == null) {


        if (getProviderName() != null && !this.loginsMap.isEmpty()
                && this.loginsMap.containsKey(getProviderName())) {

            update(identityId, token);
            return "ap-northeast-1:XXXXXXXXXXXXXXXXXXXX";

        } else {
            return super.getIdentityId();
        }

    } else {
        return identityId;
    }

}}

I have tried it in PHP where I performed getOpenIdTokenForDeveloperIdentity , stsClient and AssumerolewithWebIdentity to get credentials it worked great. 我已经在PHP中进行了尝试,在PHP中我执行了getOpenIdTokenForDeveloperIdentity,stsClient和AssumerolewithWebIdentity以获取效果很好的凭证。 Thank you in advance. 先感谢您。

When using Developer Authenticated Identities, the logins map for the call GetCredentialsForIdentity should have "cognito-identity.amazonaws.com" as the key instead of developer provider name. 使用开发人员验证身份时,调用GetCredentialsForIdentity的登录名映射应以“ cognito-identity.amazonaws.com”作为键,而不是开发人员提供者名称。

Also, you should not be calling this method yourself, the SDK will take care of this. 另外,您不应该自己调用此方法,SDK会解决这个问题。

Update: 更新:

When calling getCredentialsForIdentity, in the logins map, when key is "cognito-identity.amazonaws.com", the value should be the token you got from your backend after calling GetOpenIdTokenForDeveloperIdentity. 调用getCredentialsForIdentity时,在登录映射中,当键为“ cognito-identity.amazonaws.com”时,该值应该是您在调用GetOpenIdTokenForDeveloperIdentity之后从后端获取的令牌。 You do not need to get the credentials for doing AWS operations, just initialize the AWS Service Client with this credentials provider and you should be good to go. 您无需获取执行AWS操作的凭证,只需使用此凭证提供程序初始化AWS Service Client,您就可以开始使用了。

(1) In your application, you do not need to call GetCredentialsForIdentity, the CredentialsProvider does all of this for you. (1)在您的应用程序中,您不需要调用GetCredentialsForIdentity,CredentialsProvider会为您完成所有这一切。 (And ensures the correct arguments are passed in). (并确保传递正确的参数)。
--> You can test that you are getting credentials by making a call to another service that requires credentials (ex S3) ->您可以通过调用另一个需要凭据的服务(例如S3)来测试您是否正在获取凭据
--> Another option is to call getCredentials() on the Credentials Provider and validate that an exception is not thrown ->另一个选项是在凭据提供程序上调用getCredentials()并验证未引发异常

(2) In Authentication.java, refresh, you seem to be using a hard coded token? (2)在Authentication.java中刷新,您似乎正在使用硬编码令牌? This is likely expired, and you should make sure to get new ones. 这可能已过期,因此您应该确保获取新的。 (If you are just testing app side, make sure you hard code a fresh one). (如果您只是在测试应用程序方面,请确保对新代码进行硬编码)。

(3) In Authentication.java, refresh, it looks like you are calling update with a null token. (3)在Authentication.java中,刷新时,看起来您正在调用带有空令牌的update。

(4) Also make sure you are getting tokens for the hard coded identity you are using. (4)还要确保您正在获取所使用的硬编码身份的令牌。 (Again, you should only be hard coding this if you are testing). (同样,只有在进行测试时,才应该对此进行硬编码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM