pythonoauthbitbucketrauth

Getting 400 from Bitbucket OAuth


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

I went on Bitbucket and generated a consumer key-pair, but the response was 400. Any idea what's going on?

I looked at the Bitbucket doc and the URL are correct.


edit

Thank you to @maxcountryman for taking his time here.

I just read his 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

Example:

(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 take additional advice from 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

Solution

  • You need to give the API an oauth_callback, like so:

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

    That should get you back a proper response from the provider.