Receive 'You are not permitted to access this resource' when accessing bitbucket api from plugin

Edited

I'm trying to access a bitbucket commit through its REST api (Could not find the proper way through Bitbucket server api).  Here is the url I'm calling "http://localhost:7990/bitbucket/rest/api/1.0/projects/PROJECT_1/repos/rep_1/commits/current".  It works fine on browser, and return a json about commit 'current', which has commit message, author, etc.  My goal is to retrieve the commit message from it. 

However, when I'm calling the same url from the bitbucket plugin I put together, it only return {"errors":[{"context":null,"message":"You are not permitted to access this resource","exceptionName":"com.atlassian.bitbucket.AuthorisationException"}]} .  I'm using HttpClient and GetMethod to get the response from the url.  It did not work either after adding base authentication to the url like the following:

"http://admin:admin@localhost:7990/bitbucket/rest/api/1.0/projects/PROJECT_1/repos/rep_1/commits/current", where admin has the read/write access to rep_1.

What did I miss here?

1 answer

1 accepted

0 votes
Answer accepted
Caterina Curti
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Jul 11, 2018

Hi @Vivian Zhang,

The problem is likely with the way the authentication is performed, let's understand more about the plugin.

Is that running a curl command, maybe using Phyton code? Or is it based on Java code?

 

In case you are using REST APIs, the authentication options are listed on the Bitbucket Server - REST page (see the "Authentication") and there is an example using curl on this article.

 

Cheers,

Caterina - Atlassian

Hi, Thank you for the reply.  The curl command works fine with the same username and password.  I'm trying to retrieve the commit message using Java code inside the plugin.  Now I figured out how to use CommitService class to achieve the same result.  So I'm not blocked on that.  However it would be nice to understand why I'm getting that error message using HttpClient to get response from Bitbucket REST API. 

My next step is to retrieve the Jira ticket status, etc from Jira directly or from pull request.   I'm going to try the HttpClient route to get response from Jira REST API, and hopefully not running into the same error message again.  Please let me know if there is a better way of doing this.

Thanks.

Hi, Caterina,

I encountered similar issue when trying to connect Jira REST API using HttpClient.  I modified my code to do client.getState().setCredentials(..), etc to handle authentications.  This time it worked fine.  I'm able to complete my development and deployed my first plugin.  Thank you very much for the support.

Caterina Curti
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Jul 12, 2018

No problem @Vivian Zhang! I'm glad it is working now!

For development related questions, you can also ask on our Community dedicated to developers:

- https://community.developer.atlassian.com/ 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events