LoginSignup
4
3

More than 5 years have passed since last update.

BitbucketのOAuth:access token取得まで

Posted at

どうも公式のHP通りだと上手く行かなかったのでメモっておく。

Consumer 登録

Bitbucketのサイトにログインして、アカウント設定の所のIntegrated applicationsに登録する所があります。

認証する

Consumer keyとConsumer secretをゲットしたので、あとは認証のURLを調べる必要があります。
https://confluence.atlassian.com/display/BITBUCKET/oauth+Endpoint
が公式ドキュメントっぽいのですが、何故かこっちの日本語ページ
http://confluence.atlassian.jp/pages/viewpage.action?pageId=33687493
の方が正解っぽいです。!が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"

さて、あとは認証するだけです。お好きな方法でどうぞ、といいたいところですが、せっかくなのでサンプル(Python)を載せておきます。rauthというライブラリを利用しています。

from rauth.service import OAuth1Service
def authorize():
    service = OAuth1Service(name='bitbucket', consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET,
                         request_token_url=OAUTH_REQUEST,
                         access_token_url=OAUTH_ACCESS,
                         authorize_url=OAUTH_AUTH)
    rtoken, rtokensecret = service.get_request_token(method='GET')
    auth_url = service.get_authorize_url(rtoken)
    print "Visit this url and copy&paste your PIN.\n{0}".format(auth_url)
    pin = raw_input('Please enter your PIN:')
    r = service.get_access_token('POST', request_token=rtoken, request_token_secret=rtokensecret,
                                 data={'oauth_verifier': pin})
    content = r.content
    return content['oauth_token'], content['oauth_token_secret']

では。

4
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3