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.
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?
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 :
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 :
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.
Do you mind sharing the exact scopes needed to enable code search at the workspace level?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.