无效签名 - 提供的签名不符WooCommerce REST API在Android调用 [英] Invalid Signature - provided signature does not match WooCommerce REST API calling from Android

查看:844
本文介绍了无效签名 - 提供的签名不符WooCommerce REST API在Android调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Android开发者和新Woocommerce,并开始与Oauth1.0认证使用REST服务。我正在从邮递员正确的响应(RESTClient实现插件),并获得无效的签名的错误,而我从我的Andr​​oid应用程序调用。

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.

这是我的Andr​​oid code:

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});

        }
    }

我已经搜索在谷歌产生签名但都被说同样的code。我使用这个工具的http:// OAuth的。谷歌code.com / SVN / code / JavaScript的/例子/ signature.html 来验证签名,但无法验证,因为邮递员,这个工具和android生成的签名是彼此不同的。

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.

推荐答案

您必须发送序列中的所有参数。就像我们在PHP中的code

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

uksort( $params, 'strcmp' );

请参阅如何在Android上的参数进行排序。

See how you can sort the parameters in android.

这篇关于无效签名 - 提供的签名不符WooCommerce REST API在Android调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆