Getting 401 response when calling Confluence cloud rest API search

Hi,

I’m trying to call the confluence rest api search function but facing some issues. I’m getting a 401 response even with the correct credentials.

Here’s the simple code I’m trying out:

curl --request GET
–url ‘https://corporate.atlassian.net/wiki/rest/api/search?cql=text~test
–user ‘corporate@email.com:GENERATED_TOKEN’
–header ‘Accept: application/json’

I tried with 2 tokens with difference scopes. The first token I created have these scopes

Read

  • read:account
  • read:confluence-content.all
  • read:confluence-content.permission
  • read:confluence-content.summary
  • read:confluence-groups
  • read:confluence-props
  • read:confluence-space.summary
  • read:confluence-user
  • read:me

Search

  • search:confluence

The 2nd token that I created have a more limited scope having only these

Read

  • read:content-details:confluence

Search

  • search:confluence

But still I’m getting a 401 even just by using the simplest sample code from the documentation of search endpoint - https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-search/#api-group-search

I can see that the tokens are being access as the page API Tokens shows when it was last accessed.

Not sure how to proceed and what else to check. Please advise.

Welcome to the Atlassian developer community @ArriolaSimon,

It looks like we updated the auth expectations without updating the documentation sufficiently. During the creation of a scoped API token, you would end up on this screen:

That hyperlink correctly points to a section of docs about how to construct a proper base URL for making the request. Confusingly, that section is in the middle of a page about OAuth; however, the specific instructions about the URL construction are correct.

More confusingly, nowhere else is that specified and the instructions for obtaining a CLOUD_ID for an OAuth token will not work for the Scoped API Token. For an API Token, you need to know how to retrieve the cloud id:

Putting it all together:

SITE_HOST='corporate.atlassian.net'
# Parsing the JSON with [jq](https://jqlang.org/)
CLOUD_ID=$(curl -s "https://$SITE_HOST/_edge/tenant_info" | jq -r '.cloudId')
curl -v \
    --request GET \
    --url "https://api.atlassian.com/ex/confluence/$CLOUD_ID/wiki/rest/api/search?cql=text~test" \
    --basic \
    --user "$EMAIL":"$SCOPED_API_TOKEN"

In case you want to narrow the scopes to only what you need, I confirmed above script works with a scoped-token that only had search:confluence scope.