Song previews with OAuth signed requests (Python)

506 views
Skip to first unread message

frea...@gmail.com

unread,
Mar 3, 2014, 4:18:43 PM3/3/14
to 7digit...@googlegroups.com
Hi,

I need to access song previews through the API. I learned from this page that it now requires OAuth signed requests. Since I'm using Python, I tried rauth and requests-oauthlib for this by following the examples on their respective GitHub pages.

In the case of rauth, this is what I do...

    digital7 = OAuth1Session(client_key=MY_KEY, client_secret=MY_SECRET)
    url = 'http://previews.7digital.com/clip/1940593'
    resp = digital7.get(url) # I expect 'resp' to contain the preview
    print resp.status_code
    print resp.text

... and my output is

    401

    Error: Authorization failed for method <GET> and URL <http://previews.7digital.com/clip/1940593>. Missing oauth_consumer_key


In the case of requests-oauthlib, this is what I do...

    digital7 = rauth.OAuth1Service(
      name='7digital',
      consumer_key=MY_KEY,
      consumer_secret=MY_SECRET,
      request_token_url='https://api.7digital.com/1.2/oauth/requesttoken',
      access_token_url='https://api.7digital.com/1.2/oauth/accesstoken',
      authorize_url='https://api.7digital.com/1.2/oauth/authorize',
      base_url='https://api.7digital.com/1.2/')
   
    request_token, request_token_secret = digital7.get_request_token()

... and I get the following XML response

<?xml version="1.0" encoding="utf-8"?><response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" status="ok" version="1.2" xsi:noNamespaceSchemaLocation="http://api.7digital.com/1.2/static/7digitalAPI.xsd"><oauth_request_token><oauth_token>C7XGYVV</oauth_token><oauth_token_secret>HZgdt8ii/0CHCgYQ8szQtA==</oauth_token_secret></oauth_request_token></response>

I can see that it contains oauth_token and oauth_token_secret. I don't know how to use them. I tried generating the request as follows

    http://previews.7digital.com/clip/1940593?oauth_token=C7XGYVV&oauth_token_secret=HZgdt8ii/0CHCgYQ8szQtA==&oauth_consumer_key=MY_KEY

but it says

    Error: Authorization failed for method &lt;GET&gt; and URL &lt;http://previews.7digital.com/clip/1940593?oauth_token=C7XGYVV&amp;oauth_token_secret=HZgdt8ii/0CHCgYQ8szQtA==&amp;oauth_consumer_key=MY_KEY&gt;. Missing OAuth signature

Now where can get OAuth signature (and other required fields) from?

I spent hours going through examples from other APIs, the OAuth website, etc. but I could not get even one lousy preview in the end. And I have no idea if what I've done until now is even correct. I'll be very grateful if someone can tell me how to get a song preview with the API.

Thanks!

som.nho...@7digital.com

unread,
Mar 4, 2014, 6:53:23 AM3/4/14
to 7digit...@googlegroups.com, frea...@gmail.com
Hi there,

The track preview endpoint requires a 2-legged OAuth signed request with your consumer key and consumer secret. Neither a request token nor an access token is required for previews. The track preview signed request should look something like the following:

'http://previews.7digital.com/clip/1940593?oauth_consumer_key=YOUR_KEY_HERE&oauth_nonce=847707743&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1393932351&oauth_version=1.0&oauth_signature=g0woXobxWFbapmQhtQ4H4vhbawk%3D'

You can use this app to generate a signature to test the endpoint.  

I'm not familiar with Python, so can't really assist you with your code. We've tried searching for some API wrappers in Python for you, but none deal with generating signatures. But I hope the above information and signature generator helps.


Regards,
Som



This email, including attachments, is private and confidential. If you have received this email in error please notify the sender and delete it from your system. Emails are not secure and may contain viruses. No liability can be accepted for viruses that might be transferred by this email or any attachment. Any unauthorised copying of this message or unauthorised distribution and publication of the information contained herein are prohibited.

7digital Limited. Registered office: 69 Wilson Street, London EC2A 2BB.
Registered in
England and Wales. Registered No. 04843573.

filip

unread,
Mar 4, 2014, 7:07:53 AM3/4/14
to 7digit...@googlegroups.com, frea...@gmail.com
Found a snippet of code i helped to put together at one of the Music Hack Days, the guy got it working eventually, but i'm not a python expert and haven't tested it myself . Worth a try anyway. It should print a working link to play a preview clip.

import oauth2 as oauth

consumer_key = 'YOUR_KEY_HERE'
consumer_secret = 'YOUR_SECRET_HERE'
consumer = oauth.Consumer(consumer_key, consumer_secret)

req = oauth.Request(method="GET", url=request_url,is_form_encoded=True)

req['oauth_timestamp'] = oauth.Request.make_timestamp()
req['oauth_nonce'] = oauth.Request.make_nonce()
sig_method = oauth.SignatureMethod_HMAC_SHA1()

req.sign_request(sig_method, consumer, token=None)

print req.to_url()

Srikanth Cherla

unread,
Mar 4, 2014, 9:05:46 AM3/4/14
to filip, 7digit...@googlegroups.com
Thank you very much for the replies, Filip and Som!

Filip, the code snippet that you shared worked perfectly. I'm using it now. And it also seems more straightforward than using the other Python modules.

Again, thanks a lot!
Reply all
Reply to author
Forward
0 new messages