Hsn
Hsn

Reputation: 1238

OAuth1 Authentication of Rest API

I need to do a post request according to the scenario given below. I only have consumers_key and consumer_secret. I don't know from where to get all the other fields. and also do i need to send the Authorization request including keys in the header?

A request token for the application must be obtained using the consumer key from the above response.

Request

POST /public/v1/oauth1/request_token
  Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  Authorization: OAuth
    oauth_consumer_key="btgd2cg2bm3lbjqsfv150fj9q8",
    oauth_signature_method="HMAC-SHA1",
    oauth_timestamp="1467792887",
    oauth_nonce="9fd50a6f-40e0-41be-8809-34aa832b688e",
    oauth_callback="oob",
    oauth_signature="hcE6Q3boTytkHmM72xegCP1Y3W4%3D"

I am doing something like this

import requests
url="https://api.discovergy.com"
request_token_url=url+'/public/oauth1/request_token'
head={'Accept':'text/html, image/gif, image/jpeg, *;q=.2, '/';q=.2',
'Authorization':'OAuth'}
data= dict(oauth_consumer_key=client_key, oauth_signature_method='HMAC-SHA1', oauth_timestamp='1467792887', oauth_nonce='9fd50a6f-40e0-41be-8809-34aa832b688e', oauth_callback='oob', oauth_signature='hcE6Q3boTytkHmM72xegCP1Y3W4%3D')

r=request.post(url=request_token_url, data=data, headers=head)
print(r.content)

It gives 400 status..

Upvotes: 1

Views: 1844

Answers (3)

mikegrep
mikegrep

Reputation: 121

 Recent versions does not have problem to request authentication using OAuth1 from request_oauthlib

from requests_oauthlib import OAuth1, OAuth1Session

OAUTH = OAuth1(API_KEY, API_KEY_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
HEADERS = {'Content-Type': 'application/json', 'Accept': '*/*'}

requests.method(url, data=json.dumps(payload), headers=HEADERS, auth=OAUTH)

Upvotes: 0

andig
andig

Reputation: 13868

The Discovergy API docs are at https://api.discovergy.com/docs/. I've setup a small PHP Discovergy API client at https://github.com/andig/discovergy. It demonstrates the entire process. For the /request_token POST (it is a POST) make sure to use the consumer key obtains from /consumer_token, not the credentials provided by Discovergy.

Update I've also noticed that according to your example you seem to pass the parameters as POST body. These need to be in the header as in the first code block you've shown.

Update 2 Also make sure to accept the right content types, at least application/json, maybe plus text/plain

Upvotes: 0

BoboDarph
BoboDarph

Reputation: 2891

Oauth1 has a different authorisation building module in python for Requests. See that documentation and the help files for examples of use.

The 400 RC you get is because you try to POST to that endpoint and the method is not supported. Most likely you can only GET from there.

Upvotes: 1

Related Questions