Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple python oAuth 1.0 example with consumer key and secret

Tags:

python

oauth

I am new in python and oAuth world. I want to connect to my server with consumer key and secret and all the examples I found is where the server has access_token,authorize,request_token_ready etc api but my server does the oAuth authentication for me. So my question is how to connect with python to my server using oAuth (My server use oAuth 1.0)

elaboration: My server does not request token and access token. He use just the key and secret. How do I implement oAuth connection to this server in python

like image 651
zohar Avatar asked Jun 18 '12 14:06

zohar


3 Answers

This is a working example using requests_oauthlib

from requests_oauthlib import OAuth1Session
test = OAuth1Session('consumer_key',
                    client_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
url = 'https://one-legged-ouath.example.com/username/test'
r = test.get(url)
print r.content

I know this is an old question, but the accepted answer really doesn't address his question, since as the OP notes, none of the examples pertain to just using the key and secret, sans token.

It sounds as if you're using what I understand is referred to as OAuth 1.0a (One Leg), although some refer to it as OAuth 1.0a Two-legged.

I haven't tested this but there appears to be a pretty good example here:

https://github.com/CarmaSys/CarmaLinkAPI/wiki/Authentication-&-Permissions

There is another good example here:

https://stackoverflow.com/a/12710408/2599534

like image 172
roman Avatar answered Oct 04 '22 18:10

roman


If you're looking for a client with which to connect to your server with I can recommend rauth. There's a number of examples demonstrating both OAuth 1.0/a and 2.0.

like image 22
maxcountryman Avatar answered Oct 04 '22 17:10

maxcountryman


As roman already said, this is an old question, but there still are some APIs out there which are OAuth 1.0a (One Leg) - protected and today I spent a couple of hours finding a working solution for accessing such an API.

I hope, the solution might come handy for next one to face similar task.

My solution is based on roman's answer. Many thanks @roman!!

The default-response of the API that I wanted to access was in XML, therefore I needed a way to set request headers. It's pretty simple to do actually, if you know how.

from requests_oauthlib import OAuth1Session

CONSUMER_KEY = ""
CONSUMER_SECRET = ""

host = "rest.host.de"
uri = "/restapi/api/search/v1.0/statistic?geocode=1276001039"

oauthRequest = OAuth1Session(CONSUMER_KEY,
                    client_secret=CONSUMER_SECRET)

url = 'https://' + host + uri

headers = {
        'Accept': "application/json",
        'Accept-Encoding': "gzip, deflate",
    }

response = oauthRequest.get(url, headers=headers)

print(response.status_code)
print(response.content)
like image 34
Hans Avatar answered Oct 04 '22 17:10

Hans