我是android新手。我正在实现用于检索Yahoo联系人的应用
在我的应用程序中。但是当我运行该应用程序时,成功登录
并点击同意分享信息,但我面临此类
错误-
Authentication error: Unable to respond to any of these challenges:
{oauth=WWW-Authenticate: Oath oath_problem="signature_invalid", realm="yahooapis.com"}
和
error while fetching user contact
谁能帮我?这是我的代码:
private void getAllContacts() throws UnsupportedEncodingException,ClientProtocolException, IOException
{
HttpClient httpclient = new DefaultHttpClient();
String host_url = "http://social.yahooapis.com/v1/user/" + mUSER_GUID+ "/contacts";
String nonce = ""+System.currentTimeMillis();
String timeStamp = ""+(System.currentTimeMillis()/1000L);
try{
String params =
""+encode("oauth_consumer_key")+"=" + encode(CONSUMER_KEY)
+ "&"+encode("oauth_nonce")+"="+encode(nonce)
+ "&"+encode("oauth_signature_method")+"="+encode("HMAC-SHA1")
+ "&"+encode("oauth_timestamp")+"="+encode(timeStamp)
+ "&"+encode("oauth_token")+"="+ACCESS_TOKEN
+ "&"+encode("oauth_version")+"="+encode("1.0")
;
String baseString = encode("GET")+"&"+encode(host_url)+"&"+encode(params);
String signingKey = encode(CONSUMER_SECRET)+"&"+encode(ACCESS_TOKEN_SECRET);
Log.e(TAG, "base string: " + baseString);
String lSignature = computeHmac(baseString, signingKey);
Log.e(TAG, "signature: " + lSignature);
lSignature = encode(lSignature);
Log.e(TAG, "signature enacoded: " + lSignature);
String lRequestUrl = host_url
+ "?oauth_consumer_key="+CONSUMER_KEY
+ "&oauth_nonce="+nonce
+ "&oauth_signature_method=HMAC-SHA1"
+ "&oauth_timestamp="+timeStamp
+ "&oauth_token="+ACCESS_TOKEN
+ "&oauth_version=1.0"
+ "&oauth_signature="+lSignature
;
Log.e(TAG, lRequestUrl);
HttpGet httpget = new HttpGet(lRequestUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseBody = httpclient.execute(httpget, responseHandler);
Log.e(TAG, "contacts response: " + responseBody);
String strUrl=responseBody;
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpPost httppost=new HttpPost(strUrl);
HttpResponse httpresponse=httpClient.execute(httppost);
HttpEntity httpEntity=httpresponse.getEntity();
String strXml=EntityUtils.toString(httpEntity);
ArrayList<HashMap<String , String>> arrayList=new ArrayList<HashMap<String,String>>();
XMLParser xmlparser=new XMLParser();
String strxml = xmlparser.getXmlFromUrl(responseBody);
Log.e("",""+strxml); // getting xml url here
Document doc = xmlparser.getDomElement(strxml); // getting data object model
NodeList nl = doc.getElementsByTagName(KEY_FIELD); // Looping through all item nodes <field>
for(int i=0 ; i<nl.getLength() ; i++ )
{
//creating new hash map
HashMap<String ,String > map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
//Adding each child node to HashMap Key Value pair
map.put(KEY_ID , xmlparser.getValue(e, KEY_ID));
map.put(KEY_TYPE , xmlparser.getValue(e, KEY_TYPE));
map.put(KAE_VALUE , xmlparser.getValue(e, KAE_VALUE));
Log.e("Hash Map","Key Value"+map);
//Adding hashlist to arrayList
arrayList.add(map);
Log.e("ArrayList ","of Contact "+arrayList);
}
}
catch(Exception e)
{
e.printStackTrace();
Log.e(TAG, "error while fetching user contacts");
}
}
提前致谢。
最佳答案
请参见K-9 mail。 K-9是开源的。