Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified the usage example, because it could NOT execute. #47

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,47 @@ The recommended library for authenticating against AAD is [ADAL](https://github.
### Usage example

```ruby
require 'adal'
require 'httpclient'
require 'microsoft_graph'
require 'json'
require 'nokogiri'
require 'net/http'

# authenticate using ADAL
username = '[email protected]'
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'

client = HTTPClient.new
response = client.post(
"#{MS_BASE_URL}/#{tenant_id}/#{TOKEN_REQUEST_PATH}",
{
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']

# 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
)

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
```

Expand Down