Created
July 23, 2020 23:44
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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