Microsoft Graph Ruby on Rails Connect Sample
Use Microsoft Graph to access a user’s Microsoft account resources from within a Ruby on Rails web application. This sample uses REST calls directly to the Microsoft Graph endpoint to work with user resources--in this case, to send an email as the user.
The sample uses OmniAuth middleware to authenticate against the Azure AD v2.0 endpoint. The Azure AD v2.0 endpoint enables developers to write a single code flow that handles authentication for both users' work or school (Azure Active Directory) or personal (Microsoft) accounts, including Office 365, Outlook.com, and OneDrive accounts.
The sample also uses the Office Fabric UI for styling and formatting the user experience.
To use this sample, the following are required:
- Ruby 2.1 to run the sample on a development server.
- Rails framework (the sample has been tested on Rails 4.2).
- Bundler dependency manager.
- Rack web server interface for Ruby.
- A Microsoft account or an Office 365 for business account
Register the application
Register an app on the Microsoft App Registration Portal. This generates the app ID and password that you'll use to configure the app for authentication.
Sign into the Microsoft App Registration Portal using either your personal or work or school account.
Choose Add an app.
Enter a name for the app, and choose Create application.
The registration page displays, listing the properties of your app.
Copy the application ID. This is the unique identifier for your app.
Under Application Secrets, choose Generate New Password. Copy the app secret from the New password generated dialog.
You'll use the application ID and app secret to configure the app.
Under Platforms, choose Add platform > Web.
Make sure the Allow Implicit Flow check box is selected, and enter http://localhost:3000/auth/microsoft_v2_auth/callback as the Redirect URI.
The Allow Implicit Flow option enables the OpenID Connect hybrid flow. During authentication, this enables the app to receive both sign-in info (the id_token) and artifacts (in this case, an authorization code) that the app uses to obtain an access token.
The redirect URI http://localhost:3000/auth/microsoft_v2_auth/callback is the value that the OmniAuth middleware is configured to use once it has processed the authentication request.
Build and run the sample
- Download or clone the sample and open it in the editor of your choice.
If you don't already have bundler and rack, you can install them with the following command.
gem install bundler rack
In the config/environment.rb file do the following.
- Replace ENTER_YOUR_CLIENT_ID with the app ID of your registered application.
- Replace ENTER_YOUR_SECRET with the app secret for your registered application.
Install the Rails application and dependencies with the following command.
To start the Rails application, type the following command.
rackup -p 3000
- Go to
http://localhost:3000in your web browser.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
Questions and comments
We'd love to get your feedback about the Microsoft Graph Ruby on Rails Connect sample. You can send your questions and suggestions to us in the Issues section of this repository.
Your feedback is important to us. Connect with us on Stack Overflow. Tag your questions with [MicrosoftGraph].
Copyright (c) 2016 Microsoft. All rights reserved.