【发布时间】:2013-03-10 05:59:42
【问题描述】:
from rauth import OAuth1Service
OAUTH_REQUEST = "https://bitbucket.org/!api/1.0/oauth/request_token"
OAUTH_AUTH = "https://bitbucket.org/!api/1.0/oauth/authenticate"
OAUTH_ACCESS = "https://bitbucket.org/!api/1.0/oauth/access_token"
service = OAuth1Service(
name='test',
consumer_key='xxxxxxxxxxxxxx',
consumer_secret='xxxxxxxxxxxxxxxxxxxx',
request_token_url=OAUTH_REQUEST,
access_token_url=OAUTH_ACCESS,
authorize_url=OAUTH_AUTH)
resp = service.get_raw_request_token()
print resp
我在 Bitbucket 上生成了一个消费者密钥对,但响应为 400。 知道发生了什么吗?
我查看了 Bitbucket 文档,并且 URL 是正确的。
编辑
感谢@maxcountryman 抽出宝贵时间。
我刚刚看了他的linkedlin example code:
import os
from rauth import OAuth1Service
OAUTH_REQUEST = "https://bitbucket.org/!api/1.0/oauth/request_token"
OAUTH_AUTH = "https://bitbucket.org/!api/1.0/oauth/authenticate"
OAUTH_ACCESS = "https://bitbucket.org/!api/1.0/oauth/access_token"
service = OAuth1Service(
name='test',
consumer_key='blah',
consumer_secret='blah',
request_token_url=OAUTH_REQUEST,
access_token_url=OAUTH_ACCESS,
authorize_url=OAUTH_AUTH)
# You can run python -m SimpleHTTPServer if you want a local callback
rtoken, rtoken_secret = service.get_request_token(params={'oauth_callback': 'http://localhost:8000'})
authorize_url = service.get_authorize_url(rtoken)
print 'Visit this URL in your browser: ' + authorize_url
pin = raw_input('Enter PIN from browser: ')
session = service.get_auth_session(rtoken,
rtoken_secret,
data={'oauth_verifier': pin})
reponame = raw_input('Enter the reponame: ')
new_name = raw_input('Enter a new repo name: ')
account_name = raw_input('Enter your account name: ')
url = 'https://api.bitbucket.org/1.0/repositories/%s/%s' %(account_name, reponame)
r = session.put(url, data={'name': new_name})
print r
例子:
(k)yeukhon@yeukhon-P5E-VM-DO:/tmp$ python bb2.py
Visit this URL in your browser: https://bitbucket.org/!api/1.0/oauth/authenticate?oauth_token=xxxxxxxxxxxxx
Enter PIN from browser: 216000000
Enter the reponame: newpatch
Enter a new repo name: junk-patch
Enter your account name: yeukhon
<Response [200]>
edit 使用 base_url 听取 max 的其他建议。
OAUTH_REQUEST = "https://bitbucket.org/!api/1.0/oauth/request_token"
OAUTH_AUTH = "https://bitbucket.org/!api/1.0/oauth/authenticate"
OAUTH_ACCESS = "https://bitbucket.org/!api/1.0/oauth/access_token"
service = OAuth1Service(
name='test',
consumer_key='blah',
consumer_secret='blah',
request_token_url=OAUTH_REQUEST,
access_token_url=OAUTH_ACCESS,
authorize_url=OAUTH_AUTH,
base_url='https://api.bitbucket.org/1.0/')
# You can run python -m SimpleHTTPServer if you want a local callback
rtoken, rtoken_secret = service.get_request_token(params={'oauth_callback': 'http://localhost:8000'})
authorize_url = service.get_authorize_url(rtoken)
print 'Visit this URL in your browser: ' + authorize_url
pin = raw_input('Enter PIN from browser: ')
session = service.get_auth_session(rtoken,
rtoken_secret,
data={'oauth_verifier': pin})
reponame = raw_input('Enter the reponame: ')
new_name = raw_input('Enter a new repo name: ')
account_name = raw_input('Enter your account name: ')
url = 'repositories/%s/%s' %(account_name, reponame)
r = session.put(url, data={'name': new_name})
print r.text
print r
【问题讨论】:
-
仅供参考,如果提供者提供了一致的“基本”URI 以从中进行 API 调用,您可以通过将
base_url=https://api.bitbucket.org/1.0/传递给服务构造函数来简化代码。然后你可以像这样拨打电话:session.get('user'),所以session.put('https://api.bitbucket.org/1.0/repositories...', ...)可能会变成session.put('{0}/{1}'.format(account_name, reponame), ...)。 -
@maxcountryman 非常有趣。非常感谢!刚试过。 :)
标签: python oauth bitbucket rauth