#!/usr/bin/env bash
set -e
KEY=public-key
LABEL=key123
###
# Method 1. Authenticate by your account & password
###
AUTH=user:password
curl -X POST --user $AUTH \
https://api.bitbucket.org/1.0/repositories/plaxieappier/cmpboard/deploy-keys \
--data "key=$KEY&label=$LABEL"
###
# Method 2. Authenticate by OAuth2 (Google)
#
# ref. https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html#OAuthonBitbucketCloud-OAuth2.0
###
CLIENT_ID=client-id # As well as "Key" in OAuth consumers
SECRET=secret
# Step 1. Go to following URL to get the "code"
# https://bitbucket.org/site/oauth2/authorize?client_id=$CLIENT_ID&response_type=code
# Then, Copy the "code" value from URL query string
$CODE=from-browser-url
# Step 2. Get the access token
curl -X POST -u "$CLIENT_ID:$SECRET" \
https://bitbucket.org/site/oauth2/access_token \
-d grant_type=authorization_code -d code=$CODE
# API will returns an JSON object including "access_token", for example:
# {
# "access_token": "...Z3O1OXk...",
# "scopes": "repository:write",
# "expires_in": 3600,
# "refresh_token": "...ySRtPV...",
# "token_type": "bearer"
# }
ACCESS_TOKEN=from-step2-response
# Step 3. Request API with access token
curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \
https://api.bitbucket.org/1.0/repositories/plaxieappier/cmpboard/deploy-keys \
--data "key=$KEY&label=$LABEL"