403 exception happened when using Rest API
Asked by Ken Wu _Miansheng_:

Hello experts,

I have received following error message when I am trying to use Rest API by powershell to create a new confluence page.

{"message":"Current user not permitted to use Confluence","statusCode":403}

But I can do it successfully before with the same codes, the last successful page created was on 02/18/2023. Following is the code:

#################################  
$pair = "miansheng.wu@commscope.com:generatedToken"  
$encodedCreds = \[System.Convert\]::ToBase64String(\[System.Text.Encoding\]::ASCII.GetBytes($pair))  
$auth = "Basic " + $encodedCreds;

$Headers = @{  
Authorization = $auth; 'Content-Type' = 'application/json'  
}

$uri = "<https://arrisworkassure.atlassian.net/wiki/rest/api/content>"

Invoke-WebRequest -Headers $Headers -Uri $uri -Method Post -Body '{"type":"page","title":"Test page","ancestors":\[{"id":3734306868}\], "space":{"key":"TEAM"},"body":{"storage":{"value":"Test body","representation":"storage"}}}'  
#################################

Could you please help me? This issue blocks me a lot now.

Thanks,  
Ken


Answers:
author: Ken Wu _Miansheng_
body: This issue is resolved. Because our Cloud Site at some point is changed from arrisworkassure.atlassian.net to cloud-cs.atlassian.net, change the $uri = "<https://arrisworkassure.atlassian.net/wiki/rest/api/content>" to be "[https://cloud-cs.atlassian.net/wiki/rest/api/content](https://arrisworkassure.atlassian.net/wiki/rest/api/content)" and then page can be created successfully.

Thank you for all your help.


author: Andy Heinzer
body: Hi Ken,

I think I see a problem with the way you are encoding credentials here. Please refer to our documentation in [Basic auth for REST APIs](https://developer.atlassian.com/cloud/confluence/basic-auth-for-rest-apis/). You code seems to be using this line:

```
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
```

When I think instead it should look like this:

```
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($pair))
```

Note the only difference here is we're using UTF8 instead of ASCII character encoding, but I have seen this difference trip up some other implementations before.

The error you are seeing here fits with this idea. When Confluence or Jira does not receive valid encoded credentials using basic auth, it can still attempt to make the request as if it was an anonymous user. In which case, anonymous users typically don't have access to this content and in turn the HTTP 403 error reflects this.

Try this and let me know the results.

Andy

comments: author: Ken Wu _Miansheng_
body: Hi, Andy,

Thanks so much for your reply.

I have tried 'UTF8' encoding but the result is the same.

Then I tried 'Unicode' and other encodings and they didn't work either.

Thanks,

Ken


author: Andy Heinzer
body: Hi Ken,

I see that you have a support case for this over in <https://getsupport.atlassian.com/servicedesk/customer/portal/48/PCS-187273>

I recommend working with our support team there to determine if this problem is specific to your cloud environment.


author: Ken Wu _Miansheng_
body: OK, thank you Andy for your help.

Thanks,

Ken


author: Ken Wu _Miansheng_
body: I tried to use 'curl' instead, but the result is the same.


Tags: cloud

confluence-cloud