Only show these results:

Authenticate users with the Nylas Python SDK

The Nylas REST API uses server-side OAuth, and the bindings included in the Python SDK provide convenient ways to simplify the OAuth process. The following describes the OAuth process:

  • You redirect the user to Nylas login page, along with your App ID and secret.
  • Your user logs in.
  • They are redirected to a callback URL of your own, along with an access code.
  • You use this access code to get an authorization token to the API.

For more information about OAuth and other user authentication methods, see our Authentication docs.

What you'll learn

In this tutorial, you'll learn how to do the following tasks:

  • Redirect users to Nylas.
  • Handle authentication responses.

Prerequisites

Before you start, follow our Get Started with the Python SDK guide.

Step 1: Redirect users to Nylas

The Nylas API provides granular authentication scopes to improve security for your end users.

Pass a comma-separated string of scopes to the scopes argument for theauthentication_url method. See supported Authentication scopes in Nylas API v2 for a full list of scopes and details.

from flask import Flask, session, request, redirect, Response
from nylas import APIClient

@app.route('/')
def index():
redirect_url = "http://0.0.0.0:8888/login_callback"
client = APIClient(APP_ID, APP_SECRET)
return redirect(client.authentication_url(redirect_url, scopes='email.read_only,email.send'))

Step 2: Handle the authentication response

@app.route('/login_callback')
def login_callback():
if 'error' in request.args:
return "Login error: {0}".format(request.args['error'])


client = APIClient(APP_ID, APP_SECRET)
code = request.args.get('code')
session['access_token'] = client.token_for_code(code)

You can use an OAuth library to simplify the authentication flow, such as Python Social Auth or Flask-Dance. See the Nylas Python SDK example project that demonstrates how to implement OAuth.