I am an Android developer and new to Woocommerce and started using the REST service with Oauth1.0 authentication. I get the correct answer from PostMan (RestClient plugin) and get the message "Invalid Signature" when I call from an Android app.
Here is my Android code:
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="
I searched on Google to create a Signature, but everyone said the same code. I use this http://oauth.googlecode.com/svn/code/javascript/example/signature.html tool to verify the signature, but I canβt verify it because PostMan, this tool and the android signature were different from each other.
android rest wordpress woocommerce
Ramesh kumar
source share