The OpenGround API uses OAuth 2.0 authentication. We support both Client Credentials (Machine) and Authorization Code with PKCE (User Login) authentication. Please see here for more information.
We've included code examples below for both Authentication methods. These are simplified examples for illustrative purposes. You will need to assess suitability and modify as appropriate for your purposes.
To use these code examples, you will need additional parameters. Please see this article on how to request a Client ID & Secret. And this article on how to obtain your Region and Instance ID.
A Python User Login authentication example can be downloaded here. This example contains a proof of concept 'Python SDK' that includes an authentication example. It is purely for example/demonstration purposes, and as mentioned above may need to be modified for your purposes.
This example uses Machine authentication to obtain an access token. It then uses the access token to make an API call to load the projects within the cloud instance.
import requests
import json
# Update these values with your specific parameters
CLIENT_ID = ''
CLIENT_SECRET = ''
INSTANCE_ID = ''
REGION = ''
# Get an access token
token_url = 'https://imsoidc.bentley.com/connect/token'
scope = 'openground'
data = {
'grant_type': 'client_credentials',
'scope': scope,
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET
}
token_response = requests.post(token_url, data=data)
access_token = token_response.json()['access_token']
# Use token to make an API call to load projects within the cloud
url = 'https://api.' + REGION + '.openground.bentley.com/api/v1.0/data/projects'
headers = {
'OpenGroundCloud': 'U3VwZXJCYXRtYW5GYXN0',
'User-Agent': 'Python',
'Authorization': 'Bearer {}'.format(access_token),
'InstanceID': INSTANCE_ID
}
get_projects = requests.get(url, headers=headers)
# Print list of projects
projects = get_projects.json()
print(json.dumps(projects, indent=4, sort_keys=False))
Example output:
[
{
"Id": "00000000-0000-0000-0000-000000000001",
"Group": null,
"DataFields": [
{
"Header": "ProjectID",
"Value": "Project ABC"
},
{
"Header": "uui_ConfigPack",
"Value": "00000000-0000-0000-0000-00000000000a"
}
],
"HasDocuments": null
},
{
"Id": "00000000-0000-0000-0000-000000000002",
"Group": null,
"DataFields": [
{
"Header": "ProjectID",
"Value": "Project XYZ"
},
{
"Header": "uui_ConfigPack",
"Value": "00000000-0000-0000-0000-00000000000b"
}
],
"HasDocuments": null
}
]