【问题标题】:Generating signature for API access (Oauth 1.0)为 API 访问生成签名(Oauth 1.0)
【发布时间】:2016-06-01 23:39:44
【问题描述】:

我正在尝试使用 python 和 oauth 1.0 访问 API。我不知道如何生成 oauth_signature。以下是我目前的代码,希望有人能提供帮助。

import oauth2 as oauth
import urllib.parse
import time
from random import getrandbits


api_key = 'xxxxxxxxxxxxxxxxxxx'
CONSUMER_KEY = 'xxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxx'
access_key = 'xxxxxxxxxxxxxxxxx'
access_secret_key = 'xxxxxxxxxxxxxxxxxxx'

consumer = oauth.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
token = oauth.Token(access_key, access_secret_key)

client = oauth.Client(consumer, token)

nonce = str(getrandbits(64))
print(nonce)

url = 'https://secure.tmsandbox.co.nz/Oauth/Authorize?oauth_token='

params = {'oauth_version': "1.0", 
          'oauth_nonce': oauth.generate_nonce(), 
          'oauth_timestamp': int(time.time()),
          'oauth_consumer_key': CONSUMER_KEY,
          'oauth_token': access_key,
          'oauth_nonce' : nonce, 
          'oauth_consumer_key': consumer.key,
          'oauth_signature': 'el078a5AXGi43FBDyfg5yWY',
          }

req = oauth.Request(method="POST", url='https://secure.tmsandbox.co.nz/Oauth/RequestToken?', parameters=params)



# Sign the request.
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, token)

api 开发者建议我如何访问它:

oauth_signature 有点难以想出,它是 大多数人绊倒。要生成签名,请从基础开始 以 HTTP 方法和 URL 编码地址开头的字符串 您正在调用(没有查询字符串)。当我们得到一个 请求令牌,这将是一个 POST 请求 https://secure.trademe.co.nz/Oauth/RequestToken?scope=MyTradeMeRead,MyTradeMeWrite (请注意,在本例中,范围参数用于请求 读/写访问)。这是我们的基本字符串的开始:

POST&https%3A%2F%2Fsecure.trademe.co.nz%2FOauth%2FRequestToken

请注意,我们用与号 (&) 分隔它们。排序所有 参数键(oauth_nonce 等)和任何查询字符串参数 按字母顺序(不包括 oauth_signature,因为我们就是这样 生成)并将它们附加到单独的字符串中,[name]=[value] 用&符号分隔每个。 URL对此进行编码并将其附加到 基本字符串的结尾,前面是 & 符号。

【问题讨论】:

  • 为什么要导入 oauth 2?

标签: python signature


【解决方案1】:

查看此answer。它解释了如何使用 HMAC - SHA1 签名方法并使用 OAuth 访问网站。

【讨论】:

  • 在该线程中发现了一个有趣的方法,可能会研究这个而不是加密:link
猜你喜欢
  • 1970-01-01
  • 2021-06-10
  • 2017-03-17
  • 1970-01-01
  • 2011-03-27
  • 2022-01-21
  • 2021-12-19
  • 2012-09-08
  • 1970-01-01
相关资源
最近更新 更多