pythonoauthrauth

invalid signature from fatsecret API using rauth


I have successfully followed the rauth OAuth1 examples to get my access tokens, and hence retrieve data from the fatsecret API. I store my access_token and access_token_secret in a shelve database. My problem is that I am receiving an "invalid signature" error when I try to use my stored tokens to retrieve more data later on.

Here is my original script to get tokens and retrieve the exercise_entries.get method:

from rauth.service import OAuth1Service
import shelve

api_url = 'http://platform.fatsecret.com/rest/server.api'
shelf = shelve.open('token_shelf.db')

fatsecret = OAuth1Service(
    consumer_key = 'xxxxxxxxxxxxx',
    consumer_secret = 'xxxxxxxxxxxxx',
    name = 'fatsecret',
    request_token_url = 'http://www.fatsecret.com/oauth/request_token',
    access_token_url = 'http://www.fatsecret.com/oauth/access_token',
    authorize_url = 'http://www.fatsecret.com/oauth/authorize')

request_token, request_token_secret = fatsecret.get_request_token(
                        method = 'GET',
                        params = {'oauth_callback':'oob'})

authorize_url = fatsecret.get_authorize_url(request_token)

print 'Visit this URL in your browser: ' + authorize_url
pin = raw_input('Enter PIN from browser: ')
shelf['fatsecret_request_token'] = request_token
shelf['fatsecret_request_token_secret'] = request_token_secret
shelf['fatsecret_pin'] = pin

session = fatsecret.get_auth_session(
                                     request_token, 
                                     request_token_secret, 
                                     params={'oauth_verifier': pin}
                                     )
shelf['fatsecret_access_token'] = session.access_token
shelf['fatsecret_access_token_secret'] = session.access_token_secret

my_params = {'method': 'exercise_entries.get', 'format': 'json'}
r = session.get(api_url, params=my_params)

print r.json()
print r.content
shelf.close()

I then try to restore my access_token and access_token_secret from the shelf and open a new session, but I am told I have an invalid signature.

from rauth.service import OAuth1Service
import shelve
api_url = 'http://platform.fatsecret.com/rest/server.api'
shelf = shelve.open('token_shelf.db')
fs_access_token = shelf['fatsecret_access_token']
fs_access_token_secret = shelf['fatsecret_access_token']

fatsecret = OAuth1Service(
    consumer_key = 'xxxxxxxxxxxxx',
    consumer_secret = 'xxxxxxxxxxxxx',
    name = 'fatsecret',
    request_token_url = 'http://www.fatsecret.com/oauth/request_token',
    access_token_url = 'http://www.fatsecret.com/oauth/access_token',
    authorize_url = 'http://www.fatsecret.com/oauth/authorize')

session = fatsecret.get_session((fs_access_token,fs_access_token_secret))

my_params = {'method': 'exercise_entries.get', 'format': 'json'}
r = session.get(api_url,params=my_params)
print r.content
print r.url
shelf.close()

This returns r.content as:

{ "error": {"code": 8, "message": "Invalid signature: oauth_signature 'ccZpSYAPSn+umkTxcAVH7EChVvw='" }}

and r.url is:

http://platform.fatsecret.com/rest/server.api?oauth_nonce=604416f368159818e3ad8252a0da323be16319a3&format=json&oauth_consumer_key=xxxxxxxxxxxxx&oauth_timestamp=1390015877&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=xxxxxxxxxxxxx&oauth_signature=l4Ricqpbbwl%2BHPS2ItLLnvXQo%2FA%3D&method=exercise_entries.get

The only thing that catches my eye is that the r.url parameters do not seem to be lexigraphically sorted, but I don't know if that accurately reflects what was sent to fatsecret, and anyway it worked fine in the first script.

I have tried something similar using OAuth1Session instead of OAuth1Service, but I receive exactly the same results.

I'd appreciate any help to get this working.


Solution

  • I checked this code countless times and couldn't find anything wrong. As I added in extra printing for debugging I noticed that I retrieved access_token twice on line 6 of my re-use session. After all that it was just a typo.

    Change:

    fs_access_token = shelf['fatsecret_access_token']
    fs_access_token_secret = shelf['fatsecret_access_token']
    

    To:

    fs_access_token = shelf['fatsecret_access_token']
    fs_access_token_secret = shelf['fatsecret_access_token_secret']
    

    So the above code is actually a good demonstration of authenticating with the fatsecret api with python.