OAuth2 API Authentication

Hello,

if there are any Python experts: I need to build a OAuth2 API but I cant make it work. I also cant find any example headers and bodys for the API request anywhere on the internet. If someone could do an example of an script or has done something similar with curl or in a other programming language I would be very thankful.

@Luidie
what framework are you using? flask or something else. Who is your oauth provider? or are you tying to build you own?

This is more suitable for stackoverflow

Hello, I am building on my own. I got a Python script with the requests library.

If you are building your own provider you can use django.
I would advise against building your own provider if you don’t know what you are doing and if its going to be used in a production environment.

https://django-oauth-toolkit.readthedocs.io/en/latest/

and just to clarify you aren’t talking about orchestrator api right?

I want to add queue items to an orchestrator queue and need to authenticate

ok. @Luidie

i have this old format

This is an example code i have removed senstive data for all required paramters look at the swagger definition

import requests
import json


auth_url = "orchurl"
auth_data = {
    "tenancyName" : "<your_tenant_name>",
    "usernameOrEmailAddress" : "<your_username>",
    "password" : "<your_password>"
}
auth_headers = {
    'Content-Type': 'application/json'
}
auth_response = requests.post(auth_url, data=json.dumps(auth_data), headers=auth_headers)
access_token = auth_response.json()['result']


queue_url = "url"
queue_data = {
    "itemData": {
        "Name": "<queue_name>",
        "Priority": "Normal",
        "SpecificContent": {
            "<key>": "<value>"
        }
    }
}
queue_headers = {
    'Authorization': f'Bearer {access_token}',
    'Content-Type': 'application/json'
}
queue_response = requests.post(queue_url, data=json.dumps(queue_data), headers=queue_headers)

queue url preceded by

https://cloud.uipath.com/{organizationName}/{tenantName}/orchestrator_/odata/
image

at the end of your ochestrator url add swagger/index.html for all the api definitions and required paramters

Thank you very much! I also got the same but the token I get is not working. Now I am trying to use the OAuth2 method.

I always get the error: “queuename does not exist!”. I got everything right with folderID and so on. If I use the bearer from swagger it works. So it works with the swagger OAuth2 bearer but with my username/passwort authentication I cant find the queue.

are you using azure active directory.

If your organization uses the Azure Active Directory model, you must register external applications in Orchestrator and use the OAuth flow.

try the below sample snippet see if that works to fetch id and secret you would have to register your script as an external application

import requests


client_id = 'your_client_id'
client_secret = 'your_client_secret'


base_url = 'provder/api'


token_url = f'{base_url}token'


token_data = {
    'grant_type': 'client_credentials',
    'client_id': client_id,
    'client_secret': client_secret,
}

response = requests.post(token_url, data=token_data)

response_json = response.json()
access_token = response_json['access_token']


headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(f'{base_url}some_endpoint', headers=headers)
print(response.json())