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.
Before we jump in, make sure you've got these basics covered:
Oh, and don't forget to install these gems:
gem install oauth2 httparty json
First things first, let's get you authenticated:
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')
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'
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
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
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))
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
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! 🚀