import requests import json def main(): cdp = input("Enter p for Production account or i for Internal account\n") if cdp == "i": base_url = "https://internal-apigw.central.arubanetworks.com" print("The URL used is {}".format(base_url)) elif cdp == "p": base_url = "https://app1-apigw.central.arubanetworks.com" print("The URL used is {}".format(base_url)) else: print("Wrong Input. p or i are valid inputs") customerid = input("\nFind your customer id, by clicking on top right corner on user icon and paste it here:\n") print("\nCreate an app on Central UI > Maintenance > API GW > Authorized Apps and note down Client ID and Client Secret\nPress Enter when noted.") clientid = input("\nEnter Client ID below:\n") clientsecret = input("\nEnter Client Secret below:\n") print("\nEnter your Central Credentials.\nNote: Your HPE or Aruba Creds will not work!") username = input("\nEnter Central Username below:\n") password = input("\nEnter Central Password below:\n") print("\nThanks. Please double check:\n Your Customer ID is:{0},\n App Client ID is:{1},\n App Client Secret is:{2}".format(customerid, clientid, clientsecret)) url1 = "/oauth2/authorize/central/api/login" url2 = "/oauth2/authorize/central/api" url3 = "/oauth2/token" authurl = base_url + url1 authcodeurl = base_url + url2 params = {"client_id": clientid} headers = {'Content-type': 'application/json'} payload = {"username": username, "password": password} print("\nContacting URL:", authurl) resp = requests.post(authurl, params=params, data=json.dumps(payload), headers=headers) csrf = resp.cookies['csrftoken'] ses = resp.cookies['session'] if resp.status_code != 200: print("Failure! The code is: {}".format(resp.status_code)) else: print("\nAwesome!\nCSRF cookie is: {0}\nSession cookie is {1}\nFetching auth code now...".format(csrf, ses)) sesk = "session=" + ses # headers2 = {'Content-type': 'application/json', 'X-CSRF-TOKEN': 'csrf', 'Cookie': '"session=ses"' } headers2 = {} headers2 = { 'X-CSRF-TOKEN': csrf, 'Content-type': 'application/json', 'Cookie': sesk } payload2 = {"customer_id": customerid} params2 = {"client_id": clientid, "response_type": "code", "scope": "all"} resp = requests.post(authcodeurl, params=params2, data=json.dumps(payload2), headers=headers2) authcode = resp.json()['auth_code'] print("\nMore Awesome!\nThe Auth Code is:{}\nSwapping it for Access Token...".format(authcode)) swapurl = base_url + url3 params3 = {"client_id": clientid, "grant_type": "authorization_code", "client_secret": clientsecret, "code": authcode} resp = requests.post(swapurl, params=params3) rtoken = resp.json()['refresh_token'] atoken = resp.json()['access_token'] print("\nCongrats!\nAccess Token is:{}\nRefresh Token is:{}\nThese are valid for 2 hours only!".format(atoken, rtoken)) return if __name__ == '__main__': main()