Getting all repositories under the workspace
Asked by Eyal David:

Hi Guys

im having troubles to generate access token with simple curl ... I need this access token in order to execute

curl --request GET \\ --url '<https://api.bitbucket.org/2.0/repositories/{workspace>}' \\ --header 'Authorization: Bearer **\<access_token\>**' \\ --header 'Accept: application/json'

in the documentation :

<https://developer.atlassian.com/cloud/bitbucket/rest/intro/#authentication>

i can't find the the curl command for authenticate...

**tried to :**

curl --request GET [https://bitbucket.org/site/oauth2/authorize?client_id={client_id}\&response_type=token](https://bitbucket.org/site/oauth2/authorize?client_id={client_id}&response_type=token)

and got : no matches found....

I appreciate any help here

Thanks

Eyal


Answers:
author: Theodora Boudale
body: Hi [@Eyal David](/t5/user/viewprofilepage/user-id/4676376) and welcome to the community!

Please see below the steps to obtain the access token:

**1.** After you create the OAuth consumer in the Bitbucket workspace, open a tab in your browser and enter the following URL:

```
https://bitbucket.org/site/oauth2/authorize?client_id={consumer_key}&response_type=code
```

Replace **{consumer_key}** with the key of the OAuth consumer, no brackets in the URL, and then hit Enter.

If the callback URL you set for the OAuth consumer was e.g. <http://localhost>, you will get redirected to something like

```
http://localhost/?code=fpYPDYsryEpsRawrj4
```

Copy the code from that URL: fpYPDYsryEpsRawrj4

Please note this step needs to be executed on your browser, it is not possible to obtain the code with curl.

**2.** You can then obtain an access token by executing the following command

```
curl -X POST -u "key:secret" https://bitbucket.org/site/oauth2/access_token -d grant_type=authorization_code -d code=fpYPDYsryEpsRawrj4
```

Replace **key** with the consumer's key  
Replace **secret** with the consumer's secret  
Replace the code value in there (fpYPDYsryEpsRawrj4) with your code from the previous step

**3.** The output will include an access_token field and a refresh_token field.

You can use the value of the access token for 2 hours in your API calls before it expires.

You will then have to use the refresh token from the output in step 2 to get a new access token and a new refresh token.

```
curl -X POST -u "key:secret" https://bitbucket.org/site/oauth2/access_token -d grant_type=refresh_token -d refresh_token={2Eoiw6UJ707AD9aIyUR}
```

Replace **key** with the consumer's key  
Replace **secret** with the consumer's secret  
Replace the value of the refresh_token in there (2Eoiw6UJ707AD9aIyUR) with the one from your output in step 3.

This command will give you a new access token and a new refresh token and you can use this command from now on to generate new access and refresh tokens when each access token expires.

If you have any questions, please feel free to let me know.

Kind regards,  
Theodora

comments: author: Eyal David
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) Thank you very much for your fast response.

is there a way to get all repositories names under the workspace ?

i'm using :

"curl --request GET \\  
--url '<https://api.bitbucket.org/2.0/repositories/${workspace>}' \\  
--header 'Authorization: Bearer ${token}' \\  
--header 'Accept: application/json'"

the json doesn't include the list of repositories under the account .... i used the company name as workspace

any suggestion ?

Thanks

Eyal


author: Theodora Boudale
body: Hi Eyal,

I would suggest checking first if the OAuth consumer has Read permissions for Repositories (from the workspace's **Settings** \> **OAuth consumers** \> **Edit** the consumer).

If the permissions look ok, could you please let me know:  
**1.** Did you generate the OAuth consumer for the company workspace? Or for your personal workspace?  
**2.** Do you get no output at all when you run this call?  
**3.** Could you run the curl call with the flag **-vv** as well and copy-paste the output here?  
(Please make sure to remove any sensitive info from the output prior to sharing it, like the access token, x-consumer-client-id, any workspace/repo urls etc)  
**4.** Can you confirm if you are using in the url the workspace ID that can be found here <https://bitbucket.org/company-workspace/workspace/settings> where company-workspace replace with the one of your company's workspace?

I see that you have created a support ticket for this issue as well, so if you don't feel comfortable sharing any of this info here, you can also share it in the support ticket, which is visible only to you and Atlassian staff.

Kind regards,  
Theodora


author: Eyal David
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086)

it worked out that i didn't do the pagination ... once did it got all the repositories

Thanks

Eyal


author: Theodora Boudale
body: Hi Eyal,

Thank you for the update, I'm glad that you figured this out.

Please feel free to reach out if you ever need anything else!

Kind regards,  
Theodora


author: Eyal David
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086)

Thank you very much ! appreciated !


author: Kumaran Ramalingam
body: [@Eyal David](/t5/user/viewprofilepage/user-id/4676376)

Could you please let me know how did you used pagination concept here to get the list of all repositories.


author: Theodora Boudale
body: Hi [@Kumaran Ramalingam](/t5/user/viewprofilepage/user-id/5398897) and welcome to the community!

If the API returns more than one page, there will be a field called **next** in the output. The value of this field is the link to the next page of the results, so you can use it to run another API call to get the second page. If there is a third page, you will again see a next field with the link to the third page. The lack of a **next** link in the response indicates the end of the collection.

You can read more about pagination here:

* <https://developer.atlassian.com/cloud/bitbucket/rest/intro/#pagination>

Kind regards,  
Theodora


author: Kumaran Ramalingam
body: Hi [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086)

Thank you Sir That really helped me a lot actually the real issue was the request python module when I am using it I didnt pass the Basic HTTP authentication properly but still I was getting the paginated results that coz its the public repository I was getting (without authentication). After using HTTPrequest Basic Authentication Method parsed Username and app password made my day.

Ur insight on this helped me a lot to use python to loop over the page number to achieve the same. Thanks you a lot. Have a great day.


author: Theodora Boudale
body: Thank you for the update and you are very welcome, I'm glad I could help!

Have a nice day too!


Tags: bitbucket-cloud

cloud