Script to make an API call

🚧

Important

If you are a Druva CloudRanger user, see Getting started with CloudRanger APIs

Druva supports OAuth 2.0 based authentication for API requests. The following is the process to generate API credentials and create the Access Token to access the API requests. Sample scripts enable you to automate the process of creating the token and access Druva APIs.

Step 1: Generate API Credentials
API credentials can be created and managed from Druva Cloud Platform Console. Refer Create and Manage API Credentials to learn about API credentials management.

Sample API Credentials:

McNkxxxx4Vicxxxx4Ldpxxxx/09Uxxxx
Xmcxxxx8j5xxxx6NxxxxRbRxxxxNNyPt

Step 2 - Script to form a Base64 encoded API Credential
After you generate the Client ID and Secret Key, encode it to Base64 format.

import os
import base64
bString = base64.b64encode("<Client ID>:<Secret Key>")

Replace the and with the values generated in Step 1 earlier.

import os
import base64
bString = base64.b64encode("McNkxxxx4Vicxxxx4Ldpxxxx/09Uxxxx:Xmcxxxx8j5xxxx6NxxxxRbRxxxxNNyPt")
QXlxxxxxxKQytkOE5VS0adsfdU4SXR5M1lkRxxxxxxxTzAydGtENWpkNGw0NGZwbmxlMVNqxxxxxTktsSkU=

Step 3: Use the Base64 encoded API Credential to generate an Access Token

Use appropriate URLs in the requests.
inSync Cloud - Use https://apis.druva.com
inSync GovCloud - Use https://govcloudapis.druva.com

import os
import requests
tokenurl = 'https://apis.druva.com/token'
headers = {'Authorization': 'Basic ' + 'QXlxxxxxxKQytkOE5VS0adsfdU4SXR5M1lkRxxxxxxxTzAydGtENWpkNGw0NGZwbmxlMVNqxxxxxTktsSkU=}
data = {'grant_type': 'client_credentials', 'scope': 'read'}
response = requests.post(tokenurl,headers=headers,data=data)
respStatus_Code = response.status_code
if(respStatus_Code == 200):
responseJson = response.json()
bearer_token = responseJson.get() //access token
expires_in = responseJson.get('expires_in')
OTM3NDgxxxxxxxmNmEyZjlhZGVDNjM2MzoxNToxNTYwNDAzOTYxxxxxxxxZy1UelF6ZS1aQ3pBMzJTNE9XY3c9PQ==:c75gjnnKsXvesGt/axxxxxxxxDJdk8SOvCB35iqc=

Step 4: Use the Access Token to access Druva APIs.

import requests
url = 'https://apis.druva.com'
api_insync_events = '/insync/eventmanagement/v2/events'
bearer_token='MOTM3NDgxxxxxxxmNmEyZjlhZGVDNjM2MzoxNToxNTYwNDAzOTYxxxxxxxxZy1UelF6ZS1aQ3pBMzJTNE9XY3c9PQ==:c75gjnnKsXvesGt/axxxxxxxxDJdk8SOvCB35iqc='
headers = {'Authorization': 'Bearer ' +bearer_token}}
response = requests.get(url+api_insync_events,headers=headers)
respStatus_Code = response.status_code
if(respStatus_Code == 200):
responseJson = response.json()
//get the values from the json here.
else if (respStatus_Code == 403):
//re-generate the access_token and proceed again
             else:
//handle other error scenarios accordingly