how to intergrate java with bitbucket, for sharing files to bitbucket account?
Asked by srinath reddy:

i am trying to create a java application for sending files to bitbucket using atlasian cloud rest-api's provided by atlasian. My goal here is to send files to bitbucket account without logging in to bitbucket account. the official document says that generate token and use it but its too confusing, coz we can generate many types of token in bitbucket account, please tell me how to create a access token and how to use it to access workspaces and repositories for sending files to workpaces or repositories, thanks in advance


Answers:
author: Patrik S
body: Hello [@srinath reddy](/t5/user/viewprofilepage/user-id/5170333) ,

Thank you for reaching out to Atlassian Community!

You can use an OAuth consumer in order to generate an access token that can then be used to authenticate calls to Bitbucket Cloud Public API. Let me share below the steps for using OAuth client credential flow:

1. [Create an OAuth consumer](https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/#Create-a-consumer) under the desired workspace. The callback URL can be defined as <https://localhost> and make sure to mark the checkbox **This is a private consumer** so it enables the client credential flow. The scopes to be selected in the OAuth consumers will depend on what API endpoints you would like to call. For example, if you would like to write to a repository, you will need an Oauth consumer that has at least **repo:read** and **repo:write** permissions.
2. After creating the OAuth consumer, you will be given a **Key** and **Secret** than can be exchanged by an Access token. Use the following API endpoint to exchange the consumer credentials for an access token :

   ```
   curl -X POST -u "Key:Secret" https://bitbucket.org/site/oauth2/access_token -d grant_type=client_credentials
   ```

   Please note that access tokens expire in 2 hours, and you will need to call the endpoint above again to generate a new access token.
3. Use the access token to authenticate an API call. In this example we are using the [Create a commit by uploading a file](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-source/#api-repositories-workspace-repo-slug-src-post) endpoint :

   ```
   curl -X POST https://api.bitbucket.org/2.0/repositories/<workspace>/<repository>/src \
   --header 'Authorization: Bearer <Access Token>' -F path/within/repo/myfile.txt=@file_to_upload.txt
   ```

   This will create a new commit on the main branch adding a file named myfile.txt inside the *path/within/repo* directory of your repository.

For more details on authentication methods and available endpoints, you're also welcome to check [Bitbucket Cloud public API reference.](https://developer.atlassian.com/cloud/bitbucket/rest/intro/)

Alternatively, we have recently released Repository Access Tokens (RATs) in Bitbucket Cloud that can also be used to authenticate API calls. In case would to learn more about RATs, and how to create and use them, I would recommend taking a look at our [Repository Access Tokens documentation](https://support.atlassian.com/bitbucket-cloud/docs/repository-access-tokens/).

Hope that helps! Let me know if you have any questions.

Thank you, [@srinath reddy](/t5/user/viewprofilepage/user-id/5170333) !

Patrik S

comments: author: srinath reddy
body: Hii, [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)
thanks for the reply,
i'm working on it. {#toc-hId--471642795}
------------------------------------------------------------------------------------------------------------------------------


Tags: bitbucket-cloud

cloud

rest-api