Skip to content

Instantly share code, notes, and snippets.

@AlainODea
Created July 23, 2020 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlainODea/4e56e924697e325cadbbb4f4036d2970 to your computer and use it in GitHub Desktop.
Save AlainODea/4e56e924697e325cadbbb4f4036d2970 to your computer and use it in GitHub Desktop.
Example of connecting to AlienVault API using Python Requests 2 and Requests-OAuthlib
import json
from requests.auth import HTTPBasicAuth
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
from getpass import getpass
domain = "alienvault.cloud"
subdomain = "example" # use your actual subdomain
def main():
client_id = input("Client ID: ")
client_secret = getpass("Secret: ")
auth = HTTPBasicAuth(client_id, client_secret)
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(
token_url=f"https://{subdomain}.{domain}/api/2.0/oauth/token",
auth=auth,
params={
'grant_type': 'client_credentials'
})
client = OAuth2Session(client_id, token=token)
r = client.get(f"https://{subdomain}.{domain}/api/2.0/alarms/",
params={
'page': 1,
'size': 20,
'suppressed': False,
'status': 'open'
})
jdata = r.json()
for item in jdata["_embedded"]["alarms"]:
print(json.dumps(item, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment