Module: Pinterest::Endpoints::Authentication
- Included in:
- Client
- Defined in:
- lib/pinterest/endpoints/authentication.rb
Overview
Authentication related endpoints.
Instance Method Summary collapse
-
#authorization_state ⇒ String
Returns a state string needed for authorization by the Pinterest API.
-
#authorization_url(callback_url = nil, scopes = nil) ⇒ String
Returns the URL to start the authentication flow.
-
#fetch_access_token(authorization_token) ⇒ String
Fetches the access token.
-
#verify_access_token ⇒ Hash
Verifies the access token.
Instance Method Details
#authorization_state ⇒ String
Returns a state string needed for authorization by the Pinterest API.
14 15 16 |
# File 'lib/pinterest/endpoints/authentication.rb', line 14 def @state ||= SecureRandom.hex end |
#authorization_url(callback_url = nil, scopes = nil) ⇒ String
Returns the URL to start the authentication flow.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pinterest/endpoints/authentication.rb', line 23 def (callback_url = nil, scopes = nil) ensure_param(client_id, "You must specify the client_id.") ensure_param(callback_url, "You must specify the callback_url.") validate_callback_url(callback_url) # Create the query query = cleanup_params({ response_type: "code", client_id: client_id.to_s, authorization_state: , redirect_uri: callback_url, scope: (ensure_array(scopes, ::Pinterest::Client::SCOPES) & ::Pinterest::Client::SCOPES).join(",") # Restrict to only valid scopes }) # Create the URL url = Addressable::URI.parse(::Pinterest::Client::API_URL + "/oauth") url.query_values = query url.to_s end |
#fetch_access_token(authorization_token) ⇒ String
Fetches the access token.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pinterest/endpoints/authentication.rb', line 44 def fetch_access_token() ensure_param(client_id, "You must specify the client_id.") ensure_param(client_secret, "You must specify the client_secret.") ensure_param(, "You must specify the authorization_token.") # Create parameters query = cleanup_params({ client_id: client_id, client_secret: client_secret, grant_type: "authorization_code", code: }) # Perform the request and then get the token response = perform_network_request(method: :post, url: versioned_url("/oauth/token"), query: query) @access_token = response.body["access_token"] end |
#verify_access_token ⇒ Hash
Verifies the access token.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pinterest/endpoints/authentication.rb', line 63 def verify_access_token ensure_param(client_id, "You must specify the client_id.") ensure_param(access_token, "You must set the access token first.") # Get the data data = perform_network_request(url: versioned_url("/oauth/inspect"), authenticated: true).body["data"] # Prepare for output create_authentication(data) end |