Class: Square::OAuth::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/square/o_auth/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Square::OAuth::Client



7
8
9
# File 'lib/square/o_auth/client.rb', line 7

def initialize(client:)
  @client = client
end

Instance Method Details

#authorize(request_options: {}, **_params) ⇒ untyped

Returns:

  • (untyped)

Raises:

  • (error_class)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/square/o_auth/client.rb', line 129

def authorize(request_options: {}, **_params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "GET",
    path: "oauth2/authorize"
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  return if code.between?(200, 299)

  error_class = Square::Errors::ResponseError.subclass_for_code(code)
  raise error_class.new(_response.body, code: code)
end

#obtain_token(request_options: {}, **params) ⇒ Square::Types::ObtainTokenResponse

Returns an OAuth access token and refresh token using the ‘authorization_code` or `refresh_token` grant type.

When ‘grant_type` is `authorization_code`:

provide ‘code`, `client_id`, and `client_secret`.

provide ‘code`, `client_id`, and `code_verifier`.

When ‘grant_type` is `refresh_token`:

  • With the code flow, provide ‘refresh_token`, `client_id`, and `client_secret`.

The response returns the same refresh token provided in the request.

  • With the PKCE flow, provide ‘refresh_token` and `client_id`. The response returns

a new refresh token.

You can use the ‘scopes` parameter to limit the set of permissions authorized by the access token. You can use the `short_lived` parameter to create an access token that expires in 24 hours.

Important: OAuth tokens should be encrypted and stored on a secure server. Application clients should never interact directly with OAuth tokens.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/square/o_auth/client.rb', line 71

def obtain_token(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "oauth2/token",
    body: params
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::ObtainTokenResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#retrieve_token_status(request_options: {}, **_params) ⇒ Square::Types::RetrieveTokenStatusResponse

Returns information about an [OAuth access token](developer.squareup.com/docs/build-basics/access-tokens#get-an-oauth-access-token) or an application’s [personal access token](developer.squareup.com/docs/build-basics/access-tokens#get-a-personal-access-token).

Add the access token to the Authorization header of the request.

Important: The ‘Authorization` header you provide to this endpoint must have the following format:

“‘ Authorization: Bearer ACCESS_TOKEN “`

where ‘ACCESS_TOKEN` is a [valid production authorization credential](developer.squareup.com/docs/build-basics/access-tokens).

If the access token is expired or not a valid access token, the endpoint returns an ‘UNAUTHORIZED` error.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/square/o_auth/client.rb', line 108

def retrieve_token_status(request_options: {}, **_params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "oauth2/token/status"
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::RetrieveTokenStatusResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#revoke_token(request_options: {}, **params) ⇒ Square::Types::RevokeTokenResponse

Revokes an access token generated with the OAuth flow.

If an account has more than one OAuth access token for your application, this endpoint revokes all of them, regardless of which token you specify.

Important: The ‘Authorization` header for this endpoint must have the following format:

“‘ Authorization: Client APPLICATION_SECRET “`

Replace ‘APPLICATION_SECRET` with the application secret on the OAuth page for your application in the Developer Dashboard.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/square/o_auth/client.rb', line 27

def revoke_token(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "oauth2/revoke",
    body: params
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::RevokeTokenResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end