Python 中的 Identi.ca Oauth

发布于 2024-12-31 21:26:54 字数 1398 浏览 0 评论 0原文

我正在尝试在 Identi.ca 上完成 Oauth 身份验证。基本上,我尝试使用适用于 Twitter 的相同代码,但是,当然,更改了 url。由于 HTTP 错误 400:错误请求,我什至无法获取请求令牌,如下所示。我真的很想知道我做错了什么,或者与 Twitter 的做法有何不同。到目前为止我所拥有的是:

from oauth import oauth
import urllib2

class IdenticaOauth:
  def __init__(self):
    self.request_token_url  = 'https://identi.ca/api/oauth/request_token'
    self.access_token_url   = 'https://identi.ca/api/oauth/access_token'
    self.authorize_url      = 'https://identi.ca/api/oauth/authorize'

    self.consumer_key = '8024d4db70d9e49d22728f25b4c1458b'
    self.consumer_secret = '4eb762cfe3c0a55950375dad795cf20e'
    self.consumer = oauth.OAuthConsumer(self.consumer_key, self.consumer_secret)
    self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()

  def get_unauthorized_request_token(self):
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_url = self.request_token_url)
    oauth_request.sign_request(self.signature_method, self.consumer, None)
    url = oauth_request.to_url()
    response = self.get(url)
    token = oauth.OAuthToken.from_string(response)
    return token

  def get(self, url):
    request = urllib2.Request(url)
    # urllib2.HTTPError: HTTP Error 400: Bad Request
    response = urllib2.urlopen(request)
    return response.read()

identica_oauth = IdenticaOauth()
request_token = identica_oauth.get_unauthorized_request_token()

I am trying to accomplish the Oauth authentication on Identi.ca. Basically, I am trying to use the very same code that works for Twitter, but, of course, changing the urls. I am not even being able to get the request token, because of a HTTP Error 400: Bad Request, as shown bellow. I really would like to know what I am doing wrong, or what differs from the Twitter way of doing it. What I have so far is:

from oauth import oauth
import urllib2

class IdenticaOauth:
  def __init__(self):
    self.request_token_url  = 'https://identi.ca/api/oauth/request_token'
    self.access_token_url   = 'https://identi.ca/api/oauth/access_token'
    self.authorize_url      = 'https://identi.ca/api/oauth/authorize'

    self.consumer_key = '8024d4db70d9e49d22728f25b4c1458b'
    self.consumer_secret = '4eb762cfe3c0a55950375dad795cf20e'
    self.consumer = oauth.OAuthConsumer(self.consumer_key, self.consumer_secret)
    self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()

  def get_unauthorized_request_token(self):
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_url = self.request_token_url)
    oauth_request.sign_request(self.signature_method, self.consumer, None)
    url = oauth_request.to_url()
    response = self.get(url)
    token = oauth.OAuthToken.from_string(response)
    return token

  def get(self, url):
    request = urllib2.Request(url)
    # urllib2.HTTPError: HTTP Error 400: Bad Request
    response = urllib2.urlopen(request)
    return response.read()

identica_oauth = IdenticaOauth()
request_token = identica_oauth.get_unauthorized_request_token()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文