Hello,
I am currently trying to add a webhook to a repository using the Bitbucket API, but I am encountering the following error message:
"Access denied. You must have write or admin access."
I am using an OAuth2 token with the following scopes: ['repository:admin', 'repository:write', 'webhook'].
Despite having these scopes, I am still unable to add the webhook. Here is a brief overview of my implementation:
Token Retrieval and Webhook Creation
async getAccessToken(code: string): Promise<Record<string, any>> { const params = { code, client_id: process.env.BITBUCKET_CLIENT_ID, client_secret: process.env.BITBUCKET_CLIENT_SECRET, grant_type: 'authorization_code', scope: ['repository:admin', 'repository:write', 'webhook'], redirect_uri: redirect_url }; const response = await axios.post( 'https://bitbucket.org/site/oauth2/access_token', qs.stringify(params), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } ); return { accessToken: response.data.access_token, refreshToken: response.data.refresh_token }; async createWebhook( repoSlug: string, webhookUrl: string, accessToken: string ): Promise { const webhookPayload = { description: 'Webhook for pull request created event', url: webhookUrl, active: true, events: ['repo:push', 'repo:updated'] }; const url = `${BB_API_BASE_URL}/repositories/${repoSlug}/hooks`; return firstValueFrom( this.httpService.post(url, webhookPayload, { headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json' } }) ); }
Despite this, I receive the "Access denied" error when attempting to add the webhook.
I have ensured the following:
- The OAuth2 token includes the required scopes.
- The repository slug is correct.
- The repository is not archived or subject to additional restrictions.
Could you please help me understand why this error is occurring? Is there any additional permission or configuration required that I may have missed?