0

I want to play preview of 7Digital clips obtained from Echonest API. For this purpose I want to generate oauth signature programmatically for every single request.

Url for playing clip- http://previews.7digital.com/clip/1234 But it need 2 legged oAuth.

For this purpose I used below code which I got from stackoverflow.

private static final String HMAC_SHA1 = "HmacSHA1";

private static final String ENC = "UTF-8";

private String getSignature(String url, String params)
            throws UnsupportedEncodingException, NoSuchAlgorithmException,
            InvalidKeyException {

    StringBuilder base = new StringBuilder();
    base.append("GET&");
    base.append(url);
    base.append("&");
    base.append(params);
    System.out.println("String for oauth_signature generation:" + base);

    byte[] keyBytes = (DIGITAL_CONSUMER_SECRET + "&").getBytes();

    SecretKey key = new SecretKeySpec(keyBytes, HMAC_SHA1);

    Mac mac = Mac.getInstance(HMAC_SHA1);
    mac.init(key);

    return new String(base64.encode(mac.doFinal(base.toString().getBytes(
            ENC))), ENC).trim();
}

But I am getting invalid signature error when I hit the final url for playing the clip.

I am able to play clips when I use 7digital tool for generating url.http://7digital.github.io/oauth-reference-page/

But I need to generate final url programmatically for every play request. Help me regarding this.

Vikrant_Dev
  • 430
  • 2
  • 15

2 Answers2

0

The 7digital android sdk can be found here and contains documentation, with examples of generating an Oauth signature https://github.com/7digital/7digital-Android-SDK

0

You are encoding the whole base string, instead you should encode the three parts of the base string separately, the '&' char MUST not be encoded.

Use my OAuth reference page to compare what you are generating to the reference implementation: https://bettiolo.github.io/oauth-reference-page/

The spec has more info about Base String generation: http://oauth.net/core/1.0a/#anchor13

Marco Bettiolo
  • 5,071
  • 6
  • 30
  • 35