user2871676
user2871676

Reputation:

OAuth 1.0 - how to pass token in headers?

There's a website require OAuth 1.0 and is requiring using a token. I tried the code:

def request(path=None):
    token = access_token()
    headers = { 'Authorization' : 'oauth_token %s' %  token }
    res = requests.get('https://somewebsite.com/v1'+path, headers=headers)
    print res.text

or replace oauth_token with, access_token or TOK:. They just didn't work, which returns error Access_token is empty. Is authentication method all different with various sites?

Upvotes: 1

Views: 2576

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121486

The correct header format is:

Authorization: OAuth param1_name="param1 value", param2_name="param2 value"

so OAuth followed by comma-separated name-value pairs.

The oauth_token key is a parameter name:

headers = {'Authorization': 'OAuth oauth_token="%s"' %  token}

However, you need to send more than just the token, according to Accessing Protected Resources more parameters are expected.

If you are going to try and implement this yourself, at least take a look at the OAuth RFC and the Python oauthlib library source code. There is also a Requests-OAuthlib integration layer you can take a look at.

Upvotes: 1

Related Questions