gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:52 26 4
gpt4 key购买 nike

我正在尝试访问必须实现 Oauth1 授权的资源。当我使用 postman 并可以选择添加空参数来签名时,它工作正常,但是当我尝试使用 Python 实现相同的功能时,它会抛出错误。

enter image description here

这是我的 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()

最佳答案

我在 Python 3 中遇到了类似的问题...尝试对不使用 token 的网站使用 OAuth 1.0。对我来说最困难的部分是获得正确的签名。 This answer got me where I needed to be.

关于python - Oauth1.0 : How to generate HMAC-SHA1 Oauth_signature without using Access token and token secret in python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52649911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com