这是我的code以获得在Android的雅虎接触。 即时通讯能够获得高达访问令牌,但同时对接触使GET请求即时得到签名无效的错误。
我搜索了很多,但我不能找到什么即时做错了。所以任何人都帮我个忙。
公共类YahooContacts扩展BaseActivity {
私人最终字符串变量=yahoo_auth;
私有静态最后弦乐CONSUMER_KEY =My_consumer_key;
私有静态最后弦乐CONSUMER_SECRET =my_consumer_secret;
私有静态最后弦乐CALLBACK_SCHEME =HTTP;
私有静态最后弦乐CALLBACK_HOST =www.something_unique_here.com;
私有静态最后弦乐CALLBACK_URL = CALLBACK_SCHEME +://
+ CALLBACK_HOST;
私人字符串的auth_token = NULL;
私人字符串AUTH_TOKEN_SECRET = NULL;
私人字符串AUTH_URL = NULL;
私人字符串USER_TOKEN = NULL;
私人字符串ACCESS_TOKEN = NULL;
私人字符串mUSER_GUID = NULL;
私人的WebView mWebview;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.yahoo_layout);
mWebview =(web视图)findViewById(R.id.webview);
新getContactsTask()执行()。
}
类getContactsTask扩展的AsyncTask<虚空,虚空,虚空> {
@覆盖
在preExecute保护无效(){
super.on preExecute();
}
@覆盖
保护无效doInBackground(虚空...... PARAMS){
getAuthorizationToken();
getUserAutherization();
返回null;
}
@覆盖
保护无效onPostExecute(无效的结果){
super.onPostExecute(结果);
}
}
私人无效getAuthorizationToken(){
字符串requestPath =https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_consumer_key=
+ CONSUMER_KEY
+与& oauth_nonce =
+ System.currentTimeMillis的()
+×
+&放大器; oauth_signature_method = PLAINTEXT
+与& oauth_signature =
+ CONSUMER_SECRET
+%26
+与& oauth_timestamp =
+ System.currentTimeMillis的()
+&放大器; oauth_version = 1.0
+&放大器; xoauth_lang_ preF = EN-US
+与& oauth_callback =+ CALLBACK_URL;
HttpClient的HttpClient的=新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(requestPath);
尝试 {
ResponseHandler的<字符串> ResponseHandler的=新BasicResponseHandler();
字符串responseBody = httpclient.execute(HTTPGET,ResponseHandler所);
的String []数据= responseBody.split(&安培;);
的auth_token =数据[0] .replace(oauth_token =,);
AUTH_TOKEN_SECRET =数据[1] .replace(oauth_token_secret =,);
AUTH_URL =数据[3] .replace(xoauth_request_auth_url =,);
VIPLogger.info(TAG的authToken+的auth_token);
VIPLogger.info(TAG的authToken秘密+ AUTH_TOKEN_SECRET);
}赶上(例外五){
e.printStackTrace();
}
}
私人无效getUserAutherization(){
mWebview.getSettings()setJavaScriptEnabled(真)。
mWebview.setWebViewClient(lWebviewClient);
mWebview.loadUrl(https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=
+的auth_token);
}
私人无效getAccessToken(){
字符串requestPath =https://api.login.yahoo.com/oauth/v2/get_token?oauth_consumer_key=
+ CONSUMER_KEY
+与& oauth_nonce =
+ System.currentTimeMillis的()
+×
+&放大器; oauth_signature_method = PLAINTEXT
+与& oauth_signature =
+ CONSUMER_SECRET
+%26
+ AUTH_TOKEN_SECRET
+与& oauth_timestamp =
+ System.currentTimeMillis的()
+&放大器; oauth_version = 1.0
+与& oauth_token =
+的auth_token
+与& oauth_verifier =
+ USER_TOKEN;
HttpClient的HttpClient的=新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(requestPath);
尝试 {
ResponseHandler的<字符串> ResponseHandler的=新BasicResponseHandler();
字符串responseBody = httpclient.execute(HTTPGET,ResponseHandler所);
的String []数据= responseBody.split(&安培;);
ACCESS_TOKEN =数据[0] .replace(oauth_token =,);
mUSER_GUID =数据[5] .replace(xoauth_yahoo_guid =,);
VIPLogger.info(TAG,用户GUID:+ mUSER_GUID);
VIPLogger.info(TAG,访问令牌+ ACCESS_TOKEN);
getAllContacts();
}赶上(例外五){
e.printStackTrace();
VIPLogger.error(TAG,
)而获取用户的GUID和访问令牌错误;
}
}
WebViewClient lWebviewClient =新WebViewClient(){
公共无效onPageStarted(web视图查看,字符串URL,
android.graphics.Bitmap图标){
如果(url.contains(vipitservice)){
mWebview.stopLoading();
INT lastIndex的= url.lastIndexOf(=)+ 1;
VIPLogger.info(TAG,url.substring(lastIndex的,url.length()));
USER_TOKEN = url.substring(lastIndex的,url.length());
mWebview.setVisibility(View.GONE);
getAccessToken();
}
};
};
私人无效getAllContacts(){
HttpClient的HttpClient的=新DefaultHttpClient();
字符串host_url =http://social.yahooapis.com/v1/user/+ mUSER_GUID +/接触;
串随机数=+ System.currentTimeMillis的();
字符串的timeStamp =+(System.currentTimeMillis的()/ 1000L);
尝试{
字符串PARAMS =?境界= yahooapis.com
+与& oauth_consumer_key =+ CONSUMER_KEY
+与& oauth_nonce =+乱数
+&放大器; oauth_signature_method =+HMAC-SHA1
+与& oauth_timestamp =+的timeStamp
+&放大器; oauth_token =+ URLDe coder.de code(ACCESS_TOKEN,UTF-8)
+&放大器; oauth_version = 1.0
;
字符串basestring的=GET和放大器;+ EN code(host_url)+ EN code(PARAMS);
字符串signingKey = EN code(CONSUMER_SECRET)+&放大器;+ EN code(AUTH_TOKEN_SECRET);
VIPLogger.info(TAG,基地字符串:+ basestring的);
字符串lSignature = computeHmac(basestring的,signingKey);
VIPLogger.info(TAG的签名:+ lSignature);
lSignature = EN code(lSignature);
VIPLogger.info(TAG,签名ENA codeD:+ lSignature);
字符串lRequestUrl = host_url
+?境界= yahooapis.com
+与& oauth_consumer_key =+ CONSUMER_KEY
+与& oauth_nonce =+乱数
+&放大器; oauth_signature_method = HMAC-SHA1
+与& oauth_timestamp =+的timeStamp
+与& oauth_token =+ ACCESS_TOKEN
+&放大器; oauth_version = 1.0
+与& oauth_signature =+ lSignature
;
VIPLogger.info(TAG,lRequestUrl.substring(1202));
HTTPGET HTTPGET =新HTTPGET(lRequestUrl);
ResponseHandler的<字符串> ResponseHandler的=新BasicResponseHandler();
字符串responseBody = httpclient.execute(HTTPGET,ResponseHandler所);
VIPLogger.info(TAG,人脉回应:+ responseBody);
}赶上(例外五){
e.printStackTrace();
VIPLogger.error(TAG,错误而获取用户的联系人);
}
}
公共字符串computeHmac(字符串basestring的,字符串键){
尝试 {
苹果MAC = Mac.getInstance(HMACSHA1);
SecretKeySpec signingKey =新SecretKeySpec(key.getBytes(UTF-8),
HMAC-SHA1);
mac.init(signingKey);
byte []的消化= mac.doFinal(baseString.getBytes());
字符串结果= Base64.en codeToString(消化,Base64.URL_SAFE);
返回结果;
}赶上(例外五){
e.printStackTrace();
VIPLogger.error(TAG,错误而产生SHA);
}
返回null;
}
公共字符串连接codeURIComponent(最终字符串值){
如果(价值== NULL){
返回 ;
}
尝试 {
返回URLEn coder.en code(值,UTF-8)
// OAuth的EN codeS一些字符是不同的:
.replace(+,20%)。代替(*,%2A)
.replace(%7E,〜);
//这样可以更快地完成更多的手工制作的code。
}赶上(例外五){
e.printStackTrace();
}
返回 ;
}
公共字符串连接code(字符串输入){
StringBuilder的resultStr =新的StringBuilder();
对于(CHAR CH:input.toCharArray()){
如果(isUnsafe(CH)){
resultStr.append(%);
resultStr.append(toHex(CH / 16));
resultStr.append(toHex(CH%16));
} 其他 {
resultStr.append(CH);
}
}
返回resultStr.toString()修剪()。
}
专用字符toHex(INT CH){
返程(炭)(CH小于10'0'+ CH:'A'+ CH - 10);
}
私人布尔isUnsafe(焦CH){
如果(CH> 128 || CH℃下)
返回true;
返回%$&安培+,/:; = @&其中;>#%的indexOf(章)> = 0;
}
}
解决方案
我固定与此code这个问题。关键是ü需要连接code所有钥匙和价值,然后连接$ C C的PARMS $了。 。请记住,access_token当属$ P $笔codeD。
私人无效getAllContacts(){
HttpClient的HttpClient的=新DefaultHttpClient();
字符串host_url =http://social.yahooapis.com/v1/user/+ mUSER_GUID +/接触;
串随机数=+ System.currentTimeMillis的();
字符串的timeStamp =+(System.currentTimeMillis的()/ 1000L);
尝试{
字符串PARAMS =
+ EN code(oauth_consumer_key)+=+ EN code(CONSUMER_KEY)
+&放大器;+ EN code(oauth_nonce)+=+ EN code(随机数)
+&放大器;+ EN code(oauth_signature_method)+=+ EN code(HMAC-SHA1)
+&放大器;+ EN code(oauth_timestamp)+=+ EN code(时间戳)
+&放大器;+ EN code(oauth_token)+=+ ACCESS_TOKEN
+&放大器;+ EN code(oauth_version)+=+ EN code(1.0)
;
字符串basestring的= EN code(GET)+&放大器;+ EN code(host_url)+&放大器;+ EN code(PARAMS);
字符串signingKey = EN code(CONSUMER_SECRET)+&放大器;+ EN code(ACCESS_TOKEN_SECRET);
VIPLogger.info(TAG,基地字符串:+ basestring的);
字符串lSignature = computeHmac(basestring的,signingKey);
VIPLogger.info(TAG的签名:+ lSignature);
lSignature = EN code(lSignature);
VIPLogger.info(TAG,签名ENA codeD:+ lSignature);
字符串lRequestUrl = host_url
+?oauth_consumer_key =+ CONSUMER_KEY
+与& oauth_nonce =+乱数
+&放大器; oauth_signature_method = HMAC-SHA1
+与& oauth_timestamp =+的timeStamp
+与& oauth_token =+ ACCESS_TOKEN
+&放大器; oauth_version = 1.0
+与& oauth_signature =+ lSignature
;
//VIPLogger.info(TAG,lRequestUrl.substring(1202));
HTTPGET HTTPGET =新HTTPGET(lRequestUrl);
ResponseHandler的<字符串> ResponseHandler的=新BasicResponseHandler();
字符串responseBody = httpclient.execute(HTTPGET,ResponseHandler所);
VIPLogger.info(TAG,人脉回应:+ responseBody);
}赶上(例外五){
e.printStackTrace();
VIPLogger.error(TAG,错误而获取用户的联系人);
}
}
this is my code to get yahoo contacts in android . i m able to get upto access token but while making GET request for contacts im getting signature invalid error.
i searched a lot but i couldnt found what im doing wrong. so anyone give me a hand.
public class YahooContacts extends BaseActivity {
private final String TAG = "yahoo_auth";
private static final String CONSUMER_KEY = "My_consumer_key";
private static final String CONSUMER_SECRET = "my_consumer_secret";
private static final String CALLBACK_SCHEME = "http";
private static final String CALLBACK_HOST = "www.something_unique_here.com";
private static final String CALLBACK_URL = CALLBACK_SCHEME + "://"
+ CALLBACK_HOST;
private String AUTH_TOKEN = null;
private String AUTH_TOKEN_SECRET = null;
private String AUTH_URL = null;
private String USER_TOKEN = null;
private String ACCESS_TOKEN = null;
private String mUSER_GUID = null;
private WebView mWebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yahoo_layout);
mWebview = (WebView) findViewById(R.id.webview);
new getContactsTask().execute();
}
class getContactsTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
getAuthorizationToken();
getUserAutherization();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
private void getAuthorizationToken() {
String requestPath = "https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_consumer_key="
+ CONSUMER_KEY
+ "&oauth_nonce="
+ System.currentTimeMillis()
+ "x"
+ "&oauth_signature_method=PLAINTEXT"
+ "&oauth_signature="
+ CONSUMER_SECRET
+ "%26"
+ "&oauth_timestamp="
+ System.currentTimeMillis()
+ "&oauth_version=1.0"
+ "&xoauth_lang_pref=en-us"
+ "&oauth_callback=" + CALLBACK_URL;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(requestPath);
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
String[] data = responseBody.split("&");
AUTH_TOKEN = data[0].replace("oauth_token=", "");
AUTH_TOKEN_SECRET = data[1].replace("oauth_token_secret=", "");
AUTH_URL = data[3].replace("xoauth_request_auth_url=", "");
VIPLogger.info(TAG, "authToken" + AUTH_TOKEN);
VIPLogger.info(TAG, "authToken secret" + AUTH_TOKEN_SECRET);
} catch (Exception e) {
e.printStackTrace();
}
}
private void getUserAutherization() {
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.setWebViewClient(lWebviewClient);
mWebview.loadUrl("https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token="
+ AUTH_TOKEN);
}
private void getAccessToken() {
String requestPath = "https://api.login.yahoo.com/oauth/v2/get_token?oauth_consumer_key="
+ CONSUMER_KEY
+ "&oauth_nonce="
+ System.currentTimeMillis()
+ "x"
+ "&oauth_signature_method=PLAINTEXT"
+ "&oauth_signature="
+ CONSUMER_SECRET
+ "%26"
+ AUTH_TOKEN_SECRET
+ "&oauth_timestamp="
+ System.currentTimeMillis()
+ "&oauth_version=1.0"
+ "&oauth_token="
+ AUTH_TOKEN
+ "&oauth_verifier="
+ USER_TOKEN;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(requestPath);
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
String[] data = responseBody.split("&");
ACCESS_TOKEN = data[0].replace("oauth_token=", "");
mUSER_GUID = data[5].replace("xoauth_yahoo_guid=", "");
VIPLogger.info(TAG, "user guid: " + mUSER_GUID);
VIPLogger.info(TAG, "Access token: " + ACCESS_TOKEN);
getAllContacts();
} catch (Exception e) {
e.printStackTrace();
VIPLogger.error(TAG,
"error while fetching user guid and access token");
}
}
WebViewClient lWebviewClient = new WebViewClient() {
public void onPageStarted(WebView view, String url,
android.graphics.Bitmap favicon) {
if (url.contains("vipitservice")) {
mWebview.stopLoading();
int lastIndex = url.lastIndexOf("=") + 1;
VIPLogger.info(TAG, url.substring(lastIndex, url.length()));
USER_TOKEN = url.substring(lastIndex, url.length());
mWebview.setVisibility(View.GONE);
getAccessToken();
}
};
};
private void getAllContacts() {
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 = "?realm=yahooapis.com"
+ "&oauth_consumer_key=" + CONSUMER_KEY
+ "&oauth_nonce="+nonce
+ "&oauth_signature_method="+"HMAC-SHA1"
+ "&oauth_timestamp="+timeStamp
+ "&oauth_token="+URLDecoder.decode(ACCESS_TOKEN,"utf-8")
+ "&oauth_version=1.0"
;
String baseString = "GET&"+encode(host_url)+encode(params);
String signingKey = encode(CONSUMER_SECRET)+"&"+encode(AUTH_TOKEN_SECRET);
VIPLogger.info(TAG, "base string: " + baseString);
String lSignature = computeHmac(baseString, signingKey);
VIPLogger.info(TAG, "signature: " + lSignature);
lSignature = encode(lSignature);
VIPLogger.info(TAG, "signature enacoded: " + lSignature);
String lRequestUrl = host_url
+ "?realm=yahooapis.com"
+ "&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
;
VIPLogger.info(TAG, lRequestUrl.substring(1202));
HttpGet httpget = new HttpGet(lRequestUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
VIPLogger.info(TAG, "contacts response: " + responseBody);
}catch(Exception e){
e.printStackTrace();
VIPLogger.error(TAG, "error while fetching user contacts");
}
}
public String computeHmac(String baseString, String key) {
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"),
"HMAC-SHA1");
mac.init(signingKey);
byte[] digest = mac.doFinal(baseString.getBytes());
String result = Base64.encodeToString(digest, Base64.URL_SAFE);
return result;
} catch (Exception e) {
e.printStackTrace();
VIPLogger.error(TAG, "error while generating sha");
}
return null;
}
public String encodeURIComponent(final String value) {
if (value == null) {
return "";
}
try {
return URLEncoder.encode(value, "utf-8")
// OAuth encodes some characters differently:
.replace("+", "%20").replace("*", "%2A")
.replace("%7E", "~");
// This could be done faster with more hand-crafted code.
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String encode(String input) {
StringBuilder resultStr = new StringBuilder();
for (char ch : input.toCharArray()) {
if (isUnsafe(ch)) {
resultStr.append('%');
resultStr.append(toHex(ch / 16));
resultStr.append(toHex(ch % 16));
} else {
resultStr.append(ch);
}
}
return resultStr.toString().trim();
}
private char toHex(int ch) {
return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
}
private boolean isUnsafe(char ch) {
if (ch > 128 || ch < 0)
return true;
return " %$&+,/:;=?@<>#%".indexOf(ch) >= 0;
}
}
解决方案
i fixed this issue with this code. the trick is u need to encode all 'key' and 'values', then encode the parms again. . Remember that access_token comes as preencoded.
private void getAllContacts() {
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);
VIPLogger.info(TAG, "base string: " + baseString);
String lSignature = computeHmac(baseString, signingKey);
VIPLogger.info(TAG, "signature: " + lSignature);
lSignature = encode(lSignature);
VIPLogger.info(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
;
//VIPLogger.info(TAG, lRequestUrl.substring(1202));
HttpGet httpget = new HttpGet(lRequestUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
VIPLogger.info(TAG, "contacts response: " + responseBody);
}catch(Exception e){
e.printStackTrace();
VIPLogger.error(TAG, "error while fetching user contacts");
}
}
上一篇:VSTO:应用重点重点、VSTO