Class: Signet::OAuth1::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/signet/oauth_1/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Creates an OAuth 1.0 client.

Examples:

client = Signet::OAuth1::Client.new(
  :temporary_credential_uri =>
    'https://www.google.com/accounts/OAuthGetRequestToken',
  :authorization_uri =>
    'https://www.google.com/accounts/OAuthAuthorizeToken',
  :token_credential_uri =>
    'https://www.google.com/accounts/OAuthGetAccessToken',
  :client_credential_key => 'anonymous',
  :client_credential_secret => 'anonymous'
)

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the client.

    • :temporary_credential_uri - The OAuth temporary credentials URI.

    • :authorization_uri - The OAuth authorization URI.

    • :token_credential_uri - The OAuth token credentials URI.

    • :client_credential_key - The OAuth client credential key.

    • :client_credential_secret - The OAuth client credential secret.

    • :callback - The OAuth callback. Defaults to ‘oob’.



56
57
58
# File 'lib/signet/oauth_1/client.rb', line 56

def initialize(options={})
  self.update!(options)
end

Instance Method Details

#authorization_uri(options = {}) ⇒ Addressable::URI

Returns the authorization URI that the user should be redirected to.

Returns:

  • (Addressable::URI)

    The authorization URI.

See Also:



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/signet/oauth_1/client.rb', line 147

def authorization_uri(options={})
  options = options.merge(
    :temporary_credential_key => self.temporary_credential_key,
    :callback => self.callback
  )
  return nil if @authorization_uri == nil
  return Addressable::URI.parse(
    ::Signet::OAuth1.generate_authorization_uri(
      @authorization_uri, options
    )
  )
end

#authorization_uri=(new_authorization_uri) ⇒ Object

Sets the authorization URI for this client.

Parameters:

  • new_authorization_uri (Addressable::URI, String, #to_str)

    The authorization URI.



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/signet/oauth_1/client.rb', line 165

def authorization_uri=(new_authorization_uri)
  if new_authorization_uri != nil
    new_authorization_uri = Addressable::URI.send(
      new_authorization_uri.kind_of?(Hash) ? :new : :parse,
      new_authorization_uri
    )
    @authorization_uri = new_authorization_uri
  else
    @authorization_uri = nil
  end
end

#callbackString

Returns the callback for this client.

Returns:

  • (String)

    The OAuth callback.



508
509
510
# File 'lib/signet/oauth_1/client.rb', line 508

def callback
  return @callback || ::Signet::OAuth1::OUT_OF_BAND
end

#callback=(new_callback) ⇒ Object

Sets the callback for this client.

Parameters:

  • new_callback (String, #to_str)

    The OAuth callback.



517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/signet/oauth_1/client.rb', line 517

def callback=(new_callback)
  if new_callback != nil
    if !new_callback.respond_to?(:to_str)
      raise TypeError,
        "Can't convert #{new_callback.class} into String."
    end
    new_callback = new_callback.to_str
    @callback = new_callback
  else
    @callback = nil
  end
end

#client_credentialSignet::OAuth1::Credential Also known as: consumer_token

Returns the client credential for this client.

Returns:



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/signet/oauth_1/client.rb', line 211

def client_credential
  if self.client_credential_key && self.client_credential_secret
    return ::Signet::OAuth1::Credential.new(
      self.client_credential_key,
      self.client_credential_secret
    )
  elsif !self.client_credential_key && !self.client_credential_secret
    return nil
  else
    raise ArgumentError,
      "The client credential key and secret must be set."
  end
end

#client_credential=(new_client_credential) ⇒ Object Also known as: consumer_token=

Sets the client credential for this client.

Parameters:



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/signet/oauth_1/client.rb', line 231

def client_credential=(new_client_credential)
  if new_client_credential != nil
    if !new_client_credential.kind_of?(::Signet::OAuth1::Credential)
      raise TypeError,
        "Expected Signet::OAuth1::Credential, " +
        "got #{new_client_credential.class}."
    end
    @client_credential_key = new_client_credential.key
    @client_credential_secret = new_client_credential.secret
  else
    @client_credential_key = nil
    @client_credential_secret = nil
  end
end

#client_credential_keyString Also known as: consumer_key

Returns the client credential key for this client.

Returns:

  • (String)

    The client credential key.



251
252
253
# File 'lib/signet/oauth_1/client.rb', line 251

def client_credential_key
  return @client_credential_key
end

#client_credential_key=(new_client_credential_key) ⇒ Object Also known as: consumer_key=

Sets the client credential key for this client.

Parameters:

  • new_client_credential_key (String, #to_str)

    The client credential key.



261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/signet/oauth_1/client.rb', line 261

def client_credential_key=(new_client_credential_key)
  if new_client_credential_key != nil
    if !new_client_credential_key.respond_to?(:to_str)
      raise TypeError,
        "Can't convert #{new_client_credential_key.class} into String."
    end
    new_client_credential_key = new_client_credential_key.to_str
    @client_credential_key = new_client_credential_key
  else
    @client_credential_key = nil
  end
end

#client_credential_secretString Also known as: consumer_secret

Returns the client credential secret for this client.

Returns:

  • (String)

    The client credential secret.



279
280
281
# File 'lib/signet/oauth_1/client.rb', line 279

def client_credential_secret
  return @client_credential_secret
end

#client_credential_secret=(new_client_credential_secret) ⇒ Object Also known as: consumer_secret=

Sets the client credential secret for this client.

Parameters:

  • new_client_credential_secret (String, #to_str)

    The client credential secret.



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/signet/oauth_1/client.rb', line 289

def client_credential_secret=(new_client_credential_secret)
  if new_client_credential_secret != nil
    if !new_client_credential_secret.respond_to?(:to_str)
      raise TypeError,
        "Can't convert #{new_client_credential_secret.class} " +
        "into String."
    end
    new_client_credential_secret = new_client_credential_secret.to_str
    @client_credential_secret = new_client_credential_secret
  else
    @client_credential_secret = nil
  end
end

#fetch_protected_resource(options = {}) ⇒ Array

Transmits a request for a protected resource.

Examples:

# Using Net::HTTP
response = client.fetch_protected_resource(
  :uri => 'http://www.example.com/protected/resource'
)
# Using Typhoeus
response = client.fetch_protected_resource(
  :request => Typhoeus::Request.new(
    'http://www.example.com/protected/resource'
  ),
  :connection => connection
)

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the request.

    • :request - A pre-constructed request to sign.

    • :method - The HTTP method for the request. Defaults to :get.

    • :uri - The URI for the request.

    • :headers - The HTTP headers for the request.

    • :body - The HTTP body for the request.

    • :signature_method - The signature method. Defaults to 'HMAC-SHA1'.

    • :realm - The Authorization realm. See RFC 2617.

    • :connection - The HTTP connection to use. Must be of type Faraday::Connection.

Returns:

  • (Array)

    The response object.



1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
# File 'lib/signet/oauth_1/client.rb', line 1046

def fetch_protected_resource(options={})
  options[:connection] ||= Faraday.default_connection
  request = self.generate_authenticated_request(options)
  request_env = request.to_env(options[:connection])
  request_env[:request] ||= request
  response = options[:connection].app.call(request_env)
  if response.status.to_i == 401
    # When accessing a protected resource, we only want to raise an
    # error for 401 responses.
    message = 'Authorization failed.'
    if response.body.to_s.strip.length > 0
      message += "  Server message:\n#{response.body.to_s.strip}"
    end
    raise ::Signet::AuthorizationError.new(
      message, :request => request, :response => response
    )
  else
    return response
  end
end

#fetch_temporary_credential(options = {}) ⇒ Signet::OAuth1::Credential Also known as: fetch_request_token

Transmits a request for a temporary credential. This method does not have side-effects within the client.

Examples:

temporary_credential = client.fetch_temporary_credential(
  :additional_parameters => {
    :scope => 'https://mail.google.com/mail/feed/atom'
  }
)

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the request.

    • :signature_method - The signature method. Defaults to 'HMAC-SHA1'.

    • :additional_parameters - Non-standard additional parameters.

    • :realm - The Authorization realm. See RFC 2617.

    • :connection - The HTTP connection to use. Must be of type Faraday::Connection.

Returns:



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/signet/oauth_1/client.rb', line 667

def fetch_temporary_credential(options={})
  options[:connection] ||= Faraday.default_connection
  request = self.generate_temporary_credential_request(options)
  request_env = request.to_env(options[:connection])
  request_env[:request] ||= request
  response = options[:connection].app.call(request_env)
  if response.status.to_i == 200
    return ::Signet::OAuth1.parse_form_encoded_credentials(response.body)
  elsif [400, 401, 403].include?(response.status.to_i)
    message = 'Authorization failed.'
    if response.body.to_s.strip.length > 0
      message += "  Server message:\n#{response.body.to_s.strip}"
    end
    raise ::Signet::AuthorizationError.new(
      message, :request => request, :response => response
    )
  else
    message = "Unexpected status code: #{response.status}."
    if response.body.to_s.strip.length > 0
      message += "  Server message:\n#{response.body.to_s.strip}"
    end
    raise ::Signet::AuthorizationError.new(
      message, :request => request, :response => response
    )
  end
end

#fetch_temporary_credential!(options = {}) ⇒ Signet::OAuth1::Credential Also known as: fetch_request_token!

Transmits a request for a temporary credential. This method updates the client with the new temporary credential.

Examples:

client.fetch_temporary_credential!(:additional_parameters => {
  :scope => 'https://mail.google.com/mail/feed/atom'
})

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the request.

    • :signature_method - The signature method. Defaults to 'HMAC-SHA1'.

    • :additional_parameters - Non-standard additional parameters.

    • :realm - The Authorization realm. See RFC 2617.

    • :connection - The HTTP connection to use. Must be of type Faraday::Connection.

Returns:



720
721
722
723
# File 'lib/signet/oauth_1/client.rb', line 720

def fetch_temporary_credential!(options={})
  credential = self.fetch_temporary_credential(options)
  self.temporary_credential = credential
end

#fetch_token_credential(options = {}) ⇒ Signet::OAuth1::Credential Also known as: fetch_access_token

Transmits a request for a token credential. This method does not have side-effects within the client.

Examples:

token_credential = client.fetch_token_credential(
  :verifier => '12345'
)

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the request.

    • :verifier - The OAuth verifier provided by the server. Required.

    • :signature_method - The signature method. Defaults to 'HMAC-SHA1'.

    • :realm - The Authorization realm. See RFC 2617.

    • :connection - The HTTP connection to use. Must be of type Faraday::Connection.

Returns:



822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'lib/signet/oauth_1/client.rb', line 822

def fetch_token_credential(options={})
  options[:connection] ||= Faraday.default_connection
  request = self.generate_token_credential_request(options)
  request_env = request.to_env(options[:connection])
  request_env[:request] ||= request
  response = options[:connection].app.call(request_env)
  if response.status.to_i == 200
    return ::Signet::OAuth1.parse_form_encoded_credentials(response.body)
  elsif [400, 401, 403].include?(response.status.to_i)
    message = 'Authorization failed.'
    if response.body.to_s.strip.length > 0
      message += "  Server message:\n#{response.body.to_s.strip}"
    end
    raise ::Signet::AuthorizationError.new(
      message, :request => request, :response => response
    )
  else
    message = "Unexpected status code: #{response.status}."
    if response.body.to_s.strip.length > 0
      message += "  Server message:\n#{response.body.to_s.strip}"
    end
    raise ::Signet::AuthorizationError.new(
      message, :request => request, :response => response
    )
  end
end

#fetch_token_credential!(options = {}) ⇒ Signet::OAuth1::Credential Also known as: fetch_access_token!

Transmits a request for a token credential. This method updates the client with the new token credential.

Examples:

client.fetch_token_credential!(:verifier => '12345')

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the request.

    • :signature_method - The signature method. Defaults to 'HMAC-SHA1'.

    • :additional_parameters - Non-standard additional parameters.

    • :realm - The Authorization realm. See RFC 2617.

    • :connection - The HTTP connection to use. Must be of type Faraday::Connection.

Returns:



873
874
875
876
# File 'lib/signet/oauth_1/client.rb', line 873

def fetch_token_credential!(options={})
  credential = self.fetch_token_credential(options)
  self.token_credential = credential
end

#generate_authenticated_request(options = {}) ⇒ Array

Generates an authenticated request for protected resources.

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the request.

    • :request - A pre-constructed request to sign.

    • :method - The HTTP method for the request. Defaults to :get.

    • :uri - The URI for the request.

    • :headers - The HTTP headers for the request.

    • :body - The HTTP body for the request.

    • :signature_method - The signature method. Defaults to 'HMAC-SHA1'.

    • :realm - The Authorization realm. See RFC 2617.

Returns:

  • (Array)

    The request object.



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
# File 'lib/signet/oauth_1/client.rb', line 903

def generate_authenticated_request(options={})
  verifications = {
    :client_credential_key => 'Client credential key',
    :client_credential_secret => 'Client credential secret'
  }
  unless self.two_legged
    verifications.update(
      :token_credential_key => 'Token credential key',
      :token_credential_secret => 'Token credential secret'
    )
  end
  # Make sure all required state is set
  verifications.each do |(key, _value)|
    unless self.send(key)
      raise ArgumentError, "#{key} was not set."
    end
  end
  options = {
    :signature_method => 'HMAC-SHA1',
    :realm => nil,
    :connection => Faraday.default_connection
  }.merge(options)

  if options[:request].kind_of?(Faraday::Request)
    request = options[:request]
  else
    if options[:request].kind_of?(Array)
      method, uri, headers, body = options[:request]
    else
      method = options[:method] || :get
      uri = options[:uri]
      headers = options[:headers] || []
      body = options[:body] || ''
    end
    headers = headers.to_a if headers.kind_of?(Hash)
    request_components = {
      :method => method,
      :uri => uri,
      :headers => headers,
      :body => body
    }
    # Verify that we have all pieces required to return an HTTP request
    request_components.each do |(key, value)|
      unless value
        raise ArgumentError, "Missing :#{key} parameter."
      end
    end
    if !body.kind_of?(String) && body.respond_to?(:each)
      # Just in case we get a chunked body
      merged_body = StringIO.new
      body.each do |chunk|
        merged_body.write(chunk)
      end
      body = merged_body.string
    end
    if !body.kind_of?(String)
      raise TypeError, "Expected String, got #{body.class}."
    end
    method = method.to_s.downcase.to_sym
    request = options[:connection].build_request(method) do |req|
      req.url(Addressable::URI.parse(uri).normalize.to_s)
      req.headers = Faraday::Utils::Headers.new(headers)
      req.body = body
    end
  end

  parameters = ::Signet::OAuth1.unsigned_resource_parameters(
    :client_credential_key => self.client_credential_key,
    :token_credential_key => self.token_credential_key,
    :signature_method => options[:signature_method],
    :two_legged => self.two_legged
  )

  env = request.to_env(options[:connection])

  content_type = request['Content-Type'].to_s
  content_type = content_type.split(';', 2).first if content_type.index(';')
  if request.method == :post && content_type == 'application/x-www-form-urlencoded'
    # Serializes the body in case a hash/array was passed. Noop if already string like
    encoder = Faraday::Request::UrlEncoded.new(lambda { |_env| })
    encoder.call(env)
    request.body = env[:body]

    post_parameters = Addressable::URI.form_unencode(env[:body])
    parameters = parameters.concat(post_parameters)
  end

  # No need to attach URI query parameters, the .sign_parameters
  # method takes care of that automatically.
  signature = ::Signet::OAuth1.sign_parameters(
    env[:method],
    env[:url],
    parameters,
    self.client_credential_secret,
    self.token_credential_secret
  )

  parameters << ['oauth_signature', signature]
  request['Authorization'] =  ::Signet::OAuth1.generate_authorization_header(
      parameters, options[:realm])
  request['Cache-Control'] = 'no-store'
  return request
end

#generate_temporary_credential_request(options = {}) ⇒ Array Also known as: generate_request_token_request

Generates a request for temporary credentials.

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the request.

    • :signature_method - The signature method. Defaults to 'HMAC-SHA1'.

    • :additional_parameters - Non-standard additional parameters.

    • :realm - The Authorization realm. See RFC 2617.

Returns:

  • (Array)

    The request object.



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/signet/oauth_1/client.rb', line 588

def generate_temporary_credential_request(options={})
  verifications = {
    :temporary_credential_uri => 'Temporary credentials URI',
    :client_credential_key => 'Client credential key',
    :client_credential_secret => 'Client credential secret'
  }
  # Make sure all required state is set
  verifications.each do |(key, _value)|
    unless self.send(key)
      raise ArgumentError, "#{key} was not set."
    end
  end
  options = {
    :signature_method => 'HMAC-SHA1',
    :additional_parameters => [],
    :realm => nil,
    :connection => Faraday.default_connection
  }.merge(options)
  method = :post
  parameters = ::Signet::OAuth1.unsigned_temporary_credential_parameters(
    :client_credential_key => self.client_credential_key,
    :callback => self.callback,
    :signature_method => options[:signature_method],
    :additional_parameters => options[:additional_parameters]
  )
  signature = ::Signet::OAuth1.sign_parameters(
    method,
    self.temporary_credential_uri,
    parameters,
    self.client_credential_secret
  )
  parameters << ['oauth_signature', signature]
  authorization_header = [
    'Authorization',
    ::Signet::OAuth1.generate_authorization_header(
      parameters, options[:realm]
    )
  ]
  headers = [authorization_header]
  if method == :post
    headers << ['Content-Type', 'application/x-www-form-urlencoded']
    headers << ['Content-Length', '0']
  end
  return options[:connection].build_request(method.to_s.downcase.to_sym) do |req|
    req.url(Addressable::URI.parse(
      self.temporary_credential_uri.to_str
    ).normalize.to_s)
    req.headers = Faraday::Utils::Headers.new(headers)
  end
end

#generate_token_credential_request(options = {}) ⇒ Array Also known as: generate_access_token_request

Generates a request for token credentials.

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the request.

    • :verifier - The OAuth verifier provided by the server. Required.

    • :signature_method - The signature method. Defaults to 'HMAC-SHA1'.

    • :realm - The Authorization realm. See RFC 2617.

Returns:

  • (Array)

    The request object.



742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/signet/oauth_1/client.rb', line 742

def generate_token_credential_request(options={})
  verifications = {
    :token_credential_uri => 'Token credentials URI',
    :client_credential_key => 'Client credential key',
    :client_credential_secret => 'Client credential secret',
    :temporary_credential_key => 'Temporary credential key',
    :temporary_credential_secret => 'Temporary credential secret'
  }
  # Make sure all required state is set
  verifications.each do |(key, _value)|
    unless self.send(key)
      raise ArgumentError, "#{key} was not set."
    end
  end
  options = {
    :signature_method => 'HMAC-SHA1',
    :realm => nil,
    :connection => Faraday.default_connection
  }.merge(options)
  method = :post
  parameters = ::Signet::OAuth1.unsigned_token_credential_parameters(
    :client_credential_key => self.client_credential_key,
    :temporary_credential_key => self.temporary_credential_key,
    :signature_method => options[:signature_method],
    :verifier => options[:verifier]
  )
  signature = ::Signet::OAuth1.sign_parameters(
    method,
    self.token_credential_uri,
    parameters,
    self.client_credential_secret,
    self.temporary_credential_secret
  )
  parameters << ['oauth_signature', signature]
  authorization_header = [
    'Authorization',
    ::Signet::OAuth1.generate_authorization_header(
      parameters, options[:realm]
    )
  ]
  headers = [authorization_header]
  headers << ['Cache-Control', 'no-store']
  if method == :post
    headers << ['Content-Type', 'application/x-www-form-urlencoded']
    headers << ['Content-Length', '0']
  end
  return options[:connection].build_request(method.to_s.downcase.to_sym) do |req|
    req.url(Addressable::URI.parse(
      self.token_credential_uri.to_str
    ).normalize.to_s)
    req.headers = Faraday::Utils::Headers.new(headers)
  end
end

#temporary_credentialSignet::OAuth1::Credential Also known as: request_token

Returns the temporary credential for this client.

Returns:



308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/signet/oauth_1/client.rb', line 308

def temporary_credential
  if self.temporary_credential_key && self.temporary_credential_secret
    return ::Signet::OAuth1::Credential.new(
      self.temporary_credential_key,
      self.temporary_credential_secret
    )
  elsif !self.temporary_credential_key &&
      !self.temporary_credential_secret
    return nil
  else
    raise ArgumentError,
      "The temporary credential key and secret must be set."
  end
end

#temporary_credential=(new_temporary_credential) ⇒ Object Also known as: request_token=

Sets the temporary credential for this client.

Parameters:



329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/signet/oauth_1/client.rb', line 329

def temporary_credential=(new_temporary_credential)
  if new_temporary_credential != nil
    if !new_temporary_credential.kind_of?(::Signet::OAuth1::Credential)
      raise TypeError,
        "Expected Signet::OAuth1::Credential, " +
        "got #{new_temporary_credential.class}."
    end
    @temporary_credential_key = new_temporary_credential.key
    @temporary_credential_secret = new_temporary_credential.secret
  else
    @temporary_credential_key = nil
    @temporary_credential_secret = nil
  end
end

#temporary_credential_keyString Also known as: request_token_key

Returns the temporary credential key for this client.

Returns:

  • (String)

    The temporary credential key.



349
350
351
# File 'lib/signet/oauth_1/client.rb', line 349

def temporary_credential_key
  return @temporary_credential_key
end

#temporary_credential_key=(new_temporary_credential_key) ⇒ Object Also known as: request_token_key=

Sets the temporary credential key for this client.

Parameters:

  • new_temporary_credential_key (String, #to_str)

    The temporary credential key.



359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/signet/oauth_1/client.rb', line 359

def temporary_credential_key=(new_temporary_credential_key)
  if new_temporary_credential_key != nil
    if !new_temporary_credential_key.respond_to?(:to_str)
      raise TypeError,
        "Can't convert #{new_temporary_credential_key.class} " +
        "into String."
    end
    new_temporary_credential_key = new_temporary_credential_key.to_str
    @temporary_credential_key = new_temporary_credential_key
  else
    @temporary_credential_key = nil
  end
end

#temporary_credential_secretString Also known as: request_token_secret

Returns the temporary credential secret for this client.

Returns:

  • (String)

    The temporary credential secret.



378
379
380
# File 'lib/signet/oauth_1/client.rb', line 378

def temporary_credential_secret
  return @temporary_credential_secret
end

#temporary_credential_secret=(new_temporary_credential_secret) ⇒ Object Also known as: request_token_secret=

Sets the temporary credential secret for this client.

Parameters:

  • new_temporary_credential_secret (String, #to_str)

    The temporary credential secret.



388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/signet/oauth_1/client.rb', line 388

def temporary_credential_secret=(new_temporary_credential_secret)
  if new_temporary_credential_secret != nil
    if !new_temporary_credential_secret.respond_to?(:to_str)
      raise TypeError,
        "Can't convert #{new_temporary_credential_secret.class} " +
        "into String."
    end
    new_temporary_credential_secret =
      new_temporary_credential_secret.to_str
    @temporary_credential_secret = new_temporary_credential_secret
  else
    @temporary_credential_secret = nil
  end
end

#temporary_credential_uriAddressable::URI Also known as: request_token_uri

Returns the temporary credentials URI for this client.

Returns:

  • (Addressable::URI)

    The temporary credentials URI.



119
120
121
# File 'lib/signet/oauth_1/client.rb', line 119

def temporary_credential_uri
  return @temporary_credential_uri
end

#temporary_credential_uri=(new_temporary_credential_uri) ⇒ Object Also known as: request_token_uri=

Sets the temporary credentials URI for this client.

Parameters:

  • new_temporary_credential_uri (Addressable::URI, String, #to_str)

    The temporary credentials URI.



130
131
132
133
134
135
136
137
138
# File 'lib/signet/oauth_1/client.rb', line 130

def temporary_credential_uri=(new_temporary_credential_uri)
  if new_temporary_credential_uri != nil
    new_temporary_credential_uri =
      Addressable::URI.parse(new_temporary_credential_uri)
    @temporary_credential_uri = new_temporary_credential_uri
  else
    @temporary_credential_uri = nil
  end
end

#to_jsonString

Note:

A serialized client contains sensitive information. Persist or transmit with care.

Serialize the client object to JSON.

Returns:

  • (String)

    A serialized JSON representation of the client.



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/signet/oauth_1/client.rb', line 559

def to_json
  return MultiJson.dump({
    'temporary_credential_uri' => self.temporary_credential_uri,
    'authorization_uri' => self.authorization_uri,
    'token_credential_uri' => self.token_credential_uri,
    'callback' => self.callback,
    'two_legged' => self.two_legged,
    'client_credential_key' => self.client_credential_key,
    'client_credential_secret' => self.client_credential_secret,
    'temporary_credential_key' => self.temporary_credential_key,
    'temporary_credential_secret' => self.temporary_credential_secret,
    'token_credential_key' => self.token_credential_key,
    'token_credential_secret' => self.token_credential_secret
  })
end

#token_credentialSignet::OAuth1::Credential Also known as: access_token

Returns the token credential for this client.

Returns:



408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/signet/oauth_1/client.rb', line 408

def token_credential
  if self.token_credential_key && self.token_credential_secret
    return ::Signet::OAuth1::Credential.new(
      self.token_credential_key,
      self.token_credential_secret
    )
  elsif !self.token_credential_key &&
      !self.token_credential_secret
    return nil
  else
    raise ArgumentError,
      "The token credential key and secret must be set."
  end
end

#token_credential=(new_token_credential) ⇒ Object Also known as: access_token=

Sets the token credential for this client.

Parameters:



429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/signet/oauth_1/client.rb', line 429

def token_credential=(new_token_credential)
  if new_token_credential != nil
    if !new_token_credential.kind_of?(::Signet::OAuth1::Credential)
      raise TypeError,
        "Expected Signet::OAuth1::Credential, " +
        "got #{new_token_credential.class}."
    end
    @token_credential_key = new_token_credential.key
    @token_credential_secret = new_token_credential.secret
  else
    @token_credential_key = nil
    @token_credential_secret = nil
  end
end

#token_credential_keyString Also known as: access_token_key

Returns the token credential key for this client.

Returns:

  • (String)

    The token credential key.



449
450
451
# File 'lib/signet/oauth_1/client.rb', line 449

def token_credential_key
  return @token_credential_key
end

#token_credential_key=(new_token_credential_key) ⇒ Object Also known as: access_token_key=

Sets the token credential key for this client.

Parameters:

  • new_token_credential_key (String, #to_str)

    The token credential key.



459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/signet/oauth_1/client.rb', line 459

def token_credential_key=(new_token_credential_key)
  if new_token_credential_key != nil
    if !new_token_credential_key.respond_to?(:to_str)
      raise TypeError,
        "Can't convert #{new_token_credential_key.class} " +
        "into String."
    end
    new_token_credential_key = new_token_credential_key.to_str
    @token_credential_key = new_token_credential_key
  else
    @token_credential_key = nil
  end
end

#token_credential_secretString Also known as: access_token_secret

Returns the token credential secret for this client.

Returns:

  • (String)

    The token credential secret.



478
479
480
# File 'lib/signet/oauth_1/client.rb', line 478

def token_credential_secret
  return @token_credential_secret
end

#token_credential_secret=(new_token_credential_secret) ⇒ Object Also known as: access_token_secret=

Sets the token credential secret for this client.

Parameters:

  • new_token_credential_secret (String, #to_str)

    The token credential secret.



488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/signet/oauth_1/client.rb', line 488

def token_credential_secret=(new_token_credential_secret)
  if new_token_credential_secret != nil
    if !new_token_credential_secret.respond_to?(:to_str)
      raise TypeError,
        "Can't convert #{new_token_credential_secret.class} " +
        "into String."
    end
    new_token_credential_secret =
      new_token_credential_secret.to_str
    @token_credential_secret = new_token_credential_secret
  else
    @token_credential_secret = nil
  end
end

#token_credential_uriAddressable::URI Also known as: access_token_uri

Returns the token credential URI for this client.

Returns:

  • (Addressable::URI)

    The token credential URI.



181
182
183
# File 'lib/signet/oauth_1/client.rb', line 181

def token_credential_uri
  return @token_credential_uri
end

#token_credential_uri=(new_token_credential_uri) ⇒ Object Also known as: access_token_uri=

Sets the token credential URI for this client.

Parameters:

  • new_token_credential_uri (Addressable::URI, Hash, String, #to_str)

    The token credential URI.



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/signet/oauth_1/client.rb', line 191

def token_credential_uri=(new_token_credential_uri)
  if new_token_credential_uri != nil
    new_token_credential_uri = Addressable::URI.send(
      new_token_credential_uri.kind_of?(Hash) ? :new : :parse,
      new_token_credential_uri
    )
    @token_credential_uri = new_token_credential_uri
  else
    @token_credential_uri = nil
  end
end

#two_leggedTrueClass, FalseClass

Returns whether the client is in two-legged mode.

Returns:

  • (TrueClass, FalseClass)

    true for two-legged mode, false otherwise.



535
536
537
# File 'lib/signet/oauth_1/client.rb', line 535

def two_legged
  return @two_legged ||= false
end

#two_legged=(new_two_legged) ⇒ Object

Sets the client for two-legged mode.

Parameters:

  • new_two_legged (TrueClass, FalseClass)

    true for two-legged mode, false otherwise.



544
545
546
547
548
549
550
551
# File 'lib/signet/oauth_1/client.rb', line 544

def two_legged=(new_two_legged)
  if new_two_legged != true && new_two_legged != false
    raise TypeError,
      "Expected true or false, got #{new_two_legged.class}."
  else
    @two_legged = new_two_legged
  end
end

#update!(options = {}) ⇒ Object

Updates an OAuth 1.0 client.

Examples:

client.update!(
  :temporary_credential_uri =>
    'https://www.google.com/accounts/OAuthGetRequestToken',
  :authorization_uri =>
    'https://www.google.com/accounts/OAuthAuthorizeToken',
  :token_credential_uri =>
    'https://www.google.com/accounts/OAuthGetAccessToken',
  :client_credential_key => 'anonymous',
  :client_credential_secret => 'anonymous'
)

Parameters:

  • options (Hash) (defaults to: {})

    The configuration parameters for the client.

    • :temporary_credential_uri - The OAuth temporary credentials URI.

    • :authorization_uri - The OAuth authorization URI.

    • :token_credential_uri - The OAuth token credentials URI.

    • :client_credential_key - The OAuth client credential key.

    • :client_credential_secret - The OAuth client credential secret.

    • :callback - The OAuth callback. Defaults to ‘oob’.

See Also:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/signet/oauth_1/client.rb', line 90

def update!(options={})
  # Normalize key to String to allow indifferent access.
  options = options.inject({}) { |accu, (k, v)| accu[k.to_s] = v; accu }
  self.temporary_credential_uri = options["temporary_credential_uri"]
  self.authorization_uri = options["authorization_uri"]
  self.token_credential_uri = options["token_credential_uri"]
  # Technically... this would allow you to pass in a :client key...
  # But that would be weird.  Don't do that.
  self.client_credential_key =
    Signet::OAuth1.extract_credential_key_option("client", options)
  self.client_credential_secret =
    Signet::OAuth1.extract_credential_secret_option("client", options)
  self.temporary_credential_key =
    Signet::OAuth1.extract_credential_key_option("temporary", options)
  self.temporary_credential_secret =
    Signet::OAuth1.extract_credential_secret_option("temporary", options)
  self.token_credential_key =
    Signet::OAuth1.extract_credential_key_option("token", options)
  self.token_credential_secret =
    Signet::OAuth1.extract_credential_secret_option("token", options)
  self.callback = options["callback"]
  self.two_legged = options["two_legged"] || false
  return self
end