1

I am trying to access a resource for which I have to implement Oauth1 authorization. When I am using from postman with the option to add empty parameters to signature its working fine but when I am trying to implement the same thing with Python its throwing error.

enter image description here

This is my python code So please tell me what are the changes i have to make

import requests
import json
from requests_oauthlib import OAuth1Session
from requests_oauthlib import OAuth1
import urllib
import random
import time
from hashlib import sha1
import hmac
def sign_request(epocTime,nounce):
    key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX' 
    url = "GET&https://XXXXXXXXXXXXXXXXXXXXX/api/XXXXXXXXXXXXX&oauth_consumer_key=XXXXXXX&oauth_nonce=12345&oauth_signature_method=HMAC-SHA1&oauth_timestamp={}&oauth_version=1.0".format(epoch_time)
    raw = urllib.quote(url)
    hashed = hmac.new(key, raw, sha1)
    print hashed
    return hashed.digest().encode("base64")

def test():
    nounce = "12345"
    epoch_time = int(time.time())
    url = "https://XXXXXXXXXXXXXX/api/XXXXXXXXXXXXXXXXXXXXXX"
    strr = sign_request(epoch_time,nounce)
    print strr
    querystring = {"oauth_consumer_key":"1XXXXXXXXXXXXXXXXX","oauth_token":"",
    "oauth_signature_method":"HMAC-SHA1","oauth_timestamp":epocTime,
    "oauth_nonce":nounce,"oauth_version":"1.0",
    "oauth_signature":strr}
    response = requests.request("GET", url, params=querystring)
    print response.text
test()
Akash Sourav Nayak
  • 203
  • 1
  • 8
  • 21

1 Answers1

1

I was having a similar issue in Python 3...trying to use OAuth 1.0 for a site that didn't use tokens. The hardest part for me was getting the right signature. This answer got me where I needed to be.

grove80904
  • 419
  • 2
  • 5
  • 14