unable to add favourite recipe

27 views
Skip to first unread message

Ananda Krishna

unread,
Nov 5, 2012, 5:12:54 AM11/5/12
to fatsecret-p...@googlegroups.com
Hi,
I am using the following code to add a recipe to a particular user profile.

public static String computeHmac(String baseString, String key) throws Exception {
        SecretKeySpec secret = new SecretKeySpec(key.getBytes("UTF-8"), "HMAC-SHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secret);
        byte[] digest = mac.doFinal(baseString.getBytes("UTF-8"));
        return (new String(Base64.encodeBase64(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 addToFavouritesRecipeResultURL(String methodType, String token, long id, String Key) throws Exception {
        String outhSignature = "";
        String finalUrl = "";
        String parameters = "";
        String header = "GET";
        String encodedHeader = Hmac_signature.encodeUrl(header);
        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() + timestamp;
        parameters = "format=json&method=" + methodType + "&oauth_consumer_key=" + Key + "&oauth_nonce=" + nonce + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + timestamp + "&oauth_token=" + token + "&oauth_version=1.0&recipe_id=" + id;
        String encodedParm = Hmac_signature.encodeUrl(parameters.trim());
        String baseString = encodedHeader + "&" + ecnodedUrl + "&" + encodedParm;
        String consumerSecret = "xxxxxxx";
        String conkey = consumerSecret.concat("&");
        outhSignature = Hmac_signature.computeHmac(baseString, conkey);
        String encouthSignature = Hmac_signature.encodeUrl(outhSignature);
        String finalParam = "format=json&method=" + methodType + "&oauth_consumer_key=" + Key + "&oauth_nonce=" + nonce + "&oauth_signature=" + encouthSignature + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + timestamp + "&oauth_token=" + token + "&oauth_version=1.0&recipe_id=" + id;
        finalUrl = "http://platform.fatsecret.com/rest/server.api?" + finalParam;
        return (finalUrl);
    }

But i am getting invalid signature error .Kindly tell me where am i going wrong.
Regards,
Ananda Krishna S

Reply all
Reply to author
Forward
0 new messages