Skip to content

Commit 10cfade

Browse files
author
Encore Shao
committed
feat: fixing bug for refresh token
1 parent 6941075 commit 10cfade

11 files changed

Lines changed: 66 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
- get profile `client.contacts`
2424
- get contacts data with next link `client.contacts({next_link: 'xxx'})`
2525

26-
## [0.1.4] - (2022-10-26)
26+
## [0.1.5] - (2022-10-27)
2727

2828
- Generate URLs for token and able to refresh token
2929
- get authorize URL `client.authorize_url`

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
ruby-office365 (0.1.4)
4+
ruby-office365 (0.1.5)
55
faraday
66
faraday_middleware
77

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ irb(main):018:0> response[:results][0].as_json
135135
**Refresh User Token**
136136

137137
```ruby
138-
irb(main):005:0> client.refresh_token!
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_...."
139145
```
140146

141147
## Copyright

bin/console

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ require "office365"
88
# with your gem easier. You can also use a different console, if you like.
99

1010
# (If you use this, don't forget to add pry to your Gemfile!)
11-
# require "pry"
12-
# Pry.start
11+
require "pry"
12+
Pry.start
1313

14-
require "irb"
15-
IRB.start(__FILE__)
14+
# require "irb"
15+
# IRB.start(__FILE__)

lib/office365.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
require "office365/models"
99

1010
module Office365
11+
class Error < StandardError; end
12+
class InvalidAuthenticationTokenError < StandardError; end
13+
class InvaliRequestError < StandardError; end
14+
1115
API_HOST = "https://graph.microsoft.com"
1216
API_VERSION = "v1.0"
1317

1418
LOGIN_HOST = "https://login.microsoftonline.com"
15-
SCOPE = "User.read Calendars.read Mail.ReadBasic Contacts.Read"
16-
17-
class Error < StandardError; end
18-
class InvalidAuthenticationTokenError < StandardError; end
19+
SCOPE = "offline_access User.read Calendars.read Mail.ReadBasic Contacts.Read"
1920
# Your code goes here...
2021
end

lib/office365/models.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module Models
1010
autoload :Owner, "office365/models/owner"
1111
autoload :EmailAddress, "office365/models/email_address"
1212
autoload :Contact, "office365/models/contact"
13+
autoload :AccessToken, "office365/models/access_token"
1314
end
1415
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Office365
4+
module Models
5+
class AccessToken < Base
6+
# attr_accessor :token_type, :scope, :expires_in, :ext_expires_in, :access_token, :refresh_token
7+
end
8+
end
9+
end

lib/office365/rest/request.rb

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,41 @@ def initialize(access_token, debug: false)
1616
end
1717

1818
def get(uri, args: {})
19-
r_uri = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
19+
req_url = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
2020

21-
response = Faraday.new(url: [r_uri.scheme, "://", r_uri.hostname].join, headers: headers) do |faraday|
21+
response = Faraday.new(url: [req_url.scheme, "://", req_url.hostname].join, headers: headers) do |faraday|
2222
faraday.adapter Faraday.default_adapter
2323
faraday.response :json
2424
faraday.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
25-
end.get(r_uri.request_uri, *args)
25+
end.get(req_url.request_uri, *args)
2626

27-
resp_body = response.body
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)
3228
end
3329

3430
def post(uri, args)
35-
response = Faraday.new(url: Office365::API_HOST, headers: headers) do |faraday|
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|
3634
faraday.adapter Faraday.default_adapter
3735
faraday.response :json
3836
faraday.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
39-
end.post(uri, args)
37+
end.post(req_url.request_uri, args.to_query)
38+
39+
parse_respond(response)
40+
end
4041

42+
private
43+
44+
def parse_respond(response)
4145
resp_body = response.body
46+
4247
return resp_body if response.status == 200
4348
raise InvalidAuthenticationTokenError, resp_body.dig("error", "message") if response.status == 401
49+
raise InvaliRequestError, resp_body["error_description"] if response.status == 400
4450

4551
raise Error, resp_body["error"]
4652
end
4753

48-
private
49-
5054
def headers
5155
{
5256
"Content-Type" => "application/json",
@@ -57,6 +61,12 @@ def headers
5761
def dev_developement?
5862
debug
5963
end
64+
65+
def post_headers
66+
{
67+
"Content-Type" => "application/x-www-form-urlencoded"
68+
}
69+
end
6070
end
6171
end
6272
end

lib/office365/rest/token.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ def token_url
2323
end
2424

2525
def refresh_token!
26-
post(token_url, {
27-
refresh_token: refresh_token,
28-
client_id: client_id,
29-
client_secret: client_secret,
30-
grant_type: "refresh_token",
31-
scope: Office365::SCOPE
32-
})
26+
Models::AccessToken.new(
27+
Request.new(nil, debug: debug).post(token_url, token_params)
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+
}
3340
end
3441
end
3542
end

lib/office365/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Office365
4-
VERSION = "0.1.4"
4+
VERSION = "0.1.5"
55
end

0 commit comments

Comments
 (0)