Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not get signed in email using Office 365 REST API

I followed this post http://dev.office.com/code-samples-detail/2142 and Ruby to get user's email address. Here is the code:

# Parses an ID token and returns the user's email
def get_email_from_id_token(id_token)

  # JWT is in three parts, separated by a '.'
  token_parts = id_token.split('.')
  # Token content is in the second part
  encoded_token = token_parts[1]

  # It's base64, but may not be padded
  # Fix padding so Base64 module can decode
  leftovers = token_parts[1].length.modulo(4)
  if leftovers == 2
    encoded_token += '=='
  elsif leftovers == 3
    encoded_token += '='
  end

  # Base64 decode (urlsafe version)
  decoded_token = Base64.urlsafe_decode64(encoded_token)

  # Load into a JSON object
  jwt = JSON.parse(decoded_token)

  # Email is in the 'preferred_username' field
  email = jwt['preferred_username']
end

This function worked very well, I can get user's email address. But today, this function still works without error but the JSON I got not contain user's email address anymore.
Could someone help me? I want to get user's email address. Thank you !

like image 636
Hom nom nom nom ... Avatar asked Feb 24 '16 14:02

Hom nom nom nom ...


People also ask

How do I enable REST API in Outlook?

There's just one simple process for Outlook.com and Office 365: register and get dynamic user authorization to access users' mail, calendar, and contacts. Use a Microsoft account or Microsoft 365 subscription account to register your app. It takes only a few steps to identify your app for Outlook.com and Office 365.

Is office365 API free?

You can obtain a free developer account via the Office 365 Developer Program. This program includes a free 12-month subscription to Office 365 for use in development and testing.

Is Outlook API free?

As I know, Outlook calendar API has been integrated into the Microsoft graph API and it's free.


2 Answers

Azure deployed a breaking change to the v2 app model, and you don't get user info by default anymore.

You can read all about it here: https://azure.microsoft.com/en-us/documentation/articles/active-directory-v2-preview-oidc-changes/, but to summarize:

  • The openid scope used to give you basic profile info for the user.
  • That wasn't in line with the OpenID standard
  • Azure changed to require that you request the profile scope to get access to that information

For that sample, find this bit:

# Scopes required by the app
SCOPES = [ 'openid',
           'https://outlook.office.com/mail.read' ]

And change it to:

# Scopes required by the app
SCOPES = [ 'openid',
           'profile',
           'https://outlook.office.com/mail.read' ]
like image 117
Jason Johnston Avatar answered Sep 21 '22 21:09

Jason Johnston


Please add profile and email in your scope :

SCOPES = [ 'openid', 'profile', 'email', 'https://outlook.office.com/mail.read' ]

like image 25
Antima Gupta Avatar answered Sep 23 '22 21:09

Antima Gupta