Hi everyone,
I'm trying to automate some Bitbucket tasks using the REST API and repository access tokens, but I'm having some issues.
Use Case:
I am building a documentation project that will store files on Bitbucket, I need scoped access tokens that will have no access to any other repo on the same workspace. with zero user interactions, because I will not use Bitbucket Oauth, I will be using Google's
What I'm trying to do:
Commit Changes: I want to be able to programmatically create or update files in my repository using the token, but I'm not sure how to structure the requests or what endpoints to use.
Delete Files: I also need to be able to delete files remotely using the API and the token.
What I've tried:
repository:write
permission.My Questions:
Any help or guidance would be greatly appreciated!
Hi Mustafa,
You can provide the repository access token in the Authorization header as a bearer token when making requests to Bitbucket APIs, similar to an OAuth access token.
This is an example of committing a new file to the repo and also updating an existing file:
curl https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/src \
-F fileA.xml=@fileA.xml \
-F message="some commit message" \
--header 'Authorization: Bearer token_here'
If the file exists in the repo, its content will be replaced with the content of the file you are uploading.
This is an example of committing a file's deletion:
curl https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/src \
-F files=fileA.xml \
-F message="some commit message" \
--header 'Authorization: Bearer token_here'
You need to replace workspace-id, repo-slug, fileA.xml, some commit message and token_here with the respective values for your calls.
These calls will create the commits on the repo's main branch. If you want to specify a different branch, you can use -F branch=test-branch in your calls, where test-branch replace with the name of the branch.
Please feel free to reach out if you have any questions!
Kind regards,
Theodora
Thank you bro , it did work
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.