The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Al Pizano
September 21, 2020

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

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

@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

[redacted]

Al Pizano
September 22, 2020

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!!!

TAGS
AUG Leaders

Atlassian Community Events