Forums

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

403 exception happened when using Rest API

Ken Wu _Miansheng_ June 14, 2023

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

3 answers

1 accepted

0 votes
Answer accepted
Ken Wu _Miansheng_ June 22, 2023

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" and then page can be created successfully.

Thank you for all your help.

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 16, 2023

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.  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

Ken Wu _Miansheng_ June 16, 2023

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

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 20, 2023

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.

Ken Wu _Miansheng_ June 20, 2023

OK, thank you Andy for your help.

Thanks,

Ken

0 votes
Ken Wu _Miansheng_ June 14, 2023

I tried to use 'curl' instead, but the result is the same.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events