3
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示例代码

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]>

编辑从 max using 中获取更多建议base_url

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
4

1 回答 1

5

您需要给 API 一个oauth_callback,如下所示:

r = service.get_raw_request_token(params={'oauth_callback': 'http://example.com/'})

这应该会让您从提供商那里得到正确的回应。

于 2013-03-28T21:33:45.280 回答