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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Bitbucket code search api is not working

I am trying to use the bitbucket code search API and I am getting 401 error "Token is invalid or not supported for this endpoint."

I created a repository access token and can access this particular URL 'https://api.bitbucket.org/2.0/repositories/nasrulrules' with an authorization bearer token in the header without any issue.

But I couldn't access the code search API with the same token. I am having an invalid token issue. My code search api - 

I am following the above two articles. In these articles it is mentioned like 'GET /2.0/teams/{username}/search/code'

what username I should give? I already tried with 'nasrulrules'

In the first article I am seeing the api you provided and what is "%7B6f461d1e-a3dd-433b-a0e3-7a69daf6ea47%7D" ? 

https://api.bitbucket.org/2.0/teams/%7B6f461d1e-a3dd-433b-a0e3-7a69daf6ea47%7D/search/code?search_query=repo%3Aelasticsearch%20QueryBuilders

 

kindly provide more suggestions on this.

 

Note: This is a private repository and I am the only user in it. 

 

 

 

 

1 answer

0 votes
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Jan 10, 2023

Hello @nasrulrules ,

Welcome to Atlassian Community!

The 401 Authorization error usually means that your credentials are either invalid or do not have sufficient permissions to execute that action. If you created the token using an Oauth consumer, could you try to check all the scopes when creating the consumer to make sure you have the necessary permissions when doing the API call? 

Screenshot 2023-01-10 at 4.33.50 PM.png

As for the endpoint, Bitbucket Cloud API provides two different endpoints for code search, with the difference them being the type of the workspace: personal or shared (team) workspaces.

If you want to Code Search in a personal workspace : 

  • Search code in a user's repository 

    Example API call :
    curl --request GET \

      --url 'https://api.bitbucket.org/2.0/users/<personal workspace id>/search/code?search_query=foo+repo:demo' \

      --header 'Authorization: Bearer <Bearer token>' \

      --header 'Accept: application/json'

If you want to Code Search on a shared workspace : 

  • Search code in a team's repository 

    Example API call : 
    curl --request GET \

      --url 'https://api.bitbucket.org/2.0/teams/<shared workspace id>/search/code?search_query=foo+repo:demo' \

      --header 'Authorization: Bearer <Bearer token>' \

      --header 'Accept: application/json'

You're also welcome to check Difference between shared and personal workspaces for more details on each type of workspace.

Hope that helps! Let me know in case you're still running into issues with that endpoint.

Thank you, @nasrulrules.

Patrik S

@Patrik S I am getting the same 401 error. I currently have these scopes enabled.

Screenshot 2023-03-31 at 09.11.44.png

Do you mind sharing the exact scopes needed to enable code search at the workspace level?

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Apr 04, 2023

Hello @Johnny O ,

Unfortunately, workspace access tokens are not currently supported in the code search endpoints. I went ahead and raised a feature request for it : 

Feel free to add your vote and mark yourself as a watcher to receive updates whenever there's any progress on the implementation of that feature.

For now, to authenticate requests to the code search endpoints you will need to use the username and app password or OAuth access token.

Thank you, @Johnny O !

Patrik S

Thank you for that clarification and opening the feature request. Would updating the documentation to call out that those API endpoints need username and app password or auth access token be worth while?

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Apr 06, 2023

Hey @Johnny O ,

I've reached out internally to the team responsible for those API endpoints to get the documentation updated. Thank you for reporting this @Johnny O !

Patrik S

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events