2015-10-05 82 views
3

我是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生成的簽名是彼此不同的。

回答

1

您必須發送序列中的所有參數。就像我們在php中有代碼

uksort($params, 'strcmp'); 

看看如何在android中對參數進行排序。

+0

你能告訴我在哪兒能找到確切的秩序。我正在嘗試使用ajax來完成它,因爲我正在開發一個WinJS應用程序,但無法通過無效簽名消息? –

1

我也有同樣的問題後,研究天終於讓我找到了解決辦法希望這將有助於其他一些 我經歷的各種文件

1)Using the WooCommerce REST API – Introduction

2)woocommerce-rest-api-docs

3 )Scribe

4)scribe:1.3.5

上述文件和源碼指後我終於創建了做OAuth的1.0A的woocommerce HTTP的Android

的完整描述已經在我的圖書館

的自述部分增加了「一條腿」認證庫

檢查庫這裏

WoocommerceAndroidOAuth1 LIBRARY

+0

你節省了我很多時間。謝謝 –

相關問題