Interactive Device Authentication with Azure Python Notebook Service

Aaron (Ari) Bornstein
Microsoft Azure
Published in
3 min readNov 12, 2018

--

This post walks through how to authenticate Python Azure SDKs within the Azure Notebook Service.

Why Interactive Device Authentication?

As a Senior Cloud Developer Advocate, I constantly find myself using the Azure Notebook Service. When writing content for new Azure users I often have to explain how to track down service credentials like the Subscription Id and tenant from the Azure portal.

Tracking down Subscription Id in the portal

This process requires users to open a new tab and interrupts the instruction flow, which can distract from the new cool features that I’m trying to teach such as the Azure Data Explorer or Machine Learning Service.

Interactive Device Authentication enables the use of all the Azure services without the need to navigate to the Azure portal. In the following steps I will walk through how to do this for your own notebooks.

Tutorial

Prerequisites

  • Microsoft Azure Account — If you are reading this you already probably have a trial but if not, you can activate a trial here.

Step 1: Log in to Azure Notebooks

Azure notebooks is a free collaborative coding service provided by Microsoft to help you get started experimenting with code. Login to Azure Notebooks with your Microsoft account to get started.

Then create a new notebook.

Note: Make sure when handling Azure credentials make sure the library you create is private!

Step 2: Authentication Code

Copy and paste the following the authentication code a new cell and run it.

!az login
!az ad sp create-for-rbac --sdk-auth > mycredentials.json
import os, jsonwith open('mycredentials.json') as data_file:
azure_credentials = json.load(data_file)
# delete credentials
file os.remove("mycredentials.json")

Step 3: Interactive Authentication

Click on the microsoft.com/devicelogin link and copy the code from the cell see the example below as reference.

Enter the code:

Chose your subscription and close the tab.

You should see the message below:

Close the tab and return to your notebook.

Step 4: You are all set

Your credentials are here in the azure_credential object now you can use Azure Management services in python without ever having to leave the notebook context again.

Call To Action

  • Use this method to make your Azure notebooks more clear and self-contained and reduce dependence on the Azure portal.

If you have any questions, comments, or topics you would like me to discuss feel free to follow me on Twitter.

About the Author
Aaron (Ari) Bornstein is an avid AI enthusiast with a passion for history, engaging with new technologies and computational medicine. As an Open Source Engineer at Microsoft’s Cloud Developer Advocacy team, he collaborates with Israeli Hi-Tech Community, to solve real world problems with game changing technologies that are then documented, open sourced, and shared with the rest of the world.

--

--

Aaron (Ari) Bornstein
Microsoft Azure

<Microsoft Open Source Engineer> I am an AI enthusiast with a passion for engaging with new technologies, history, and computational medicine.