generated from nylas-samples/nylas-code-samples-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
quickstart-contacts-python.py
77 lines (67 loc) · 2.59 KB
/
quickstart-contacts-python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Import your dependencies
from dotenv import load_dotenv
import os
from nylas import Client
from flask import Flask, request, redirect, url_for, session, jsonify
from flask_session.__init__ import Session
from nylas.models.auth import URLForAuthenticationConfig
from nylas.models.auth import CodeExchangeRequest
# Load your env variables
load_dotenv()
# Create the app
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Initialize Nylas client
nylas = Client(
api_key = os.environ.get("NYLAS_API_KEY"),
api_uri = os.environ.get("NYLAS_API_URI"),
)
# Call the authorization page
@app.route("/oauth/exchange", methods=["GET"])
def authorized():
if session.get("grant_id") is None:
code = request.args.get("code")
exchangeRequest = CodeExchangeRequest({"redirect_uri": "http://localhost:5000/oauth/exchange",
"code": code,
"client_id": os.environ.get("NYLAS_CLIENT_ID")})
exchange = nylas.auth.exchange_code_for_token(exchangeRequest)
session["grant_id"] = exchange.grant_id
return redirect(url_for("login"))
# Main page
@app.route("/nylas/auth", methods=["GET"])
def login():
if session.get("grant_id") is None:
config = URLForAuthenticationConfig({"client_id": os.environ.get("NYLAS_CLIENT_ID"),
"redirect_uri" : "http://localhost:5000/oauth/exchange"})
url = nylas.auth.url_for_oauth2(config)
return redirect(url)
else:
return f'{session["grant_id"]}'
@app.route("/nylas/list-contacts", methods=["GET"])
def list_contacts():
try:
query_params = {"limit": 5}
contacts = nylas.contacts.list(session["grant_id"], query_params=query_params).data
return jsonify(contacts)
except Exception as e:
return f'{e}'
@app.route("/nylas/create-contact", methods=["GET"])
def create_contact():
request_body = {
"emails": [{"email": "[email protected]", "type": "work"}],
"company_name": "Nylas",
"given_name": "Nylas' Swag",
"notes": "This is good swag",
"web_pages": [{"url": "https://www.nylas.com", "type": "work"}]
}
try:
event = nylas.contacts.create(session["grant_id"],
request_body = request_body)
return jsonify(event)
except Exception as e:
return f'{e}'
# Run our application
if __name__ == "__main__":
app.run()