Splunk AppDynamics

Python Code For Data Extraction using OAuth API Client

Vaibhav_Vir_Sin
Communicator

Team,

With recent release (4.5) we see "API Client" through which we can pull metric data via POST API.

Even though we have documentation available at https://docs.appdynamics.com/display/PRO45/API+Clients

But I have not been able to execute it successully, so thought to check with you.

In below code, we have API Client named 'test21' and Account i am taking by default that is customer1.

Appreciate your help. 

geturl = 'https://'+ControllerPort+'/controller/api/oauth/access_token'
client_secret = '<<Secret_Copied>>'
data = {'grant_type': 'client_credentials','Content-Type': 'application/vnd.appd.cntrl+protobuf;v=1'}
access_token_response = requests.post(geturl, data=data, verify=False, allow_redirects=False, auth=('test21@customer1', client_secret))
print(access_token_response.text)

Labels (3)
0 Karma
1 Solution

Vaibhav_Vir_Sin
Communicator

Just realized this thread i raised did not have any solution marked.

I accomplished so sharing for broader community. 

replace 'token' with big token string without quotes in the headers.

################

import requests

headers = {

    'Authorization': 'Bearer token',

}

# Sample Query to fetch all applications

response = requests.get('https://<<CONTROLLERHOST>>:8181/controller/rest/applications', headers=headers, verify=False)

print(response.text) # OR response.content

################

Either you use Token directly like above, OR you can get token generated from secret of API Client like below: 

curl -k -X POST -H "Content-Type: application/vnd.appd.cntrl+protobuf;v=1" "https://<<CONTROLLER:8181/controller/api/oauth/access_token " -d “grant_type=client_credentials&client_id=XYZ@customer1&client_secret=4c45*****801”

 

Regards,

VVS

View solution in original post

Vaibhav_Vir_Sin
Communicator

Any Updates or Response?

0 Karma

Umervali_Niyama
Path Finder

Hi Vaibhav 

I am able to run this succcessfully. See the below command for your referance. 

curl -H "Authorization:Bearer Authkey "https://controller.appdynamics.com/controller/rest/applications"

Vaibhav_Vir_Sin
Communicator
Hi Umerveli,
Thanks for confirming the same.
Could you please share your python code snippet?...
0 Karma

Umervali_Niyama
Path Finder

Hi vaibhav

I did not develop any python code...if you can share your full code i can test with my controller and get back to you.

cheers

Umer

0 Karma

Vaibhav_Vir_Sin
Communicator
I had already provided you with above. (Original mail)
You said you were able to extract, so was curious.
Even curl is not working for me which you mentioned.please elaborate that part of you can.

Appreciate it.
0 Karma

Umervali_Niyama
Path Finder

Do you have time for quick webex?

0 Karma

Vaibhav_Vir_Sin
Communicator
I am not sure if I can share it on WebEx.

Others,
Any help on above?
0 Karma

Umervali_Niyama
Path Finder

Ok lets take one step back.

the command I tested is curl -H "Authorization:Bearer Authkey "https://controller.appdynamics.com/controller/rest/applications"

 

so when you create a API client from Adminstartion tab then make sure you use access token not the client secret

Under the roles I tried with Admin not sure which role you are trying but I am not saying that it should be an admin 

 

 

 

0 Karma

Vaibhav_Vir_Sin
Communicator

I tried above it is working (no issue with my setup)

My query is how to use secreta nd create token and use that for subsequent multiple REST API calls.

Regards,

Vaibhav

0 Karma

Vaibhav_Vir_Sin
Communicator
Any thoughts and help in this?
0 Karma

Vaibhav_Vir_Sin
Communicator

Just realized this thread i raised did not have any solution marked.

I accomplished so sharing for broader community. 

replace 'token' with big token string without quotes in the headers.

################

import requests

headers = {

    'Authorization': 'Bearer token',

}

# Sample Query to fetch all applications

response = requests.get('https://<<CONTROLLERHOST>>:8181/controller/rest/applications', headers=headers, verify=False)

print(response.text) # OR response.content

################

Either you use Token directly like above, OR you can get token generated from secret of API Client like below: 

curl -k -X POST -H "Content-Type: application/vnd.appd.cntrl+protobuf;v=1" "https://<<CONTROLLER:8181/controller/api/oauth/access_token " -d “grant_type=client_credentials&client_id=XYZ@customer1&client_secret=4c45*****801”

 

Regards,

VVS

keshav_Swami
Engager

Hi 

To generate temporary access token using python, you can use request library of python .

  generate_token_url="https://<Controller URL>/controller/api/oauth/access_token"
  payload = {
    "grant_type":"client_credentials",
    "client_id":"Client_Id@ControllerName",
    "client_secret":"fe************************rf"
  }
  token_headers = { "Content-Type": "application/vnd.appd.cntrl+protobuf;v=1" }
  generate_token_response = requests.request("POST", generate_token_url, headers=token_headers, data=payload).json()
print(generate_token_response["access_token"])
 
you just need to change client_id and client_secret in payload

iamryan
Community Manager
Community Manager

Hi, @keshav.Swami 

Thanks for sharing that information. This is a peer-to-peer community, so helping others is key to the community's success

0 Karma
Get Updates on the Splunk Community!

3-2-1 Go! How Fast Can You Debug Microservices with Observability Cloud?

3-2-1 Go! How Fast Can You Debug Microservices with Observability Cloud? Learn how unique features like ...

Leverage Cisco Talos Threat Intelligence Across Splunk Security Products

Leverage Cisco Talos Threat Intelligence Across Splunk Security Products   Security Edition   Have you ...

What’s New in Splunk Enterprise 9.4: Tools for Digital Resilience

What’s New in Splunk Enterprise 9.4: Tools for Digital Resilience Tune in to What’s New in Splunk Enterprise ...