Module: Pinterest::Endpoints::Authentication

Included in:
Client
Defined in:
lib/pinterest/endpoints/authentication.rb

Overview

Authentication related endpoints.

Instance Method Summary collapse

Instance Method Details

#authorization_stateString

Returns a state string needed for authorization by the Pinterest API.

Returns:

  • (String)

    The state.



14
15
16
# File 'lib/pinterest/endpoints/authentication.rb', line 14

def authorization_state
  @state ||= SecureRandom.hex
end

#authorization_url(callback_url = nil, scopes = nil) ⇒ String

Returns the URL to start the authentication flow.

Parameters:

  • callback_url (String) (defaults to: nil)

    The callback where to redirect the browser when done.

  • scopes (Array) (defaults to: nil)

    The list of scopes to ask for. For a list of valid fields, see Pinterest::Client::SCOPES.

Returns:

  • (String)

    The authorization URL.



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 authorization_url(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: 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.

Parameters:

  • authorization_token (String)

    The authorization token.

Returns:

  • (String)

    The authentication 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(authorization_token)
  ensure_param(client_id, "You must specify the client_id.")
  ensure_param(client_secret, "You must specify the client_secret.")
  ensure_param(authorization_token, "You must specify the authorization_token.")

  # Create parameters
  query = cleanup_params({
    client_id: client_id, client_secret: client_secret,
    grant_type: "authorization_code", code: authorization_token
  })

  # 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_tokenHash

Verifies the access token.

Returns:

  • (Hash)

    The access token informations.



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