Back

Step by Step Guide to Building a Microsoft Intune API Integration in Ruby

Aug 8, 20245 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Microsoft Intune API integration with Ruby? You're in for a treat. This guide will walk you through the process of building a robust integration that'll have you managing devices and apps like a pro in no time.

Prerequisites

Before we jump in, make sure you've got these basics covered:

  • A Ruby environment (you've got this, right?)
  • A Microsoft Azure account with an Intune subscription
  • Your favorite code editor

Oh, and don't forget to install these gems:

gem install oauth2 httparty json

Authentication: Your Golden Ticket

First things first, let's get you authenticated:

  1. Head over to the Azure portal and register your application.
  2. Grab your client ID and secret - guard these with your life!
  3. Time to implement OAuth 2.0. Here's a quick snippet to get you started:
require 'oauth2' client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET, site: 'https://login.microsoftonline.com', token_url: '/common/oauth2/v2.0/token', authorize_url: '/common/oauth2/v2.0/authorize' ) token = client.client_credentials.get_token(scope: 'https://graph.microsoft.com/.default')

Setting Up Your Ruby Project

Let's keep it simple:

intune_integration/
├── lib/
│   └── intune_api.rb
├── Gemfile
└── main.rb

Your Gemfile should look something like this:

source 'https://rubygems.org' gem 'oauth2' gem 'httparty' gem 'json'

Making API Requests: The Fun Part

Now, let's create a basic IntuneAPI class:

require 'httparty' class IntuneAPI include HTTParty base_uri 'https://graph.microsoft.com/v1.0' def initialize(token) @options = { headers: { 'Authorization' => "Bearer #{token}" } } end def get_devices self.class.get('/deviceManagement/managedDevices', @options) end def create_app(app_data) self.class.post('/deviceAppManagement/mobileApps', @options.merge(body: app_data.to_json)) end end

Parsing and Handling Responses

Let's add some error handling and response parsing:

def handle_response(response) case response.code when 200..299 JSON.parse(response.body) else raise "API error: #{response.code} - #{response.message}" end end

Implementing Key Intune API Endpoints

Now you're ready to rock! Here are some examples of what you can do:

intune = IntuneAPI.new(token.token) # Get all devices devices = intune.handle_response(intune.get_devices) # Create a new app app_data = { '@odata.type' => '#microsoft.graph.windowsStoreApp', 'displayName' => 'My Awesome App' } new_app = intune.handle_response(intune.create_app(app_data))

Best Practices: Don't Be That Guy

  • Respect rate limits. Nobody likes a spammer.
  • Cache responses when possible. Your API (and your users) will thank you.
  • Implement retry logic for transient errors. Because sometimes, things just happen.

Testing Your Integration

Don't forget to test! Here's a simple RSpec example to get you started:

RSpec.describe IntuneAPI do it 'fetches devices successfully' do intune = IntuneAPI.new('fake_token') expect(intune.get_devices.code).to eq(200) end end

Conclusion

And there you have it! You're now equipped to build a killer Microsoft Intune API integration in Ruby. Remember, the official Microsoft Graph API docs are your best friend for diving deeper into what's possible.

Now go forth and manage those devices like a boss! 🚀