Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

API call to enable repository access to a new user

parajaganesh November 2, 2023

I had created a project level token with admin access and was trying to use Bitbucket API call (as recommended in "The Bitbucket Server REST API (atlassian.com)"). Here was my API call:

curl --request PUT --url "https://xxxx:8443/rest/api/latest/projects/PROJ/repos/test/permissions/users?name={new_user_name}&permission={REPO_READ}"


However, getting below error while running this API:

{"errors":[{"context":null,"message":"You are not permitted to access this resource","exceptionName":"com.atlassian.bitbucket.AuthorisationException"}]}


I had also tried to run from a Powershell script:

$token = <<mytoken>>

$permissionsData = @{
permission = $permission
}

$permissionsJson = $permissionsData | ConvertTo-Json

$headers= @{
Authorization = "Bearer $token"

}


$url="https://xxxx:8443/rest/api/latest/projects/PROJ/repos/test/permissions/users?name={new_user_name}&permission={REPO_READ}"
$response = Invoke-WebRequest -Uri $url -Method PUT -Headers $headers -Body $permissionsJson -contentType "application/json"

However, this PowerShell script gives below output:

Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
At line:xx char:13
+ $response = Invoke-WebRequest -Uri $url -Method PUT -Headers $headers ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Looking for your help to address this.

1 answer

0 votes
Navpreet Ahuja
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 6, 2023

Hi

The `curl` request looks fine but it's missing the authorization header, hence the error.

You can pass the HTTP access token in that curl request

e.g.

```

curl --request PUT -H "Authorization: Bearer {HTTP-access-token}" --url "https://xxxx:8443/rest/api/latest/projects/PROJ/repos/test/permissions/users?name={new_user_name}&permission={REPO_READ}"

```

 

If it still fails, make sure the token is not expired or has the correct permissions

parajaganesh November 6, 2023

Thanks for your inputs. It's working now. 

I have also tried it from PowerShell using below which is working:

$url = "https://xxxxx:8443/rest/api/latest/projects/PROJ/repos/REPO/permissions/users?name={username}&permission={permission}"
$response = Invoke-WebRequest -Uri $url -Method PUT -Headers $headers -Body $permissionsJson -contentType "application/json"

Like Navpreet Ahuja likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events