Invalid Signature - provided signature does not match WooCommerce REST API calling from Android
Asked Answered
C

2

4

I am an Android developer and new to Woocommerce and started consuming REST service with Oauth1.0 authentication. I am getting proper response from PostMan (RestClient plugin) and getting "Invalid Signature" error while I call from my android application.

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="
//               + 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});

        }
    }

I have searched on google for generating Signature but all were saying the same code. I use this tool http://oauth.googlecode.com/svn/code/javascript/example/signature.html to validate signature but could not validate because PostMan, this tool and android generated signature were different from each other.

Calen answered 5/10, 2015 at 12:45 Comment(0)
S
1

You must send all the parameters in the sequence. Like we have a code in php

uksort( $params, 'strcmp' );

See how you can sort the parameters in android.

Seaworthy answered 7/10, 2015 at 10:48 Comment(1)
can you tell me where I can find the exact order. I am trying to do it with ajax since I am developing a WinJS app but can't get past the invalid signature message?Safir
C
1

I also have the same issue after days of research finally i found the solution hope this will help some other i go through various documents

1)Using the WooCommerce REST API – Introduction

2)woocommerce-rest-api-docs

3)Scribe

4)scribe:1.3.5

After referring above documents and Source codes finally i created a library which do the OAuth 1.0a “one-legged” authentication for woocommerce HTTP android

The full description has added in the read me section of my library

Check The Library Here

WoocommerceAndroidOAuth1 LIBRARY

Choriocarcinoma answered 12/10, 2016 at 6:26 Comment(2)
you saved lot of my time. Thank youBedside
Hey ! I tried as explained in github but i'm getting error {"errors":[{"code":"woocommerce_api_authentication_error","message":"Invalid signature - provided signature does not match."}]} can you please help me with this.Homoeo

© 2022 - 2024 — McMap. All rights reserved.