Postman Rest Client如何创建Oauth签名?无法在android中解析Oauth_Signature

时间:2014-05-20 12:57:33

标签: android encryption oauth

我正试图点击1.0版本的Oauth网络服务。 我可以成功完成邮递员客户端,但是无法完成这项工作 Android应用程序。

使用的图书馆: - 路标-commonshttp4-1.2.1.2.jar

API所需的参数: - oauth_consumer_key oauth_nonce oauth_version oauth_signature oauth_signature_method oauth_timestamp

代码: -

HttpClient httpclient = new DefaultHttpClient();


            // generate the oauth_signature
            String urlParamsForSignature = "oauth_consumer_key="+consumerKey + 
                    "&oauth_nonce=" + "pT6c0H"+
                    "&oauth_signature_method=HMAC-SHA1" +
                    "&oauth_timestamp=" + timestamp +
                    "&oauth_version=1.0";
            String baseString = "https://oauth.withings.com/account/request_token?" + urlParamsForSignature;
            String signature = computeHmac(URLEncoder.encode(baseString), consumerSecret);
            // add it to params list
            qparams.add(new BasicNameValuePair("oauth_signature", signature));

            // generate URI which lead to access_token and token_secret.
            String urlParams = "oauth_consumer_key="+consumerKey + 
                        "&oauth_nonce=" + "pT6c0H"+
                        "&oauth_signature=" + signature +
                        "&oauth_signature_method=HMAC-SHA1" +
                        "&oauth_timestamp=" + timestamp +
                        "&oauth_version=1.0";

            String url = "https://oauth.withings.com/account/request_token?" + urlParams;

            HttpGet httpget = new HttpGet(url);
            // output the response content.
            System.out.println("oken and Token Secrect:");

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                int len;
                byte[] tmp = new byte[2048];
                while ((len = instream.read(tmp)) != -1) {
                    System.out.println(new String(tmp, 0, len, ENC));
                }
            }



public String computeHmac(String baseString, String key)
{
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());
        mac.init(secret);
        byte[] digest = mac.doFinal(baseString.getBytes());
        return new String(Base64.encodeBase64(digest));
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

根据您发布的代码..您没有使用路标库来生成签名。您正在使用自定义代码。

您可以使用路标库,如下所示:

//create an oAuth consumer and provide CONSUMER_KEY & CONSUMER SECRET.
DefaultOAuthConsumer defaultOAuthConsumer  = new DefaultOAuthConsumer("CONSUMER_KEY","CONSUMER_SECRET");

   //REQUEST URL
    String url = "https://oauth.withings.com/account/request_token";
    try {
        // sign the url with consumer. (This will add all oAuth parameters to the query automatically and return the signed request url with all parameter). 
        url = defaultOAuthConsumer.sign(url);
    } catch (OAuthMessageSignerException e) {
        e.printStackTrace();
    } catch (OAuthExpectationFailedException e) {
        e.printStackTrace();
    } catch (OAuthCommunicationException e) {
        e.printStackTrace();
    }

// use the url to make your request.