简体   繁体   English

Oauth1.0:如何在不使用python中的访问令牌和令牌密钥的情况下生成HMAC-SHA1 Oauth_signature?

[英]Oauth1.0: How to generate HMAC-SHA1 Oauth_signature without using Access token and token secret in python?

I am trying to access a resource for which I have to implement Oauth1 authorization. 我正在尝试访问必须为其执行Oauth1授权的资源。 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. 当我使用来自邮递员的选项来添加空参数来签名时,它的工作正常,但是当我尝试使用Python实现相同的操作时,会抛出错误。

在此处输入图片说明

This is my python code So please tell me what are the changes i have to make 这是我的python代码,所以请告诉我我需要进行哪些更改

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()

I was having a similar issue in Python 3...trying to use OAuth 1.0 for a site that didn't use tokens. 我在Python 3中遇到了类似的问题...试图对不使用令牌的网站使用OAuth 1.0。 The hardest part for me was getting the right signature. 对我而言,最难的部分是获得正确的签名。 This answer got me where I needed to be. 这个答案将我带到了我需要去的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM