Created
November 20, 2021 12:09
-
-
Save macmule/59b646bf824a6a361459e5cd6cf50516 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env python | |
| #################################################################################################### | |
| # | |
| # License: https://macmule.com/license/ | |
| # | |
| #################################################################################################### | |
| import json | |
| import logging | |
| import os | |
| import plistlib | |
| import requests | |
| import smtplib | |
| import sys | |
| from datetime import datetime | |
| from email.mime.text import MIMEText | |
| # Variables | |
| oauth2URL = 'https://app.box.com/api/oauth2/token' | |
| apiURL = 'https://api.box.com/2.0/' | |
| authorizationCode = '' | |
| clientId = '' | |
| clientSecret = '' | |
| apiFolder = '' | |
| logFileFullPath = os.path.join(apiFolder, os.path.basename(sys.argv[0]) + '.log') | |
| plistFileFullPath = os.path.join(apiFolder, 'Box-API.plist') | |
| mailServer = '' | |
| mailFrom = '' | |
| mailTo = '' | |
| # Get Script Name | |
| scriptName = os.path.basename(sys.argv[0]) | |
| # Configure Logging | |
| logging.basicConfig(filename=logFileFullPath,level=logging.DEBUG,format='%(asctime)s %(levelname)s %(message)s',filemode='w') | |
| # Send email | |
| def sendEmail(statusMessage): | |
| # Open the logFile as email contents | |
| logFile = open(logFileFullPath, 'rb') | |
| # Create a text/plain message | |
| msg = MIMEText(logFile.read()) | |
| logFile.close() | |
| # Email From, To & Subject | |
| msg['From'] = mailFrom | |
| msg['To'] = mailTo | |
| msg['Subject'] = scriptName + ": " + statusMessage | |
| # Send the email | |
| s = smtplib.SMTP(mailServer) | |
| s.sendmail(mailFrom, mailTo, msg.as_string()) | |
| s.quit() | |
| # Exit script | |
| sys.exit(0) | |
| # Generate OAuth2 tokens | |
| def generateTokens(): | |
| # Read this scripts plist | |
| try: | |
| # Read this scripts plist | |
| tokenPlist = plistlib.readPlist(plistFileFullPath) | |
| # Get the Refresh token from the plist | |
| refreshToken = tokenPlist["Refresh Token"] | |
| # If we can't find the plist | |
| except: | |
| # Try & generate new tokens | |
| try: | |
| # API call to generate tokens | |
| getTokens = requests.post(oauth2URL, data={'grant_type' : 'authorization_code','code' : authorizationCode, 'client_id' : clientId, 'client_secret' : clientSecret}) | |
| # If the above gives a 4XX or 5XX error | |
| getTokens.raise_for_status() | |
| # Get the JSON from the above | |
| newTokens = getTokens.json() | |
| # Get the new access token, valid for 60 minutes | |
| accessToken = newTokens['access_token'] | |
| # Log the access token we're using | |
| logging.info('Generated Access Token: %s' % accessToken) | |
| # Get the refresh token, valid for 60 days | |
| refreshToken = newTokens['refresh_token'] | |
| # Log the new refresh token we've generated | |
| logging.info('Generated Refresh Token: %s' % refreshToken) | |
| # Update plist with new refresh token & time generated, refresh token used for subsequent runs | |
| plistlib.writePlist({'Refresh Token':refreshToken,'Time Generated': datetime.now().isoformat(),}, plistFileFullPath) | |
| # Update tokenPlist variable | |
| tokenPlist = plistlib.readPlist(plistFileFullPath) | |
| # If we cannot generate the tokens | |
| except requests.exceptions.RequestException, e: | |
| # Status message to use as subject for sendMail funtion | |
| statusMessage = 'Cannot generate tokens %s' % e | |
| # Advise that no devices are to be deleted | |
| logging.error('-------- ' + statusMessage + ' --------') | |
| # Email & exit | |
| sendEmail(statusMessage) | |
| # If we cannot create the plist | |
| except: | |
| # Status message to use as subject for sendMail funtion | |
| statusMessage = 'Cannot create plist' | |
| # Advise that no devices are to be deleted | |
| logging.error('-------- ' + statusMessage + ' --------') | |
| # Email & exit | |
| sendEmail(statusMessage) | |
| # Try to make the API call | |
| try: | |
| # Log the token we're using & starting call | |
| logging.info('Using Refresh Token: %s' % refreshToken) | |
| # Get new access & refresh tokens | |
| getTokens = requests.post(oauth2URL, data={'grant_type' : 'refresh_token','refresh_token' : refreshToken, 'client_id' : clientId, 'client_secret' : clientSecret}) | |
| # If the above gives a 4XX or 5XX error | |
| getTokens.raise_for_status() | |
| # Get the JSON from the above | |
| newTokens = getTokens.json() | |
| # Get the new access token, valid for 60 minutes | |
| accessToken = newTokens['access_token'] | |
| # Log the access token we're using | |
| logging.info('Generated Access Token: %s' % accessToken) | |
| # Get the refresh token, valid for 60 days | |
| refreshToken = newTokens['refresh_token'] | |
| # Log the new refresh token we've generated | |
| logging.info('Generated Refresh Token: %s' % refreshToken) | |
| # Update plist with new refresh token & time generated, refresh token used for subsequent runs | |
| plistlib.writePlist({'Refresh Token':refreshToken,'Time Generated': datetime.now().isoformat(),}, plistFileFullPath) | |
| # Email & exit | |
| sendEmail('Successfully Generated Tokens') | |
| # If the API call fails, report error as e | |
| except requests.exceptions.RequestException, e: | |
| # Status message to use as subject for sendMail funtion | |
| statusMessage = 'Get request failed with %s' % e | |
| # Advise that no devices are to be deleted | |
| logging.error('-------- ' + statusMessage + ' --------') | |
| # Email & exit | |
| sendEmail(statusMessage) | |
| # Run functions in this order | |
| if __name__ == '__main__': | |
| # Generate the API Tokens | |
| generateTokens() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment