【问题标题】:Amazon cognito: identity not foundAmazon cognito:找不到身份
【发布时间】:2016-05-26 20:39:17
【问题描述】:

我正在开发经过开发人员身份验证的项目。我正在尝试使用以下代码获取凭据。但它给了我以下错误。

我已将 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)

这是我的代码: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);
    }
}}

身份验证.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;
    }

}}

我已经在 PHP 中进行了尝试,我在其中执行了 getOpenIdTokenForDeveloperIdentity 、 stsClient 和 AssumerolewithWebIdentity 来获取凭据,效果很好。先感谢您。

【问题讨论】:

  • 任何想法为什么它显示错误?
  • 手动调用 GetCredentialsForIdentity 的用例是什么?凭据提供程序将为您执行此操作。事实上,您所做的 refresh() 调用已经有了。您应该能够将凭据提供程序传递给您需要的客户端。是那个失败的电话吗?

标签: java android amazon-web-services amazon-cognito


【解决方案1】:

使用 Developer Authenticated Identities 时,调用 GetCredentialsForIdentity 的登录映射应将“cognito-identity.amazonaws.com”作为键,而不是开发者提供商名称。

另外,你不应该自己调用这个方法,SDK 会处理这个。

更新:

调用getCredentialsForIdentity时,在登录映射中,当key为“cognito-identity.amazonaws.com”时,值应该是调用GetOpenIdTokenForDeveloperIdentity后从后端获取的token。您无需获取执行 AWS 操作的凭证,只需使用此凭证提供程序初始化 AWS 服务客户端即可。

【讨论】:

  • 你是说在这一行调用 GetCredentialsForIdentity cognitoIdentityClient.getCredentialsForIdentity(credentialsForIdentityRequest);如果是,那么如何从上述代码中的 cogntioidentityClient 检索这些凭据?
  • 而上面的loginmap应该是这样的 loginsMap.put("cognito-identity.amazonaws.com", "7386372772");正确的?还有在 AwsabstractcognitodeveloperIdentityprovider 类中,我是否需要输入我的开发人员名称“login.blupinch.app”?
  • 正如您所建议的,我已尝试提供 cognito-identity.amazonaws.com 作为登录密钥。它向我抛出错误,说您提供的令牌不正确。我在我创建的 cognito 中尝试使用 providername。并没有尝试打印凭据,而是将其传递给 s3 客户端并尝试将文件上传到 s3。令人惊讶的是,它奏效了。文件已上传。真的不知道我应该以什么方式理解这一点。请帮忙
  • 您是否从后端提供了正确的令牌,通过调用 GetOpenIdTokenForDeveloperIdentity 获得?另外,如果您有 requestId,我可以深入挖掘。对于它工作的情况,您能否检查它是否创建了经过身份验证的身份或未经身份验证的身份?您可以检查这是 Amazon Cognito 控制台。
  • 我确定我提供了正确的令牌。它正在创建经过身份验证的身份。我在 AWS 控制台中检查了这一点
【解决方案2】:

(1) 在您的应用程序中,您不需要调用 GetCredentialsForIdentity,CredentialsProvider 会为您完成所有这些工作。 (并确保传入正确的参数)。
--> 您可以通过调用另一个需要凭据的服务(例如 S3)来测试您是否正在获取凭据
--> 另一种选择是在 Credentials Provider 上调用 getCredentials() 并验证没有抛出异常

(2) 在 Authentication.java 中,刷新一下,你似乎使用的是硬编码令牌?这可能已过期,您应该确保获得新的。 (如果您只是在测试应用程序端,请确保您硬编码一个新的)。

(3) 在 Authentication.java 中,刷新,看起来您正在使用 null 令牌调用更新。

(4) 还要确保您正在获取您正在使用的硬编码身份的令牌。 (同样,只有在测试时才应该对其进行硬编码)。

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 2017-06-08
    • 2018-02-16
    • 2015-06-01
    • 2016-08-27
    • 2016-09-26
    • 2016-10-23
    • 2014-09-26
    • 1970-01-01
    相关资源
    最近更新 更多