我正在尝试使用 Oauth 进行验证。我的示例代码是:
ArrayList <NameValuePair> params = new ArrayList<NameValuePair>();
boolean flag=false;
URL url;
String oauth_nonce="q14F5ApN4Ka5RJGbYgabXwMSZ4BxVrlNGedN9zoFAoi3rc7dpLjIgri1erAAriPu";
String oauth_timestamp="1336069726";
String signatureString =
URLEncoder.encode("device_id=id_here")
+URLEncoder.encode("&device_type=psw_here")
+URLEncoder.encode("&oauth_consumer_key=consumer_key_here...&oauth_nonce=" +
oauth_nonce+
"&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1336069726&oauth_version=1.0");
String percent_encoded_url = URLEncoder.encode("http://www.example.com");
String signatureBaseString = "POST"+
"&"+percent_encoded_url+
"&"+signatureString;
String oauth_signature=URLEncoder.encode(computeHmac(signatureBaseString, "aWAQTWR7VvHapaNhWBCL0VaUdGN2xzN4&"));
String headerValue ="OAuth "+
"oauth_consumer_key=\""+"consumer_key_here..."+"\","+
"oauth_signature_method=\"HMAC-SHA1"+"\","+
"oauth_signature=\""+oauth_signature+"\","+
"oauth_timestamp=\"1336069726"+"\","+
"oauth_nonce=\""+oauth_nonce+"\"," +
"oauth_version=\"1.0"+"\"";
HttpPost httppost = new HttpPost("http://www.example.com/");
httppost.setHeader("Content-Type","application/x-www-form-urlencoded");
httppost.setHeader("Authorization",headerValue);
HttpEntity httpentity = new UrlEncodedFormEntity(params);
HttpClient httpclient = new DefaultHttpClient(clientConnectionManager, httpparams);
StringBuilder sb = new StringBuilder();
try {
HttpResponse response = httpclient.execute(httppost);
当我发送请求时,它总是说“签名匹配错误”。我对Oauth一无所知。我不知道我错在哪里。有人可以指导我正确的方向吗?