我正在通过android应用程序编程地使用amazon web-service创建一个新用户。下面是我的代码(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/"
我遵循我的文档。但是,不是根据文档接收xml结果,而是接收HTML响应,如下所示:
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"
如果我做错了什么或发送了一些错误的参数,那么代码必须抛出异常或停止。但是为什么它返回这个HTML文件??我在这里做错了什么?
更新
Android的AWS SDK似乎在入门方面确实有点欠缺——它的设计似乎与Java的AWS SDK相当相似(很自然):
在那里,每个AWS服务都有自己的特定于服务的客户端,对于您的用例,这将是AmazonIdentityManagementClient。鉴于它不存在,似乎Android的AWS SDK不直接支持IAM,所以你需要诉诸于IAM查询API确实(如该页所述),即你需要实现签名[的]AWS API请求自己,不幸的是。
初始回答
一般的AWS服务,特别是Amazon IAM,需要签名AWS API请求,也就是说,您不能简单地调用API端点,而是需要您的AWS安全凭证为每个请求生成数字签名,然后将其提交给API。
请求签名过程有点复杂,而且在不同的AWS服务之间也有所不同,所以我强烈建议你直接使用Android的AWS SDK,它为你做了所有繁重的工作——顺便说一下,这也适用于处理AWS的任何其他复杂性。,所以即使你在概念上可以,如果没有任何特定的原因,我实际上永远不会使用没有SDK的AWS(例如,在一个不受众多官方AWS SDK支持的平台上)。