无效签名 - 提供的签名与Android的WooCommerce REST API不匹配
我是Android开发人员,也是Woocommerce的新成员,并开始使用Oauth1.0身份验证使用REST服务。我从PostMan(RestClient插件)获得正确响应,并在我从我的android应用程序调用时出现“无效签名”错误。无效签名 - 提供的签名与Android的WooCommerce REST API不匹配
这里是我的Android代码:
OAuthParameters oauth;
public OAuthParameters authChecking() {
oauth = new OAuthParameters();
GenericUrl genericUrl = new GenericUrl("http://localhost/wordpress/wc-api/v3/products/count");
oauth.consumerKey = "ck_xxxxxxxxxxxxxxxxxxxxxxxxxxx";
oauth.signatureMethod = "HMAC-SHA1";
oauth.version = "3.0";
oauth.computeTimestamp();
oauth.computeNonce();
oauth.signer = new OAuthSigner() {
@Override
public String getSignatureMethod() {
return oauth.signatureMethod;
}
@Override
public String computeSignature(String signatureBaseString) throws GeneralSecurityException {
String key = "cs_xxxxxxxxxxxxxxxxxxxxxxxxxx";
Mac mac = Mac.getInstance(
"HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), "HmacSHA1");
mac.init(secret);
byte[] digest = mac.doFinal(signatureBaseString.getBytes());
Log.e("SIGNATURE Base64", new String(Base64.encode(digest, 0)).trim());
String signature = new String(com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64.encodeBase64String(digest));
return signature;
}
};
try {
oauth.computeSignature("GET", genericUrl);
} catch (GeneralSecurityException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
methodSignatureTest();
return oauth;
}
@Override
public void requestAPI(Object... param) {
OAuthParameters oauth = authChecking();
if (oauth != null) {
String url = null;
try {
Toast.makeText(MainActivity.this, "Signature retrive called", Toast.LENGTH_SHORT).show();
url = "http://localhost/wordpress/wc-api/v3/products/"+"count?oauth_consumer_key=" + oauth.consumerKey + "&oauth_signature_method=" + oauth.signatureMethod + "&oauth_timestamp=" + oauth.timestamp + "&oauth_nonce=" + oauth.nonce + "&oauth_version=" + oauth.version + "&oauth_signature="
// + java.net.URLDecoder.decode(oauth.signature, "UTF-8");
+ URLEncoder.encode(oauth.signature, "UTF-8");
// +oauth.signature;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
url = null;
}
Log.v("URL ", url);
Log.v("SINGNATURE ", oauth.signature);
getDataFromWeb_Get.getData(this, this, new String[]{"http://localhost/wordpress/wc-api/v3/products/", url});
}
}
我已经搜索在谷歌生成签名,但所有在说相同的代码。我使用这个工具http://oauth.googlecode.com/svn/code/javascript/example/signature.html来验证签名,但无法验证,因为PostMan,这个工具和android生成的签名是彼此不同的。
您必须发送序列中的所有参数。就像我们在php中有代码
uksort($params, 'strcmp');
看看如何在android中对参数进行排序。
我也有同样的问题后,研究天终于让我找到了解决办法希望这将有助于其他一些 我经历的各种文件
1)Using the WooCommerce REST API – Introduction
3 )Scribe
上述文件和源码指后我终于创建了做OAuth的1.0A的woocommerce HTTP的Android
的完整描述已经在我的图书馆
的自述部分增加了“一条腿”认证库检查库这里
你节省了我很多时间。谢谢 –
你能告诉我在哪儿能找到确切的秩序。我正在尝试使用ajax来完成它,因为我正在开发一个WinJS应用程序,但无法通过无效签名消息? –