All you need to know to pull Data from an API using Python Requests

API Documentation

Authorization

filename = ####path to the name of your pickle file#######
print(filename)
filehandler = open(filename, ‘rb’)
refresh_token = pickle.load(filehandler)url = “https://login.microsoftonline.com/common/oauth2/v2.0/token" payload = “””client_id%0A=##########& redirect_uri=##################& client_secret=##################& code={}& refresh_token={}& grant_type=refresh_token”””.format(code,refresh_token)
headers = { ‘Content-Type’: “application/x-www-form-urlencoded”, ‘cache-control’: “no-cache” } response = requests.request(“POST”, url, data=payload, headers=headers)
r = response.json()
access_token = r.get(‘access_token’)
refresh_token = r.get(‘refresh_token’) with open(filename, ‘wb’) as f:
pickle.dump(refresh_token, f)

Calling the API and Cleaning and parsing the JSON

import json
import requestsurl = ‘https://graph.microsoft.com/v1.0/me/messages
$search=
"subject:###SUBJECT YOU WANT TO SEARCH FOR###”’headers = {
‘Authorization’: ‘Bearer ‘ +access_token,
‘Content-Type’: ‘application/json; charset=utf-8’
}r = requests.get(url, headers=headers)files = r.json()print(files)
emails = []
while True:
try:
url = files[‘@odata.nextLink’]
for item in range (0,len(files[‘value’])):
emails.append(files['value'][item]['Subject'],[files[‘value’] [item][‘bodyPreview’])
r = requests.get(url, headers=headers)
print(r.text)
files = r.json()
except:
break

Putting the Data Somewhere

import csv write_file =‘###LOCATION WHERE YOU WANT TO PUT THE CSV FILE###’
with open(write_file,’w’, newline = ‘’) as f:
writer = csv.writer(f, lineterminator = ‘\n’)
writer.writerows(emails)

Automate

  1. Using Crontab on an Ubuntu Server
0 1 * * * /full path to python environment/python /full path to file/example.py

--

--

--

If your Product gives great value, you will live immortal !

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Starting your Multi-Cloud journey with Terraform (Part 1)

GCP Service Account + HashiCorp Vault

Download/Upload a File/Directory from AWS EC2 Instance to Local Machine.

Working with sObjects, SOQL, and SOSL

An intro to computer programming

Create an Agile Game based on Game of Thrones — Tutorial

Using 3D printers in schools — from uncomfortable beginnings to Karmen

The Momentum Counter Trading Strategy.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akash M Dubey

Akash M Dubey

If your Product gives great value, you will live immortal !

More from Medium

Moving from tech to business analysis

Photo by <a href=”https://unsplash.com/@firmbee?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Firmbee.com</a> on <a href=”https://unsplash.com/s/photos/analysis?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

Good Product Data, Bad Product Data

Unlocking Mobile-First Data Intelligence

A Student at Heart: Introducing, Sales Representative, Jacob Hoskins