Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttachPolicyRequest using programmatically not working in android #1812

Closed
amarpulli1994 opened this issue Jun 3, 2020 · 9 comments
Closed
Labels
iot Issues with the AWS Android SDK for Internet of Things (IoT) mobile client Issues with AWS Mobile's client-side Cognito wrapper question General question

Comments

@amarpulli1994
Copy link

Hi All,

I am trying to do attaching policy request for cognito identity user using programmatically instead using aws cli. Through aws cli attaching successfully and also working fine. But programmatically not working. It gives exception like Cognito Identity not configured .
This is my sample code

  CognitoUserSession cognitoUserSession = AppHelper.getCurrSession();

                String idToken = cognitoUserSession.getIdToken().getJWTToken();

                Map<String, String> logins = new HashMap<String, String>();

                logins.put("cognito-idp.ap-south-1.amazonaws.com/xx-xxxx-x_xxxxxxxxx", idToken);

                credentialsProvider = new CognitoCachingCredentialsProvider(
                        getApplicationContext(),
                        "ap-south-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxdxxx", // Identity pool ID
                        Regions.AP_SOUTH_1 // Region
                );

                credentialsProvider.setLogins(logins);
                AttachPolicyRequest attachPolicyReq = new AttachPolicyRequest();
                attachPolicyReq.withPolicyName("MyIotPolicy");
                attachPolicyReq.withTarget(credentialsProvider.getIdentityId());

                AWSIotClient mIotAndroidClient = new AWSIotClient(AWSMobileClient.getInstance());
                mIotAndroidClient.setRegion(Region.getRegion(Regions.AP_SOUTH_1)); 
                mIotAndroidClient.attachPolicy(attachPolicyReq);

I am getting below exception

Exception : com.amazonaws.AmazonClientException: Cognito Identity not configured

By using below command is working fine.

aws iot attach-principal-policy --policy-name <iot-policy-name> --principal <cognito-identity-id>
Please guide me how to attach policy request through programmatically.

Thanks in Advance.

@kirantpatil
Copy link

Please check this code #600 (comment)


import com.amazonaws.services.iot.AWSIotClient;
import com.amazonaws.services.iot.model.AttachPolicyRequest;

AttachPolicyRequest attachPolicyReq = new AttachPolicyRequest();
attachPolicyReq.setPolicyName("myIOTPolicy"); // name of your IoT AWS policy
attachPolicyReq.setTarget(AWSMobileClient.getInstance().getIdentityId());
AWSIotClient mIotAndroidClient = new AWSIotClient(AWSMobileClient.getInstance());
mIotAndroidClient.setRegion(Region.getRegion("MyRegion")); // name of your aws region such as "us-east-1"
mIotAndroidClient.attachPolicy(attachPolicyReq);

@amarpulli1994
Copy link
Author

@kirantpatil I tried its not working

@kirantpatil
Copy link

Please try this #1552 (comment)

@amarpulli1994
Copy link
Author

@kirantpatil There is no attachpolicyrequest.

@rjuliano rjuliano added iot Issues with the AWS Android SDK for Internet of Things (IoT) mobile client Issues with AWS Mobile's client-side Cognito wrapper question General question labels Jun 3, 2020
@kirantpatil
Copy link

@rjuliano, Any updates on this issue ?

Why no one is answering to this question, we even went and purchased aws developer subscription to my surprise they are again asking us to go back to github.

What should we do, since we are struck here ?

Thank you.

@kirantpatil
Copy link

Dear @anuragdce,

Did you face this issue ?

Thanks.

@kirantpatil
Copy link

I think you need to add below code. Found at https://stackoverflow.com/questions/44244375/aws-iot-android-application-over-mqtt-throws-mqttexception-0-java-io-ioexcep

AmazonCognitoIdentity cognitoIdentity = new AmazonCognitoIdentityClient(credentialsProvider);
GetIdRequest getIdReq = new GetIdRequest();
getIdReq.setLogins(logins); //or if you have already set provider logins just use credentialsProvider.getLogins()
getIdReq.setIdentityPoolId(COGNITO_POOL_ID);
GetIdResult getIdRes = cognitoIdentity.getId(getIdReq);

@amarpulli1994
Copy link
Author

Hi all,
After spent lot of time..found mistake. In my case the attach policy request api by default it will take us-east-1. So here we have to set end point and as well as region also. Then only it will work.
Here is the sample code.

 AttachPrincipalPolicyRequest principalPolicyRequest = new AttachPrincipalPolicyRequest();
                principalPolicyRequest.setPolicyName("iotPolicy");
                principalPolicyRequest.setPrincipal(credentialsProvider.getIdentityId());

                AWSIotClient awsIotClient = new AWSIotClient(credentialsProvider);
                awsIotClient.setEndpoint(CUSTOMER_SPECIFIC_ENDPOINT);
                awsIotClient.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
                awsIotClient.attachPrincipalPolicy(principalPolicyRequest);

@xueyangp
Copy link

Hi all, After spent lot of time..found mistake. In my case the attach policy request api by default it will take us-east-1. So here we have to set end point and as well as region also. Then only it will work. Here is the sample code.

 AttachPrincipalPolicyRequest principalPolicyRequest = new AttachPrincipalPolicyRequest();
                principalPolicyRequest.setPolicyName("iotPolicy");
                principalPolicyRequest.setPrincipal(credentialsProvider.getIdentityId());

                AWSIotClient awsIotClient = new AWSIotClient(credentialsProvider);
                awsIotClient.setEndpoint(CUSTOMER_SPECIFIC_ENDPOINT);
                awsIotClient.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
                awsIotClient.attachPrincipalPolicy(principalPolicyRequest);

I have been trying to fix my issue for days. You are awesome. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iot Issues with the AWS Android SDK for Internet of Things (IoT) mobile client Issues with AWS Mobile's client-side Cognito wrapper question General question
Projects
None yet
Development

No branches or pull requests

4 participants