Script to make an API call

🚧

Important

If you are a Druva CloudRanger user, see [Getting started with CloudRanger APIs] (https://developer.druva.com/docs/getting-started-with-cloudranger-api-trial)

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 requests
tokenurl = 'https://apis.druva.com/token'
headers = {'Authorization': 'Basic ' + 'QXlxxxxxxKQytkOE5VS0adsfdU4SXR5M1lkRxxxxxxxTzAydGtENWpkNGw0NGZwbmxlMVNqxxxxxTktsSkU='}
payload = {
            'grant_type': 'client_credentials',
            'scope': 'read'
        }
response = requests.post(tokenurl,headers=headers,data=payload)
respStatus_Code = response.status_code
if respStatus_Code == 200:
    responseJson = response.json()
    # Access token to be used
    bearer_token = responseJson.get('access_token')
    # Token expiry time in seconds
    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'
# Token that was generated in step 3
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:
    #handle other error scenarios accordingly
    pass