Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Trying to create Pull Request on BitBucket through Rest API - "401 Unauthorized"

Edited

I'm using this API documentation to create a Pull request on bitbucket but its not working, I keep getting a 401 - Unauthorized, even though I created a personal access token with Admin access and I "believe" I am using the correct username and access token (as my password) for the curl POST request:
https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests#post

This is sample JSON:

curl https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \
    -u my-username:my-password \
    --request POST \
    --header 'Content-Type: application/json' \
    --data '{
        "title": "My Title",
        "source": {
            "branch": {
                "name": "staging"
            }
        }
    }'

What is my repository is listed under a "project name"? where the heck does the Project name go in the URL? and the "my-username" is just under the Bitbucket website "Manage accounts" -> "Name" , correct? If so, then my JSON should be correct.

Can anyone help me? If I go to the repository on the Bitbucket GUI in web browser, I can definitely log in and create Pull Request manually - why cannot I do this through REST API? What is the most common cause for 401 - Unauthorized? Do I need to request special access from the repository owners, even though I have my Admin access token, and I can view & make Pull requests on Bitbucket to this repo through web browser???

1 answer

1 accepted

0 votes
Answer accepted

Hi @Al Pizano,

Sorry for the late response.

curl https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \
    -u my-username:my-password \
    --request POST \
    --header 'Content-Type: application/json' \
    --data '{
        "title": "My Title",
        "source": {
            "branch": {
                "name": "staging"
            }
        }
    }'

where

my-username -> https://bitbucket.org/account/settings/ , get your username, below bitbucket profile settings

my-password -> an app password with pullrequest -> write permission
https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/


Dont hesitate to get in touch if you have any further questions.
Cheers

@Joao Sgreccia We don't use the https://bitbucket.org website, which lets you create an app-password. We seem to use the https://bitbucket.company-name.com format, and I don't see that I can create an app-password there.

 

I can however create Personal Access Tokens or add SSH keys from my local git. I saw that the bitbucket 1.0 API seemed to allow Authentication using the Personal access token (Bearer), but I tried and still got a "401 - Unauthorized":

bbucketq1.PNG

 

Also, what about the "Project" name , that comes before the repository name? Doesn't that need to be added to the URL, or branch name? i.e. "projectName/RepoName/BranchName" (below my project name is "Radio2.0-INFRA" with repository name "deployable-radio"?

 

Also, as you can see, my repository "" is actually in a workspace called "". How can I be able to create a Pull Request programmatically and bypass these 401 - Unauthorized errors???

 

sorry, duplicated post entries.

Hi @Al Pizano , 

Sorry, for this, I thought you were talking about bitbucket cloud.

Please take a look at bitbucket server docs, and let me know if you have any questions.

Authentication: 
https://developer.atlassian.com/server/bitbucket/how-tos/example-basic-authentication/

Rest API:
https://developer.atlassian.com/server/bitbucket/how-tos/command-line-rest/

PullRequest:
https://docs.atlassian.com/bitbucket-server/rest/7.6.0/bitbucket-rest.html#idp291

Cheers

@Joao Sgreccia Thank you very much. I was FINALLY able to get it to work with your latest reply with information about the SERVER API and authentication.

 

Thank you!!!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,717 views 10 16
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you