ruby-office365 0.1.3 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +8 -1
- data/Gemfile.lock +1 -1
- data/README.md +12 -0
- data/lib/office365/models/access_token.rb +9 -0
- data/lib/office365/models.rb +1 -0
- data/lib/office365/rest/request.rb +22 -12
- data/lib/office365/rest/token.rb +27 -15
- data/lib/office365/version.rb +1 -1
- data/lib/office365.rb +6 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86ee47df4ac96c36024adb5c0595afa78889dc9659aebcdbdf01a59e407a0db9
|
4
|
+
data.tar.gz: 42ab9271c0d32b6035ad9caecc837137774bde57d0894fd49f34782d8c0f1f1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8dd5a3b6d974b91bf6c2846b1b9b3f433fcc1abe11d6dfc85f3fe3a7b023313db0079b6056a1e27d3d8a36506a434a4ef0e45f327f4d2dd25b0416f681ff5906
|
7
|
+
data.tar.gz: fc2997832814ec584184584546ca7443841914191056f5fd3c080017cf21b273a05f9dd5612b928ad7570fb3cbc202ff0cbaba01e86a62b250c409f1e87776b9
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -21,4 +21,11 @@
|
|
21
21
|
|
22
22
|
- Integrate REST API to get contacts
|
23
23
|
- get profile `client.contacts`
|
24
|
-
- get contacts data with next link `client.contacts({next_link: 'xxx'})`
|
24
|
+
- get contacts data with next link `client.contacts({next_link: 'xxx'})`
|
25
|
+
|
26
|
+
## [0.1.5] - (2022-10-27)
|
27
|
+
|
28
|
+
- Generate URLs for token and able to refresh token
|
29
|
+
- get authorize URL `client.authorize_url`
|
30
|
+
- get token URL `client.token_url`
|
31
|
+
- be able to refresh token `client.refresh_token!`
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -132,6 +132,18 @@ irb(main):018:0> response[:results][0].as_json
|
|
132
132
|
}
|
133
133
|
```
|
134
134
|
|
135
|
+
**Refresh User Token**
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
irb(main):005:0> response = client.refresh_token!
|
139
|
+
irb(main):005:0> response.scope
|
140
|
+
=> "openid User.Read profile email"
|
141
|
+
irb(main):005:0> response.access_token
|
142
|
+
=> "eyJ0eXAiOiJKV1QiLCJub25jZSI6ImFDYUladFJ6M3RSc3dFaktxUHdGbF9kVlFmbjJabG85Mjlkb2xaeFBhZm8iLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGxWMFRPSSIsImtpZCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGxWMFRPSSJ9..."
|
143
|
+
irb(main):005:0> response.refresh_token
|
144
|
+
=> "0.ARgA7EiQdLv1qECnFqPfrznKsT9ERYaGfG9Ki5WzQtEllj8YAJk.AgABAAEAAAD--DLA3VO7QrddgJg7WevrAgDs_wQA9P-Q1ODlBsrdZi-5s2mfLtEsavBgiEhGcz1KEf26fMrGFU3LM_og5l6wjSAtQ83XHLuje0_KYGol26_LGV_uH0F1MwCFR1N3ctwg4_...."
|
145
|
+
```
|
146
|
+
|
135
147
|
## Copyright
|
136
148
|
|
137
149
|
Copyright (c) 2022 Encore Shao. See LICENSE for details.
|
data/lib/office365/models.rb
CHANGED
@@ -16,37 +16,41 @@ module Office365
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def get(uri, args: {})
|
19
|
-
|
19
|
+
req_url = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
|
20
20
|
|
21
|
-
response = Faraday.new(url: [
|
21
|
+
response = Faraday.new(url: [req_url.scheme, "://", req_url.hostname].join, headers: headers) do |faraday|
|
22
22
|
faraday.adapter Faraday.default_adapter
|
23
23
|
faraday.response :json
|
24
24
|
faraday.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
|
25
|
-
end.get(
|
25
|
+
end.get(req_url.request_uri, *args)
|
26
26
|
|
27
|
-
|
28
|
-
return resp_body if response.status == 200
|
29
|
-
raise InvalidAuthenticationTokenError, resp_body.dig("error", "message") if response.status == 401
|
30
|
-
|
31
|
-
raise Error, resp_body["error"]
|
27
|
+
parse_respond(response)
|
32
28
|
end
|
33
29
|
|
34
30
|
def post(uri, args)
|
35
|
-
|
31
|
+
req_url = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
|
32
|
+
|
33
|
+
response = Faraday.new(url: [req_url.scheme, "://", req_url.hostname].join, headers: post_headers) do |faraday|
|
36
34
|
faraday.adapter Faraday.default_adapter
|
37
35
|
faraday.response :json
|
38
36
|
faraday.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
|
39
|
-
end.post(
|
37
|
+
end.post(req_url.request_uri, args.to_query)
|
38
|
+
|
39
|
+
parse_respond(response)
|
40
|
+
end
|
40
41
|
|
42
|
+
private
|
43
|
+
|
44
|
+
def parse_respond(response)
|
41
45
|
resp_body = response.body
|
46
|
+
|
42
47
|
return resp_body if response.status == 200
|
43
48
|
raise InvalidAuthenticationTokenError, resp_body.dig("error", "message") if response.status == 401
|
49
|
+
raise InvaliRequestError, resp_body["error_description"] if response.status == 400
|
44
50
|
|
45
51
|
raise Error, resp_body["error"]
|
46
52
|
end
|
47
53
|
|
48
|
-
private
|
49
|
-
|
50
54
|
def headers
|
51
55
|
{
|
52
56
|
"Content-Type" => "application/json",
|
@@ -57,6 +61,12 @@ module Office365
|
|
57
61
|
def dev_developement?
|
58
62
|
debug
|
59
63
|
end
|
64
|
+
|
65
|
+
def post_headers
|
66
|
+
{
|
67
|
+
"Content-Type" => "application/x-www-form-urlencoded"
|
68
|
+
}
|
69
|
+
end
|
60
70
|
end
|
61
71
|
end
|
62
72
|
end
|
data/lib/office365/rest/token.rb
CHANGED
@@ -3,29 +3,41 @@
|
|
3
3
|
module Office365
|
4
4
|
module REST
|
5
5
|
module Token
|
6
|
-
def
|
7
|
-
|
8
|
-
|
6
|
+
def authorize_url
|
7
|
+
base_uri = [LOGIN_HOST, "/#{tenant_id}/oauth2/v2.0/authorize"].join
|
8
|
+
azure_params = {
|
9
9
|
client_id: client_id,
|
10
10
|
client_secret: client_secret,
|
11
|
-
|
12
|
-
|
11
|
+
scope: Office365::SCOPE,
|
12
|
+
response_type: "code",
|
13
|
+
response_mode: "query",
|
14
|
+
redirect_uri: redirect_uri,
|
15
|
+
state: SecureRandom.hex
|
16
|
+
}.to_query
|
13
17
|
|
14
|
-
|
18
|
+
[base_uri, "?", azure_params].join
|
15
19
|
end
|
16
20
|
|
17
|
-
|
21
|
+
def token_url
|
22
|
+
[LOGIN_HOST, "/#{tenant_id}/oauth2/v2.0/token"].join
|
23
|
+
end
|
18
24
|
|
19
|
-
def
|
20
|
-
|
21
|
-
|
22
|
-
client_secret,
|
23
|
-
authorize_url: AUTHORIZE_URL,
|
24
|
-
site: LOGIN_HOST,
|
25
|
-
token_url: TOKEN_URL,
|
26
|
-
redirect_uri: redirect_uri
|
25
|
+
def refresh_token!
|
26
|
+
Models::AccessToken.new(
|
27
|
+
Request.new(nil, debug: debug).post(token_url, token_params)
|
27
28
|
)
|
28
29
|
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def token_params
|
34
|
+
{
|
35
|
+
refresh_token: refresh_token,
|
36
|
+
client_id: client_id,
|
37
|
+
client_secret: client_secret,
|
38
|
+
grant_type: "refresh_token"
|
39
|
+
}
|
40
|
+
end
|
29
41
|
end
|
30
42
|
end
|
31
43
|
end
|
data/lib/office365/version.rb
CHANGED
data/lib/office365.rb
CHANGED
@@ -8,15 +8,14 @@ require "office365/rest"
|
|
8
8
|
require "office365/models"
|
9
9
|
|
10
10
|
module Office365
|
11
|
-
|
12
|
-
|
11
|
+
class Error < StandardError; end
|
12
|
+
class InvalidAuthenticationTokenError < StandardError; end
|
13
|
+
class InvaliRequestError < StandardError; end
|
13
14
|
|
14
|
-
|
15
|
-
TOKEN_URL = "common/oauth2/token"
|
15
|
+
API_HOST = "https://graph.microsoft.com"
|
16
16
|
API_VERSION = "v1.0"
|
17
|
-
SCOPE = "User.read Calendars.read Mail.ReadBasic Contacts.Read"
|
18
17
|
|
19
|
-
|
20
|
-
|
18
|
+
LOGIN_HOST = "https://login.microsoftonline.com"
|
19
|
+
SCOPE = "offline_access User.read Calendars.read Mail.ReadBasic Contacts.Read"
|
21
20
|
# Your code goes here...
|
22
21
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-office365
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Encore Shao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -113,6 +113,7 @@ files:
|
|
113
113
|
- lib/office365.rb
|
114
114
|
- lib/office365/client.rb
|
115
115
|
- lib/office365/models.rb
|
116
|
+
- lib/office365/models/access_token.rb
|
116
117
|
- lib/office365/models/calendar.rb
|
117
118
|
- lib/office365/models/concerns/base.rb
|
118
119
|
- lib/office365/models/contact.rb
|