1

I want to use Bitbucket's Rest API with Bitbucket's two-factor authentication enabled, so I can administer my account using curl via the terminal. Previously, I made REST API calls without 2FA and now I want to make this transition.

With 2FA enabled, you need to use the OAuth 2.0 protocol to make API calls; rather than the standard REST API invocations you'll find on the Bitbucket site.

Now, I got as far as creating a so-called consumer, on the Bitbucket website. This generates a Key and a Secret.

The part where I got stuck is in the following. With this Key and Secret, you can obtain a so-called access token (which expires after an hour) via

https://bitbucket.org/site/oauth2/authorize?client_id={client_id}&response_type=code

, which you can do via a the curl command

$ curl -X POST -u "client_id:secret" \ https://bitbucket.org/site/oauth2/access_token \ -d grant_type=authorization_code -d code={code}.

I don't know how to obtain this access token; i.e., the step from invoking the curl command to having it in a Bash variable.

Once we've obtained our access token we can make API requests by including it in our curl command, according to the Bitbucket documentation. I presume, something like,

curl -u "client_id:secret" https://api.bitbucket.org/2.0/[API Request] --data-urlencode "access_token=$[Access_token]" --data "[api_request_data]=[Api_request_data]"

, where $[Access_token] is our Bash variable holding our unexpired access token.

I want to create two functions implemented in Bash: one to obtain an access token; and, one to refresh the access token. Or is there a more simple way?

Anyhow, a quick outline on how to make REST API calls with 2FA enabled, would be highly appreciated.

1
  • First you need to get the temporary code. Which you will get redirecting the user to the first URL you have, passing your client_id. Then in the callback you need to extract the code (temp code). After that you can request the access token passing client_id, secret and the temp code, doing the request with the CURL you mentioned. In the response this will include the access token which you should save to request the information you need. Commented Dec 1, 2016 at 6:24

1 Answer 1

1

You can use a Bitbucket "app password". More info here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.