Configure ActiveResource to support OAuth2

I need to configure ActiveResource to connect to OAuth2 or basic connection-by-connection authentication. I found a couple of ways to configure ActiveResource using OAuth2, but they don't seem so elegant and defy dynamic type configuration. Any help there?

+6
source share
1 answer

I figured out how to do this if my ActiveResource classes inherit from an intermediate class:

class Resource < ActiveResource::Base end class MyClass < Resource end 

This allows you to dynamically set authentication (as well as the site, format, etc.) for all classes that inherit from the intermediate resource class:

if the user has configured OAuth2:

  Resource.headers['authorization'] = 'Bearer ' + my_oauth2_token 

or if the user just uses basic authentication:

  Resource.user = my_user_name Resource.password = my_password 

Hope this helps someone!

+5
source

Source: https://habr.com/ru/post/910444/


All Articles