From d88631f95ca65bb10b0bda3b52100c82f8404fbb Mon Sep 17 00:00:00 2001 From: jinroq <2787780+jinroq@users.noreply.github.com> Date: Sun, 30 Jun 2019 23:28:05 +0900 Subject: [PATCH 1/2] Modified usage example, because it could NOT execute. --- README.md | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3fef5f0cf0..5e3f7984c2 100644 --- a/README.md +++ b/README.md @@ -28,27 +28,36 @@ The recommended library for authenticating against AAD is [ADAL](https://github. ### Usage example ```ruby -require 'adal' +require 'httparty' +require 'nokogiri' require 'microsoft_graph' -# authenticate using ADAL -username = 'admin@tenant.onmicrosoft.com' -password = 'xxxxxxxxxxxx' +MS_BASE_URL = "https://login.microsoftonline.com".freeze +TOKEN_REQUEST_PATH = "oauth2/v2.0/token".freeze + +# Your configuration client_id = 'xxxxx-xxxx-xxx-xxxxxx-xxxxxxx' client_secret = 'xxxXXXxxXXXxxxXXXxxXXXXXXXXxxxxxx=' -tenant = 'tenant.onmicrosoft.com' -user_cred = ADAL::UserCredential.new(username, password) -client_cred = ADAL::ClientCredential.new(client_id, client_secret) -context = ADAL::AuthenticationContext.new(ADAL::Authority::WORLD_WIDE_AUTHORITY, tenant) -resource = "https://graph.microsoft.com" -tokens = context.acquire_token_for_user(resource, client_cred, user_cred) +tenant_id = 'xxxxx-xxxx-xxx-xxxxxx-xxxxxxx' + +response = HTTParty.post( + "#{MS_BASE_URL}/#{tenant_id}/#{TOKEN_REQUEST_PATH}", + multipart: true, + body: { + client_id: client_id, + client_secret: client_secret, + scope: 'https://graph.microsoft.com/.default', + grant_type: 'client_credentials' + } +) +raise "#{response.message}" unless response.code == 200 +token = JSON.parse(response.body)['access_token'] # add the access token to the request header -callback = Proc.new { |r| r.headers["Authorization"] = "Bearer #{tokens.access_token}" } +callback = Proc.new { |r| r.headers["Authorization"] = "Bearer #{token}" } -graph = MicrosoftGraph.new(base_url: "https://graph.microsoft.com/v1.0", +graph = MicrosoftGraph.new(base_url: MicrosoftGraph::BASE_URL, cached_metadata_file: File.join(MicrosoftGraph::CACHED_METADATA_DIRECTORY, "metadata_v1.0.xml"), - api_version: '1.6', # Optional &callback ) From b26cf40ad001b98b34121d3a9e3761354dc31e65 Mon Sep 17 00:00:00 2001 From: jinroq <2787780+jinroq@users.noreply.github.com> Date: Wed, 10 Jul 2019 20:25:03 +0900 Subject: [PATCH 2/2] httparty -> httpclient --- README.md | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5e3f7984c2..9e6f8492eb 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,11 @@ The recommended library for authenticating against AAD is [ADAL](https://github. ### Usage example ```ruby -require 'httparty' -require 'nokogiri' +require 'httpclient' require 'microsoft_graph' +require 'json' +require 'nokogiri' +require 'net/http' MS_BASE_URL = "https://login.microsoftonline.com".freeze TOKEN_REQUEST_PATH = "oauth2/v2.0/token".freeze @@ -40,15 +42,18 @@ client_id = 'xxxxx-xxxx-xxx-xxxxxx-xxxxxxx' client_secret = 'xxxXXXxxXXXxxxXXXxxXXXXXXXXxxxxxx=' tenant_id = 'xxxxx-xxxx-xxx-xxxxxx-xxxxxxx' -response = HTTParty.post( +client = HTTPClient.new +response = client.post( "#{MS_BASE_URL}/#{tenant_id}/#{TOKEN_REQUEST_PATH}", - multipart: true, - body: { - client_id: client_id, - client_secret: client_secret, - scope: 'https://graph.microsoft.com/.default', - grant_type: 'client_credentials' - } + { + body: { client_id: client_id, + client_secret: client_secret, + scope: 'https://graph.microsoft.com/.default', + grant_type: 'client_credentials', + }, + 'Content-Type' => 'application/json', + multipart: true, + } ) raise "#{response.message}" unless response.code == 200 token = JSON.parse(response.body)['access_token'] @@ -61,11 +66,9 @@ graph = MicrosoftGraph.new(base_url: MicrosoftGraph::BASE_URL, &callback ) -me = graph.me # get the current user -puts "Hello, I am #{me.display_name}." - -me.direct_reports.each do |person| - puts "How's it going, #{person.display_name}?" +users = graph.users +users.each do |user| + puts "Hello, I am #{user.display_name}" end ```