Class: InstagramBasicDisplay::Auth
- Inherits:
-
Object
- Object
- InstagramBasicDisplay::Auth
- Defined in:
- lib/instagram_basic_display/auth.rb
Overview
A module to handle authentication requests to the Instagram API. Allows you to retrieve short- and long-lived tokens, and refresh long-lived tokens.
Does not handle retrieving access codes. These must be retrieved by implementing Instagram’s
authentication window.
https://developers.facebook.com/docs/instagram-basic-display-api/overview#authentication-window
Instance Method Summary collapse
-
#initialize(configuration) ⇒ Object
constructor
Constructor.
-
#long_lived_token(short_lived_token: nil, access_code: nil) ⇒ InstagramBasicDisplay::Response
Exchanges either an access code OR a short-lived token for a long-lived token.
-
#refresh_long_lived_token(token:) ⇒ InstagramBasicDisplay::Response
Refreshes a long-lived token for a new period of validity.
-
#short_lived_token(access_code:) ⇒ InstagramBasicDisplay::Response
Exchanges an access code for a short-lived access token.
Constructor Details
#initialize(configuration) ⇒ Object
Constructor
35 36 37 |
# File 'lib/instagram_basic_display/auth.rb', line 35 def initialize(configuration) @configuration = configuration end |
Instance Method Details
#long_lived_token(short_lived_token: nil, access_code: nil) ⇒ InstagramBasicDisplay::Response
Exchanges either an access code OR a short-lived token for a long-lived token.
If an access code is passed, it will first be exchanged for a short-lived token.
Once that is achieved, the short-lived token will be exchanged for a long-lived token.
Refer to Instagram's documentation for more information on tokens:
https://developers.facebook.com/docs/instagram-basic-display-api/overview
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/instagram_basic_display/auth.rb', line 67 def long_lived_token(short_lived_token: nil, access_code: nil) short_lived_token ||= short_lived_token(access_code: access_code).payload.access_token uri = URI('https://graph.instagram.com/access_token') params = { client_secret: configuration.client_secret, grant_type: 'ig_exchange_token', access_token: short_lived_token } uri.query = URI.encode_www_form(params) response = Net::HTTP.get_response(uri) InstagramBasicDisplay::Response.new(response) end |
#refresh_long_lived_token(token:) ⇒ InstagramBasicDisplay::Response
Refreshes a long-lived token for a new period of validity.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/instagram_basic_display/auth.rb', line 85 def refresh_long_lived_token(token:) uri = URI('https://graph.instagram.com/refresh_access_token') params = { grant_type: 'ig_refresh_token', access_token: token } uri.query = URI.encode_www_form(params) response = Net::HTTP.get_response(uri) InstagramBasicDisplay::Response.new(response) end |
#short_lived_token(access_code:) ⇒ InstagramBasicDisplay::Response
Exchanges an access code for a short-lived access token. A short-lived token is valid for one hour, but can be exchanged for a long-lived token. Refer to Instagram’s documentation. developers.facebook.com/docs/instagram-basic-display-api/overview#short-lived-access-tokens
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/instagram_basic_display/auth.rb', line 47 def short_lived_token(access_code:) response = Net::HTTP.post_form( URI('https://api.instagram.com/oauth/access_token'), client_id: configuration.client_id, client_secret: configuration.client_secret, grant_type: 'authorization_code', redirect_uri: configuration.redirect_uri, code: access_code ) InstagramBasicDisplay::Response.new(response) end |