Class: BaiduNetDisk::Auth
- Inherits:
-
Object
- Object
- BaiduNetDisk::Auth
- Defined in:
- lib/baidu-netdisk/auth.rb
Class Method Summary collapse
-
.get_auth_code(redirect_uri = 'oob') ⇒ Object
This is a debug tool to get auth code for your own Baidu account.
- .get_token(auth_code, redirect_uri = 'oob') ⇒ Object
- .refresh_access_token(refresh_token) ⇒ Object
Class Method Details
.get_auth_code(redirect_uri = 'oob') ⇒ Object
This is a debug tool to get auth code for your own Baidu account
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/baidu-netdisk/auth.rb', line 7 def get_auth_code(redirect_uri = 'oob') check_required_configs if redirect_uri == 'oob' url = "https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=#{BaiduNetDisk.app_key}&redirect_uri=#{redirect_uri}&scope=basic,netdisk&device_id=#{BaiduNetDisk.app_id}" if redirect_uri == 'oob' system('open', url) else RestClient.get url end end |
.get_token(auth_code, redirect_uri = 'oob') ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/baidu-netdisk/auth.rb', line 19 def get_token(auth_code, redirect_uri = 'oob') response = RestClient.get "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code=#{auth_code}&client_id=#{BaiduNetDisk.app_key}&client_secret=#{BaiduNetDisk.secret_key}&redirect_uri=#{redirect_uri}" if redirect_uri == 'oob' response_body = JSON.parse response.body BaiduNetDisk.access_token, BaiduNetDisk.refresh_token = response_body.fetch_values('access_token', 'refresh_token') end end |
.refresh_access_token(refresh_token) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/baidu-netdisk/auth.rb', line 28 def refresh_access_token(refresh_token) response = RestClient.get "https://openapi.baidu.com/oauth/2.0/token?grant_type=refresh_token&refresh_token=#{refresh_token}&client_id=#{BaiduNetDisk.app_key}&client_secret=#{BaiduNetDisk.secret_key}" response_body = JSON.parse response.body access_token, refresh_token = response_body.fetch_values('access_token', 'refresh_token') if BaiduNetDisk.after_token_refreshed&.respond_to? :call BaiduNetDisk.after_token_refreshed.call(access_token, refresh_token) end [access_token, refresh_token] rescue RestClient::BadRequest $stdout.puts "Refresh token failed." raise BaiduNetDisk::Exception::RefreshTokenFailed end |