4

我正在尝试使用 volley 在我的 android 应用程序中添加 Oauth1 授权

在邮递员中,当我添加 oauth_consumer_key、oauth_consumer_secret、token_key token_secret 等详细信息时,如下图所示

在 Postman 中添加授权详细信息

它会生成如下图所示的标题并成功收到响应。

邮递员生成的标头

邮递员生成的标头

Authorization:OAuth oauth_consumer_key="4e77abaec9b6fcda9kjgkjgh44c2e1",oauth_token="2da9439r34104293b1gfhse2feaffca9a1",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1482470443",oauth_nonce="cCbH5b",oauth_version="1.0",oauth_signature="A1QPwTATVF4x3cN0%2FN46CZrtSKw%3D"

问题

我搜索了很多以创建 oauth 签名,例如创建 postmasn 以附加 volley ServerConnectionChannel 但失败了。oauth_signature="A1QPwTATVF4x3cN0%2FN46CZrtSKw%3D"

当前代码

 public void doSendJsonRequest(final ERequest ERequest) {
 requestMethod = String.valueOf(ERequest.method);
        requestUrl = String.valueOf(ERequest.mReqUrl);
        if(requestMethod.equals(Request.Method.GET)){
            requestMethod = "GET";
        }else if(requestMethod.equals(Request.Method.POST)){
            requestMethod = "POST";
        }else if(requestMethod.equals(Request.Method.PUT)){
            requestMethod = "PUT";
        }else if(requestMethod.equals(Request.Method.DELETE)){
            requestMethod = "DELETE";
        }

 Long tsLong = System.currentTimeMillis()/1000;
        final  String ts = tsLong.toString();

      final String  kk = requestMethod+"&" + encode(requestUrl)+"&";
        final String  kk = encode("GET"+"&"
                + requestUrl+"&"
                + OAUTH_CONSUMER_KEY + "=\"4e77abaec9b6fcda9b11e89a9744c2e1\"&"
                +OAUTH_NONCE + "=\"" + getNonce()+ "\"&"
                +OAUTH_SIGNATURE_METHOD + "=\""+OAUTH_SIGNATURE_METHOD_VALUE+"\"&"
                +OAUTH_TIMESTAMP + "=\"" + ts + "\"&"
                +OAUTH_TOKEN +"=\"2da943934104293b167fe2feaffca9a1\"");


        RequestQueue queue = VolleyUtils.getRequestQueue();
        try {
            JSONObject jsonObject = ERequest.jsonObject;


            EJsonRequest myReq = new EJsonRequest(ERequest.method, ERequest.mReqUrl, jsonObject, createReqSuccessListener(ERequest), createReqErrorListener(ERequest)) {

                public Map < String, String > getHeaders() throws AuthFailureError {
//                    Long tsLong = System.currentTimeMillis()/1000;
//                    String ts = tsLong.toString();
                    String strHmacSha1 = "";
                    String oauthStr = "";

                    strHmacSha1 = generateSignature(kk, oAuthConsumerSecret, oAuthTokenSecret);
                    strHmacSha1 = toSHA1(strHmacSha1.getBytes());

                    Log.e("SHA   !",strHmacSha1);


                     oauthStr ="OAuth "+ OAUTH_CONSUMER_KEY + "=\"4e77abaec9b6fcda9b11e89a9744c2e1\","
                            +OAUTH_TOKEN +"=\"2da943934104293b167fe2feaffca9a1\","
                            +OAUTH_SIGNATURE_METHOD + "=\""+OAUTH_SIGNATURE_METHOD_VALUE+"\","
                            +OAUTH_TIMESTAMP + "=\"" + ts + "\","
                            +OAUTH_NONCE + "=\"" + getNonce()+ "\","
                            +OAUTH_VERSION + "=\"" + OAUTH_VERSION_VALUE + "\","
                            +OAUTH_SIGNATURE + "=\"" + strHmacSha1+ "\"";

                    Log.e("VALUE OF OAuth str",oauthStr);


                    Map<String, String> params = new HashMap<String, String>();
                                 params.put("Content-Type", "application/json");
                                 params.put("Authorization",oauthStr);
                                // params.put("Authorization",getConsumer().toString());



                                 return params;

                }


            };

            myReq.setRetryPolicy(new DefaultRetryPolicy(
                    DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 4,
                    BABTAIN_MAX_RETRIES,
                    BABTAIN_BACKOFF_MULT));
                              myReq.setHeader("Cache-Control", "no-cache");
                             //myReq.setHeader("Content-Type", "application/json");
                                 queue.add(myReq);
        } catch (Exception e) {
            e.printStackTrace();
        }

 private String generateSignature(String signatueBaseStr, String oAuthConsumerSecret, String oAuthTokenSecret) {
        byte[] byteHMAC = null;
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec spec;
            if (null == oAuthTokenSecret) {
                String signingKey = encode(oAuthConsumerSecret) + '&';
                spec = new SecretKeySpec(signingKey.getBytes(), "HmacSHA1");
            } else {
                String signingKey = encode(oAuthConsumerSecret) + '&' + encode(oAuthTokenSecret);
                spec = new SecretKeySpec(signingKey.getBytes(), "HmacSHA1");
            }
            mac.init(spec);
            byteHMAC = mac.doFinal(signatueBaseStr.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String base64 = Base64.encodeToString(byteHMAC, Base64.DEFAULT);
        return base64.trim();
    }

  private String toSHA1(byte[] convertme) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
        }
        catch(NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return byteArrayToHexString(md.digest(convertme));
    }

    private String byteArrayToHexString(byte[] b) {
        String result = "";
        for (int i=0; i < b.length; i++)
            result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
        return result;
    }

此代码创建一个签名,如 :oauth_signature="42a611860e29e893a435b555e7a9559a704f4e94" 并且未能获得授权。

收到类似错误:BasicNetwork.performRequest: url 的意外响应代码 401

?如何生成像邮递员一样使用 volley 提供的 oauth_signature

?我们如何在 volley 中添加 oauth1 签名..

请帮忙..谢谢

4

1 回答 1

3

刚刚在Oauth1中找到了一个生成nonce对应签名的github示例,并成功集成到我的项目中

这是链接:https ://github.com/rameshvoltella/WoocommerceAndroidOAuth1

于 2017-01-04T04:04:06.210 回答