Class: Client
- Inherits:
-
Object
- Object
- Client
- Defined in:
- lib/sem4r_debug_client/client.rb
Instance Method Summary collapse
-
#authorize_token(force = false) ⇒ Object
ClientLogin.
-
#initialize(profile = :sandbox) ⇒ Client
constructor
A new instance of Client.
- #oauth ⇒ Object
- #read_message(filename) ⇒ Object
- #send(service_url, soap_action, filename) ⇒ Object
- #send_oauth(service_url, soap_action, filename) ⇒ Object
Constructor Details
#initialize(profile = :sandbox) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sem4r_debug_client/client.rb', line 17 def initialize(profile = :sandbox) @config = Sem4r::Profile.configure @options = Sem4r::Profile.load_config(@config, profile) Sem4r::Profile.try_to_find_in_password_file("password", @options, @config) Sem4r::Profile.try_to_find_in_password_file("auth_token", @options, @config) Sem4r::Profile.try_to_find_in_password_file("token_credential_key", @options, @config) Sem4r::Profile.try_to_find_in_password_file("token_credential_secret", @options, @config) @email = @options["email"] @password = @options["password"] @auth_token = @options["auth_token"] @token_credential_key = @options["token_credential_key"] @token_credential_secret = @options["token_credential_secret"] end |
Instance Method Details
#authorize_token(force = false) ⇒ Object
ClientLogin
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/sem4r_debug_client/client.rb', line 129 def (force = false) return @auth_token unless force or @auth_token.nil? url = "https://www.google.com/accounts/ClientLogin" body = "accountType=GOOGLE&Email=#{URI.escape(@email)}&Passwd=#{URI.escape(@password)}&service=adwords" headers = {'Content-Type' => 'application/x-www-form-urlencoded'} client = HTTPClient.new(:agent_name => 'Ruby') response = client.post(url, body, headers) #pp response #pp response.body if response.status < 400 # pp response.body.content end @auth_token = response.body.content[/Auth=(.*)/, 1] @options["auth_token"] = @auth_token Sem4r::Profile.save_passwords("auth_token", @options, @config) @auth_token end |
#oauth ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/sem4r_debug_client/client.rb', line 153 def oauth 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' ) # ""https://{server}/api/adwords/", # where {server} is either # "adwords.google.com" or "adwords-sandbox.google.com". client.fetch_temporary_credential!(:additional_parameters => { # :scope => 'https://mail.google.com/mail/feed/atom' :scope => "https://adwords-sandbox.google.com/api/adwords/" }) puts "go to this url: #{client.}" puts "and type authorization here: " verifier = gets.chop puts "authorization is '#{verifier}'" client.fetch_token_credential!(:verifier => verifier) puts "client_credential_key #{client.client_credential_key}" puts "client_credential_secret #{client.client_credential_secret}" puts "token_credential_key #{client.token_credential_key}" puts "token_credential_secret #{client.token_credential_secret}" @options["token_credential_key"] = client.token_credential_key @options["token_credential_secret"] = client.token_credential_secret Sem4r::Profile.save_passwords("token_credential_key", @options, @config) Sem4r::Profile.save_passwords("token_credential_secret", @options, @config) # response = client.fetch_protected_resource( # :uri => 'https://mail.google.com/mail/feed/atom' # ) # The Rack response format is used here # status, headers, body = response # puts body end |
#read_message(filename) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sem4r_debug_client/client.rb', line 105 def (filename) requests_xml_dir = File.(File.join(File.dirname(__FILE__), "requests_xml")) unless File.directory?(requests_xml_dir) puts "#{requests_xml_dir} not exists" exit end var_for_erb = VarForErb.new var_for_erb.email = @email var_for_erb.password = @password var_for_erb.auth_token = @auth_token var_for_erb.developer_token = "#{@email}++EUR" var_for_erb.client_email = "client_1+#{@email}" filepath = File.join(requests_xml_dir, filename) template_request_xml = File.read(filepath) template = ERB.new(template_request_xml) request_xml = template.result(var_for_erb.get_binding) request_xml end |
#send(service_url, soap_action, filename) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sem4r_debug_client/client.rb', line 36 def send(service_url, soap_action, filename) puts "** #{service_url}" request_xml = (filename) puts Sem4r::pretty_print_xml_with_nokogiri(request_xml) headers = { "Content-Type" => "text/xml; charset=utf-8", "Content-Length" => request_xml.length.to_s, "SOAPAction" => soap_action} client = HTTPClient.new(:agent_name => 'Ruby') response = client.post(service_url, request_xml, headers) # pp response response_xml = response.body.content puts "*********************" puts Sem4r::pretty_print_xml_with_nokogiri(response_xml) end |
#send_oauth(service_url, soap_action, filename) ⇒ Object
56 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 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/sem4r_debug_client/client.rb', line 56 def send_oauth(service_url, soap_action, filename) puts "** #{service_url}" request_xml = (filename) puts Sem4r::pretty_print_xml_with_nokogiri(request_xml) headers = { "Content-Type" => "text/xml; charset=utf-8", "Content-Length" => request_xml.length.to_s, "SOAPAction" => soap_action} 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', :token_credential_key => @token_credential_key, :token_credential_secret => @token_credential_secret ) request = client.generate_authenticated_request( :method => "POST", :uri => service_url, :headers => headers, :body => request_xml ) auth_header = request[2].select { |k,v| k == "Authorization" } v = auth_header[0][1] headers["Authorization"] = v pp headers client = HTTPClient.new(:agent_name => 'Ruby') response = client.post(service_url, request_xml, headers) # pp response response_xml = response.body.content puts "*********************" puts Sem4r::pretty_print_xml_with_nokogiri(response_xml) end |