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

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

Al Pizano September 21, 2020 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

1 vote
Answer accepted
Joao Sgreccia
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 22, 2020

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

Al Pizano September 22, 2020 edited

@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???

 

Al Pizano September 22, 2020 edited

[redacted]

Al Pizano September 22, 2020 edited

sorry, duplicated post entries.

Joao Sgreccia
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 22, 2020

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

Al Pizano September 23, 2020

@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
atlassian, team '25, conference, certifications, bootcamps, training experience, anaheim ca,

Want to make the most of Team ‘25?

Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.

Learn more
AUG Leaders

Upcoming Bitbucket Events