class GData::Auth::ClientLogin

This class implements ClientLogin signatures for Data API requests. It can be used with a GData::Client::GData object.

Attributes

account_type[RW]

One of 'HOSTED_OR_GOOGLE', 'GOOGLE', or 'HOSTED'. See documentation here: code.google.com/apis/accounts/docs/AuthForInstalledApps.html

auth_url[RW]

The ClientLogin authentication handler

service[RW]

The service name for the API you are working with

token[RW]

The access token

Public Class Methods

new(service, options = {}) click to toggle source

Initialize the class with the service name of an API that you wish to request a token for.

# File lib/gdata/auth/clientlogin.rb, line 37
def initialize(service, options = {})
  if service.nil?
    raise ArgumentError, "Service name cannot be nil"
  end
  
  @service = service
  
  options.each do |key, value|
    self.send("#{key}=", value)
  end
  
  @auth_url ||= 'https://www.google.com/accounts/ClientLogin'
  @account_type ||= 'HOSTED_OR_GOOGLE'
end

Public Instance Methods

get_token(username, password, source, login_token = nil, login_captcha = nil) click to toggle source

Retrieves a token for the given username and password. source identifies your application. login_token and login_captcha are used only if you are responding to a previously issued CAPTCHA challenge.

# File lib/gdata/auth/clientlogin.rb, line 56
def get_token(username, password, source, login_token = nil, 
    login_captcha = nil)
  body = Hash.new
  body['accountType'] = @account_type
  body['Email'] = username
  body['Passwd'] = password
  body['service'] = @service
  body['source'] = source
  if login_token and login_captcha
    body['logintoken'] = login_token
    body['logincaptcha'] = login_captcha
  end
  
  request = GData::HTTP::Request.new(@auth_url, :body => body, 
    :method => :post)
  service = GData::HTTP::DefaultService.new
  response = service.make_request(request)
  if response.status_code != 200
    url = response.body[/Url=(.*)/,1]
    error = response.body[/Error=(.*)/,1]
    
    if error == "CaptchaRequired"
      captcha_token = response.body[/CaptchaToken=(.*)/,1]
      captcha_url = response.body[/CaptchaUrl=(.*)/,1]
      raise GData::Client::CaptchaError.new(captcha_token, captcha_url), 
        "#{error} : #{url}"
    end
    
    raise GData::Client::AuthorizationError.new(response)
  end
  
  @token = response.body[/Auth=(.*)/,1]
  return @token
end
sign_request!(request) click to toggle source

Creates an appropriate Authorization header on a GData::HTTP::Request object.

# File lib/gdata/auth/clientlogin.rb, line 93
def sign_request!(request)
  if @token == nil
    raise GData::Client::Error, "Cannot sign request without credentials"
  end
  
  request.headers['Authorization'] = "GoogleLogin auth=#{@token}" 
end