Method: Twitter::REST::OAuth#token

Defined in:
lib/twitter/rest/oauth.rb

#token(options = {}) ⇒ String Also known as: bearer_token

Allows a registered application to obtain an OAuth 2 Bearer Token, which can be used to make API requests on an application's own behalf, without a user context.

Only one bearer token may exist outstanding for an application, and repeated requests to this method will yield the same already-existent token until it has been invalidated.

Examples:

Generate a Bearer Token

client = Twitter::REST::Client.new(consumer_key: 'abc', consumer_secret: 'def')
bearer_token = client.token

Parameters:

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

    A customizable set of options.

Returns:

  • (String)

    The Bearer token.

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Required



24
25
26
27
28
29
30
31
32
# File 'lib/twitter/rest/oauth.rb', line 24

def token(options = {})
  options = options.dup
  options[:bearer_token_request] = true
  options[:grant_type] ||= "client_credentials"
  url = "https://api.twitter.com/oauth2/token"
  headers = Twitter::Headers.new(self, :post, url, options).request_headers
  response = HTTP.headers(headers).post(url, form: options)
  response.parse["access_token"]
end