Invalid signature in java

90 views
Skip to first unread message

Ananda Krishna

unread,
Oct 9, 2012, 6:15:45 AM10/9/12
to fatsecret-p...@googlegroups.com
Hi,
I am getting oauth_signature error .How to resolve this issue in java?
Error Message:Invalid signature: oauth_signature 'JmVGKnfp83t65HqlIAdyASawYhk
Any help would be appreciated.
Regards,
AnandaKrishna S

Len

unread,
Oct 9, 2012, 6:25:41 AM10/9/12
to fatsecret-p...@googlegroups.com
Hey AnandaKrishna,
 
There's many posts in the forums about resolving OAuth signature issues, so if you could please peruse those first and then you're welcome to post code (that doesn't reveal your secret).
 
All the best,
Len

Ananda Krishna

unread,
Oct 10, 2012, 2:17:58 AM10/10/12
to fatsecret-p...@googlegroups.com
hi ,
This is my code for hmac signature:
public static String computeHmac(String baseString, String key) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());
        mac.init(secret);
        byte[] digest = mac.doFinal(baseString.getBytes("utf-8"));
        return (Base64.encode(digest));
       
    }

    public static String encodeUrl(String web_url) throws Exception {
        String encodedReqUrl = URLEncoder.encode(web_url, "UTF-8");
        return (encodedReqUrl);
    }

    public static String randomString() {
        final String ALPHA_NUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuilder sb = new StringBuilder(5);
        for (int i = 0; i < 5; i++) {
            int ndx = (int) (Math.random() * ALPHA_NUM.length());
            sb.append(ALPHA_NUM.charAt(ndx));
        }
        return (sb.toString());
    }

    public static String getSignaturedUrl(String methodType) throws Exception {
        String outhSignature = "";
        String finalUrl = "";
        String requestUrl = "http://platform.fatsecret.com/rest/server.api";
        String ecnodedUrl = Hmac_signature.encodeUrl(requestUrl);
        Long tstamp = System.currentTimeMillis() / 1000;
        int timestamp = tstamp.intValue();
        String nonce = Hmac_signature.randomString();
        String parameters = "oauth_consumer_key=xxxxxxx&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + timestamp + "&oauth_version=1.0&oauth_nonce="+ nonce;
        String encodedParm = Hmac_signature.encodeUrl(parameters);
        String baseString = "POST&" + ecnodedUrl + "&" + encodedParm;
        String key = "yyyyyy&";
        outhSignature = Hmac_signature.computeHmac(baseString, key);
        finalUrl = "http://platform.fatsecret.com/rest/server.api?"+parameters+"&oauth_signature="+outhSignature+"&format=json&method="+methodType;
        return (finalUrl);

    }
Please tell me where am i going wrong..
Regards,
AnandaKrishna S
Reply all
Reply to author
Forward
0 new messages