Class: GData::Auth::ClientLogin

Inherits:
Object
  • Object
show all
Defined in:
lib/gdata/auth/clientlogin.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, options = {}) ⇒ ClientLogin

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gdata/auth/clientlogin.rb', line 38

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

Instance Attribute Details

#account_typeObject

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



30
31
32
# File 'lib/gdata/auth/clientlogin.rb', line 30

def 
  @account_type
end

#auth_urlObject

The ClientLogin authentication handler



26
27
28
# File 'lib/gdata/auth/clientlogin.rb', line 26

def auth_url
  @auth_url
end

#serviceObject

The service name for the API you are working with



34
35
36
# File 'lib/gdata/auth/clientlogin.rb', line 34

def service
  @service
end

#tokenObject

The access token



32
33
34
# File 'lib/gdata/auth/clientlogin.rb', line 32

def token
  @token
end

Instance Method Details

#get_token(username, password, source, login_token = nil, login_captcha = nil) ⇒ Object

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gdata/auth/clientlogin.rb', line 57

def get_token(username, password, source,  = nil, 
     = nil)
  body = Hash.new
  body['accountType'] = @account_type
  body['Email'] = username
  body['Passwd'] = password
  body['service'] = @service
  body['source'] = source
  if  and 
    body['logintoken'] = 
    body['logincaptcha'] = 
  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) ⇒ Object

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



94
95
96
97
98
99
100
# File 'lib/gdata/auth/clientlogin.rb', line 94

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