API call to enable repository access to a new user
Asked by parajaganesh:

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)](https://developer.atlassian.com/server/bitbucket/rest/v815/api-group-permission-management/#api-api-latest-projects-projectkey-repos-repositoryslug-permissions-users-put)"). 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](https://xxxx:8443/rest/api/latest/projects/PROJ/repos/test/permissions/users?name={new_user_name}&permission={REPO_READ)}"  

<br />

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"}\]}  

<br />

I had also tried to run from a Powershell script:  

$token = \<\<mytoken\>\>

$permissionsData = @{  
permission = $permission  
}

$permissionsJson = $permissionsData \| ConvertTo-Json

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

}

<br />

$url="[https://xxxx:8443/rest/api/latest/projects/PROJ/repos/test/permissions/users?name={new_user_name}\&permission={REPO_READ](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.


Answers:
author: Navpreet Ahuja
body: 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](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](https://confluence.atlassian.com/bitbucketserver/http-access-tokens-939515499.html) is not expired or has the correct permissions

comments: author: parajaganesh
body: 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](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"


Tags: bitbucket-server

server