10
votes

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

3
As I said in all examples the server support access_token,authorize,request_token_ready etc APis while my server don't. Am I missing something?zohar

3 Answers

3
votes

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.

26
votes

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

1
votes

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)