I was unable to list Confluence users through OAuth 2.0 App

Mathangi Phani Babu
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 24, 2024

I have been create an OAuth 2.0 App in Developer Console and It is working fine with JIRA Apis and not working for Confluence to "List users" Getting the below error 

 

Failed to retrieve users: 401 {"code":401,"message":"Unauthorized; scope does not match"}

Here is the flask function

 

 

from flask import Flask, redirect, request, url_for, session
import requests
import os

app = Flask(__name__)
app.secret_key = os.urandom(24)

# Replace these with your actual values
CLIENT_ID = '*********************************************'
CLIENT_SECRET = '******************************************'

SCOPES = 'read:confluence-user read:confluence-groups'

@app.route('/')
def home():
    return 'Welcome to the Confluence User List App! <a href="/login">Login with Atlassian</a>'

@app.route('/login')
def login():
    auth_url = (f'{AUTH_URL}?audience=api.atlassian.com&client_id={CLIENT_ID}&'
                f'scope={SCOPES}&redirect_uri={REDIRECT_URI}&state=random_state&'
                f'response_type=code&prompt=consent')
    return redirect(auth_url)

@app.route('/callback')
def callback():
    code = request.args.get('code')
    state = request.args.get('state')

    token_response = requests.post(
        TOKEN_URL,
        json={
            'grant_type': 'authorization_code',
            'client_id': CLIENT_ID,
            'client_secret': CLIENT_SECRET,
            'code': code,
            'redirect_uri': REDIRECT_URI
        },
        headers={'Content-Type': 'application/json'}
    )

    token_response_data = token_response.json()
    access_token = token_response_data.get('access_token')
    session['access_token'] = access_token

    return redirect(url_for('list_users'))

@app.route('/list_users')
def list_users():
    access_token = session.get('access_token')
    if not access_token:
        return redirect(url_for('login'))

    # Get the cloud ID for the Confluence instance
    cloud_id_response = requests.get(
        f'{API_URL}/oauth/token/accessible-resources',
        headers={'Authorization': f'Bearer {access_token}'}
    )

    cloud_id_response_data = cloud_id_response.json()
    if not cloud_id_response_data:
        return 'No accessible resources found.'

    cloud_id = cloud_id_response_data[0]['id']

    # Get the members of the 'confluence-users' group
    group_name = 'confluence-users'
    users_response = requests.get(
        f'{API_URL}/ex/confluence/{cloud_id}/wiki/rest/api/group/{group_name}/member',
        headers={'Authorization': f'Bearer {access_token}'}
    )

    if users_response.status_code == 200:
        users = users_response.json()
        user_list = '<br>'.join([f"{user['displayName']} ({user['username']})" for user in users['results']])
        return f'<h1>Confluence Users:</h1><p>{user_list}</p>'
    else:
        return f'Failed to retrieve users: {users_response.status_code} {users_response.text}'

if __name__ == '__main__':
    app.run(debug=True)

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events