Class: Gplus::Client

Inherits:
Object
  • Object
show all
Includes:
Activity, Comment, Person
Defined in:
lib/gplus/client.rb

Overview

The main Gplus class, containing methods for initializing a Google+ client and requesting authorization

Constant Summary collapse

DEFAULT_ENDPOINT =

The default Google+ API endpoint that all requests are sent to.

'https://www.googleapis.com/plus'
DEFAULT_API_VERSION =

The default version of the Google+ API to send requests to.

'v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Person

#get_person, #list_people_by_activity, #search_people

Methods included from Comment

#get_comment, #list_comments

Methods included from Activity

#get_activity, #list_activities, #search_activities

Constructor Details

#initialize(options = {}) ⇒ Gplus::Client

Create a Google+ API client. Read the README to find learn about the different ways of initializing a client.

Parameters:

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

Options Hash (options):

  • :api_key (String)

    Your application's API key, used for non-authenticated requests (for public data).

  • :token (String)

    The OAuth token to authorize the API client for authenticated requests (for non-public data). This can be supplied after initialization by calling #authorize.

  • :refresh_token (String)

    The OAuth refresh_token, to request a new token if the provided token has expired.

  • :token_expires_at (Integer)

    The time that the OAuth token expires at in seconds since the epoch.

  • :client_id (String)

    Your application's Client ID. Required to generate an authorization URL with #authorize_url.

  • :client_secret (String)

    Your application's Client Secret. Required to generate an authorization URL with #authorize_url.

  • :redirect_uri (String)

    The default URI to redirect to after authorization. You can override this in many other methods. It must be specified as an authorized URI in your application's console. Required to generate an authorization URL with #authorize_url.


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

def initialize(options = {})
  self.endpoint = DEFAULT_ENDPOINT
  self.api_version = DEFAULT_API_VERSION

  @api_key = options[:api_key]
  @token = options[:token]
  @refresh_token = options[:refresh_token]
  @token_expires_at = options[:token_expires_at]
  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @redirect_uri = options[:redirect_uri]

  @oauth_client = OAuth2::Client.new(
    @client_id,
    @client_secret,
    :site => self.endpoint,
    :authorize_url => 'https://accounts.google.com/o/oauth2/auth',
    :token_url => 'https://accounts.google.com/o/oauth2/token'
  )
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version


13
14
15
# File 'lib/gplus/client.rb', line 13

def api_version
  @api_version
end

#endpointObject

Returns the value of attribute endpoint


13
14
15
# File 'lib/gplus/client.rb', line 13

def endpoint
  @endpoint
end

Instance Method Details

#access_tokenObject

Retrieve or create an OAuth2::AccessToken, using the :token and :refresh_token specified when the API client instance was initialized

Returns:

  • An OAuth2::AccessToken


89
90
91
92
93
94
95
96
97
98
# File 'lib/gplus/client.rb', line 89

def access_token
  if @token
    @access_token ||= OAuth2::AccessToken.new(@oauth_client, @token, :refresh_token => @refresh_token, :expires_at => @token_expires_at)
    if @access_token.expired?
      @access_token = @access_token.refresh!
      @access_token_refreshed = true
    end
    @access_token
  end
end

#access_token_refreshed?Boolean

Return true if the user's access token has been refreshed. If so, you should store the new token's :token and :expires_at.

Returns:

  • (Boolean)

101
102
103
# File 'lib/gplus/client.rb', line 101

def access_token_refreshed?
  @access_token_refreshed
end

#authorize(token, refresh_token, token_expires_at) ⇒ Object

Authorize a Gplus::Client instance to access the user's private data, after initialization

Parameters:

  • :token (String)

    The OAuth token to authorize the API client for authenticated requests (for non-public data).

  • :refresh_token (String)

    The OAuth refresh_token, to request a new token if the provided token has expired.

  • :token_expires_at (Integer)

    The time that the OAuth token expires at in seconds since the epoch.

Returns:

  • An OAuth2::AccessToken


79
80
81
82
83
84
# File 'lib/gplus/client.rb', line 79

def authorize(token, refresh_token, token_expires_at)
  @token = token
  @refresh_token = refresh_token
  @token_expires_at = token_expires_at
  access_token
end

#authorize_url(options = {}) ⇒ String

Generate an authorization URL where a user can authorize your application to access their Google+ data.

Parameters:

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

    Additional parameters used in the OAuth request.

Options Hash (options):

  • :redirect_uri (String)

    An optional over-ride for the redirect_uri you initialized the API client with. This must match the redirect_uri you use when you call #get_token.

  • :access_type (String) — default: 'online'

    . Indicates if your application needs to access a Google API when the user is not present at the browser. Allowed values are 'online' and 'offline'.

Returns:

  • (String)

    A Google account authorization URL for your application.

See Also:


54
55
56
57
58
# File 'lib/gplus/client.rb', line 54

def authorize_url(options = {})
  defaults = { :scope => 'https://www.googleapis.com/auth/plus.me', :redirect_uri => @redirect_uri }
  options = defaults.merge(options)
  @oauth_client.auth_code.authorize_url(options)
end

#get_token(auth_code, params = {}, opts = {}) ⇒ OAuth2::AccessToken

Retrieve an OAuth access token using the short-lived authentication code given to you after a user authorizes your application. Note that if you specified an over-ride redirect_uri when you called #authorize_url, you must use the same redirect_uri when calling #get_token.

Parameters:

  • auth_code (String)

    The code returned to your redirect_uri after the user authorized your application to access their Google+ data.

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

    Additional parameters for the token endpoint (passed through to OAuth2::Client#get_token)

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

    Additional access token options (passed through to OAuth2::Client#get_token)

Returns:


67
68
69
70
71
# File 'lib/gplus/client.rb', line 67

def get_token(auth_code, params = {}, opts = {})
  defaults = { :redirect_uri => @redirect_uri }
  params = defaults.merge(params)
  @access_token = @oauth_client.auth_code.get_token(auth_code, params, opts)
end