OAuth2 token 401Unauthorized
Asked by Adam Eldda:

Hi, Everybody :).  
I tried to connect to confluence cloud using OAuth2:  
I send a post request to <https://auth.atlassian.com/oauth/token> and succeed to get the access_token :  
This is a POST request body:  
{  
"grant_type": "authorization_code",  
"client_id": "xxxxx",  
"client_secret": "xxxxx",  
"code":"xxxx",  
"redirect_uri":"xxxx"  
}  
The problem when I used to send GET Request and I add the Bearer {token} for example <https://MY_API.atlassian.net/wiki/rest/api/content/>  
using postman  
The response is :  
\<!DOCTYPE html\>  
\<html lang="en"\>

\<head\>  
\<meta charset="utf-8"\>  
\<meta name="description" content="Encountered a 401 - Unauthorized error while loading this page."\>  
\<meta name="viewport" content="width=device-width, initial-scale=1"\>  
\<title\>Atlassian Cloud Notifications - Page access unauthorized\</title\>  
\<link rel="stylesheet" href="/_edge/resources/aui-prototyping.css"\>  
\<style\>  
body {  
background: #fff;  
}

main {  
margin: auto;  
padding-top: 120px;  
text-align: center;  
}

.suggestion {  
font-size: 16px;  
color: rgb(94, 108, 132);  
width: 350px;  
padding-top: 20px;  
margin: auto;  
}  
\</style\>  
\</head\>

\<body\>  
\<main\>  
\<img  
src="/_edge/resources/images/error-generic.svg"  
alt="error icon"  
width="120"  
/\>  
\<h2\>  
There was a problem accessing this content  
\</h2\>  
\<p class="suggestion"\>  
Encountered a 401 - Unauthorized error while loading this page.  
\</p\>  
\</main\>  
\</body\>

\</html\>  
I need your help :)  
Thanks :).


Answers:
author: Shannon S
body: Hi Adam,

Good afternoon, and welcome to Atlassian Community. It's nice to have you join us.

Can you help clarify what you're looking to do exactly? Are you developing an app for Atlassian Cloud? To use OAuth, you need to enable it for your app in your developer console. See [OAuth 2.0 (3LO) apps](https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps/) and [Security Overview](https://developer.atlassian.com/cloud/confluence/security-overview/). If you still have problems authenticating your application, reach out to your developer forum: [Atlassian Developer Community](https://community.developer.atlassian.com/).

If you're looking to access the Cloud API, then you can use the API token. See [Basic auth for REST APIs.](https://developer.atlassian.com/cloud/confluence/basic-auth-for-rest-apis/)

* [Manage API tokens for your Atlassian Account](https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/)

I hope this helps! Let me know if you have any trouble.

Take care,

Shannon

comments: author: Adam Eldda
body: **Thanks, Shannon for your reply and sorry for the delay.**   
**I know how the basic auth works and it works for me good.**   
**but I need to fetch the data using OAuth2.**

++***First Question:***++

<br />

I get the token using ++**Post Request**++ to ***<https://auth.atlassian.com/oauth/token>***

**++Body Request++**:  
***{***  
***"audience": "api.atlassian.com",***  
***"grant_type":"client_credentials",***  
***"client_id": "xxxx",***  
***"client_secret":"xxxx"***  
***}***  
and I get the token from the response.  
after that, I create HTTP Get for example to:  
***<https://my-app.atlassian.net/wiki/rest/api/space?limit=10>***  
***and I add the Authorization header Bearer + my token.***   

But The response status is 401 and the body  
\<!DOCTYPE html\>  
\<html lang="en"\>

\<head\>  
\<meta charset="utf-8"\>  
\<meta name="description" content="Encountered a 401 - Unauthorized error while loading this page."\>  
\<meta name="viewport" content="width=device-width, initial-scale=1"\>  
\<title\>Atlassian Cloud Notifications - Page access unauthorized\</title\>  
\<link rel="stylesheet" href="/_edge/resources/aui-prototyping.css"\>  
\<style\>  
body {  
background: #fff;  
}

main {  
margin: auto;  
padding-top: 120px;  
text-align: center;  
}

.suggestion {  
font-size: 16px;  
color: rgb(94, 108, 132);  
width: 350px;  
padding-top: 20px;  
margin: auto;  
}  
\</style\>  
\</head\>

\<body\>  
\<main\>  
\<img  
src="/_edge/resources/images/error-generic.svg"  
alt="error icon"  
width="120"  
/\>  
\<h2\>  
There was a problem accessing this content  
\</h2\>  
\<p class="suggestion"\>  
Encountered a 401 - Unauthorized error while loading this page.  
\</p\>  
\</main\>  
\</body\>

\</html\>  
++***I am using the same account for my confluence and developer console.***++  
++***Second Question:***++  
The other question is why I get "expires_in": 3600?  
how can I change it?  


author: Shannon S
body: Hi Adam,

Thank you for your reply. Unfortunately, I'm not too familiar with this, but I'll share some threads that might help you:

* [OAuth 2.0 tokens stop to work](https://community.atlassian.com/t5/Answers-Developer-Questions/OAuth-2-0-tokens-stop-to-work/qaq-p/570171)
* [OAuth2 3LO Unable to exchange code for access token](https://community.developer.atlassian.com/t/oauth2-3lo-unable-to-exchange-code-for-access-token/26980)
* [JIRA Rest API authentication always returns 401 unauthorized](https://community.atlassian.com/t5/Jira-questions/JIRA-Rest-API-authentication-always-returns-401-unauthorized/qaq-p/1110047?)

Because the Atlassian Community is more geared towards end-users, it's better to raise this question on the [Atlassian Developer Community](https://community.developer.atlassian.com/) instead. This is because they are more familiar with this feature and are better equipped to answer your question.

Best of luck, and take care!  
Shannon


Tags: cloud

confluence-cloud