AWS iam create new user returning html instead of xml response android

人盡茶涼 提交于 2019-12-12 04:46:48

问题


I am creating a new user through android app programatically using amazon web-service. here is my code(asynctask):

private class S3RegisterNewUser extends AsyncTask<String, Void, String> {

            ProgressDialog dialog;
            String email;
             public S3RegisterNewUser(String email) {
                // TODO Auto-generated constructor stub
                this.email=email;
            }


            protected void onPreExecute() {
                dialog = new ProgressDialog(S3UploaderActivity.this);
                dialog.setMessage(S3UploaderActivity.this
                        .getString(R.string.plswait));
                dialog.setCancelable(false);
                dialog.show();
            }


            protected String doInBackground(String ...strings) {
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(Constants.REGISTER); //url[0]

                    String result = "";
                    boolean result_recvd = false;
                    boolean ifallset = false;

//                  sparser = new ParsingClass();

                    try {
                      // Add your data
                      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                      nameValuePairs.add(new BasicNameValuePair("UserName",email));
                      nameValuePairs.add(new BasicNameValuePair("Path","/"));
                      nameValuePairs.add(new BasicNameValuePair("Version","2010-05-08"));


                      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


                   // Execute HTTP Post Request
                      /*HttpResponse response = httpclient.execute(httppost);
                      HttpEntity entity = response.getEntity();
                      is = entity.getContent();
                      bindDataToListing(is);*/

                      HttpResponse resp = httpclient.execute(httppost);
                      HttpEntity ent = resp.getEntity();
                      ist = ent.getContent();
                      result = getStringFromInputStream(ist);



                      if(!result.contains("result")) {
                          Toast.makeText(S3UploaderActivity.this, "No connection or data not received from server", Toast.LENGTH_SHORT).show();

                          //result = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml><result>0</result><message>"+Constants.noConnection+"</message></xml>";
                          result_recvd = false;
                      }else{
                          /** Result has been received from server which contains atleast the 'result' TAG 
                           *  This helps us to know if data is being fetched from server or not.*/
                          result_recvd = true;
                      }

                     // ifallset = bindDataToListing(result, result_recvd);

                      if(!result.contains("result")){
                          Toast.makeText(S3UploaderActivity.this, "No connection or data not received from server", Toast.LENGTH_SHORT).show();
                      }

                      //result = convertStreamtoString(is);

                      Log.e("-- parseResultXML: ", ""+result);

                    } catch (ClientProtocolException e) {
                      e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }



                }  catch (AmazonClientException ace) {
                    System.out.println("Caught an AmazonClientException, " +
                            "which means the client encountered " +
                            "an internal error while trying to communicate" +
                            " with S3, " +
                            "such as not being able to access the network.");
                    System.out.println("Error Message: " + ace.getMessage());
                }
                return null;
            }

            protected void onPostExecute(String file_url) {

                dialog.dismiss();


            }


        }

          /** Convert input stream to a String 
         * @param is -Supply the input stream variable here */
        private String getStringFromInputStream(InputStream is) {
            StringBuilder sb = new StringBuilder();
            String line;
            try {
                br = new BufferedReader(new InputStreamReader(is));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null ) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return sb.toString();
        }   

Constants.REGISTER="https://iam.amazonaws.com/"

I am following the Iam documentation.But instead of recieving xml result according to the documentation I am recieving HTML responses like below:

06-20 15:05:55.856: E/-- parseResultXML:(665): <!DOCTYPE html><!--[if lt IE 7]><html class="no-js lt-ie10 lt-ie9 lt-ie8 lt-ie7 lang-en lang-en_US" lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml"><![endif]--><!--[if IE 7]><html class="no-js lt-ie10 lt-ie9 lt-ie8 lang-en lang-en_US" lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml"><![endif]--><!--[if IE 8]><html class="no-js lt-ie10 lt-ie9 lang-en lang-en_US" lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml"><![endif]--><!--[if IE 9]><html class="no-js lt-ie10 lang-en lang-en_US" lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml"><![endif]--><!--[if gt IE 9]><!--><html class="no-js lang-en lang-en_US" lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml"> <!--<![endif]--> <head>   <meta http-equiv="content-type" content="text/html; charset=UTF-8" />   <link rel="dns-prefetch" href="//a1.awsstatic.com" />   <link rel="dns-prefetch" href="//a0.awsstatic.com" />   <link rel="dns-prefetch" href="//d0.awsstatic.com" />   <meta name="viewport" content="width=device-width, initial-scale=1.0" />   <title>AWS Identity and Access Management (IAM) in the Cloud</title>   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />   <link rel="icon" type="image/ico" href="//a1.awsstatic.com/images/site/favicon.ico" />   <link rel="shortcut icon" type="image/ico" href="//a1.awsstatic.com/images/site/favicon.ico" />   <link rel="apple-touch-icon" sizes="57x57" href="//a1.awsstatic.com/images/site/touch-icon-iphone-114-precomposed.png" />   <link rel="apple-touch-icon" sizes="72x72" href="//a1.awsstatic.com/images/site/touch-icon-ipad-144-precomposed.png" />   <link rel="apple-touch-icon" sizes="114x114" href="//a1.awsstatic.com/images/site/touch-icon-iphone-114-precomposed.png" />   <link rel="apple-touch-icon" sizes="144x144" href="//a1.awsstatic.com/images/site/touch-icon-ipad-144-precomposed.png" />   <meta property="og:title" content="AWS Identity and Access Management (IAM) in the Cloud" />   <meta property="og:type" content="company" />   <meta property="og:url" content="//aws.amazon.com/iam/" />   <meta property="og:image" content="//a1.awsstatic.com/images/open-graph/opengraph.gif" />   <meta property="og:site_name" content="Amazon Web Services, Inc." />   <meta name="google-site-verification" content="XHghG81ulgiW-3EylGcF48sG28tBW5EH0bNUhgo_DrU" />   <meta name="msvalidate.01" content="6F92E52A288E266E30C2797ECB5FCCF3" />   <link rel="canonical" href="http://aws.amazon.com/iam/" />   <link rel="alternate" href="//aws.amazon.com/de/iam/" hreflang="de-de" />   <link rel="alternate" href="//aws.amazon.com/es/iam/" hreflang="es-es" />   <link rel="alternate" href="//aws.amazon.com/fr/iam/" hreflang="fr-fr" />   <link rel="alternate" href="//aws.amazon.com/jp/iam/" hreflang="ja-jp" />   <link rel="alternate" href="//aws.amazon.com/pt/iam/" hreflang="pt-br" />   <link rel="alternate" href="//aws.amazon.com/ko/iam/" hreflang="ko-kr" />   <link rel="alternate" href="//aws.amazon.com/cn/iam/" hreflang="zh-cn" />   <link rel="stylesheet" href="//a1.awsstatic.com/css/35/style.css" />   <!--[if lt IE 9]>    <script src="//a1.awsstatic.com/js/35/jquery.1.9.js"></script>  <![endif]-->   <!--[if (gte IE 9) | (!IE)]><!-->   <script src="//a1.awsstatic.com/js/35/jquery.2.0.js"></script>   <!--<![endif]-->   <script src="//a1.awsstatic.com/js/35/aws-target-mediator.js"></script>   <script>AWS.TargetMediator.init();</script>   <script src="//a1.awsstatic.com/js/35/modernizr.js"></script>   <script>    var require = {      baseUrl: "//a1.awsstatic.com/js/35/",      paths: {        "jquery": "jquery-amd"      },      deps: ["scripts"],      shim: {        "scripts": ["jquery"],        "forms": ["jquery"],        "pricing-table": ["jquery"

If I am doing something wrong or sending some wrong parameters then the code must throw an exception or stop.But why it is returning this HTML file??What wrong I am doing here??


回答1:


Update

The AWS SDK for Android seems to be a bit lacking on the getting started front indeed - it seems to be designed fairly similar to the AWS SDK for Java though (naturally):

There, every AWS service has its own service specific client, which would be AmazonIdentityManagementClient for your use case. Given it is absent, it seems the AWS SDK for Android doesn't support IAM directly, so you'd need to resort to the IAM Query API indeed (as mentioned on that page), i.e. you'll need to implement the Signing [of] AWS API Requests yourself, unfortunately.


Initial Answer

AWS services in general and Amazon IAM in particular require the Signing [of] AWS API Requests, i.e. you cannot simply call an API endpoint, rather you need your AWS Security Credentials to generate a Digital Signature for each request before submitting it to the API.

The request signing process is a bit complex though and also varies between AWS services, so I'd highly recommend to simply use the AWS SDK for Android instead, which does all the heavy lifting for you - this also applies for any other complexities in dealing with AWS btw., so even if you can conceptually, I'd actually never use AWS without a SDK if there isn't any specific reason to do so (e.g. being on a platform not supported by one of the many offical AWS SDKs yet).



来源:https://stackoverflow.com/questions/24324869/aws-iam-create-new-user-returning-html-instead-of-xml-response-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!