I am trying to access the AdSense Management API using ruby. They recommend using their generic Google-API client library:
http://code.google.com/p/google-api-ruby-client/#Google_AdSense_Management_API
This hasn't been very helpful and I have run into errors:
Faraday conflicts in google_drive and google-api-client
Where should I start in order to get access to my AdSense data?
Thanks in advance.
Unfortunately, we haven't prepared any sample code for the AdSense Management API... yet! As you point out, though, the client library is generic, and should work with any of the newer Google APIs, so some of the other samples may help.
If you're running into any specific issues, please create a question focused on those and point me to it, and I'll do my best to help.
If you want a quick sample to get started, I can cook that up for you, but we should make sure the issues you're running into have to do with the AdSense Management API itself, and not just the client library, as the one you were linking to.
[Edit]
Here's a quick sample, based on Sinatra:
#!/usr/bin/ruby
require 'rubygems'
require 'sinatra'
require 'google/api_client'
FILENAME = 'auth.obj'
OAUTH_CLIENT_ID = 'INSERT_OAUTH2_CLIENT_ID_HERE'
OAUTH_CLIENT_SECRET = 'INSERT_OAUTH2_CLIENT_SECRET_HERE'
before do
#client = Google::APIClient.new
#client.authorization.client_id = OAUTH_CLIENT_ID
#client.authorization.client_secret = OAUTH_CLIENT_SECRET
#client.authorization.scope = 'https://www.googleapis.com/auth/adsense'
#client.authorization.redirect_uri = to('/oauth2callback')
#client.authorization.code = params[:code] if params[:code]
# Load the access token here if it's available
if File.exist?(FILENAME)
serialized_auth = IO.read(FILENAME)
#client.authorization = Marshal::load(serialized_auth)
end
if #client.authorization.refresh_token && #client.authorization.expired?
#client.authorization.fetch_access_token!
end
#adsense = #client.discovered_api('adsense', 'v1.1')
unless #client.authorization.access_token || request.path_info =~ /^\/oauth2/
redirect to('/oauth2authorize')
end
end
get '/oauth2authorize' do
redirect #client.authorization.authorization_uri.to_s, 303
end
get '/oauth2callback' do
#client.authorization.fetch_access_token!
# Persist the token here
serialized_auth = Marshal::dump(#client.authorization)
File.open(FILENAME, 'w') do |f|
f.write(serialized_auth)
end
redirect to('/')
end
get '/' do
call = {
:api_method => #adsense.reports.generate,
:parameters => {
'startDate' => '2011-01-01',
'endDate' => '2011-08-31',
'dimension' => ['MONTH', 'CUSTOM_CHANNEL_NAME'],
'metric' => ['EARNINGS', 'TOTAL_EARNINGS']
}
}
response = #client.execute(call)
output = ''
if response && response.data && response.data['rows'] &&
!response.data['rows'].empty?
result = response.data
output << '<table><tr>'
result['headers'].each do |header|
output << '<td>%s</td>' % header['name']
end
output << '</tr>'
result['rows'].each do |row|
output << '<tr>'
row.each do |column|
output << '<td>%s</td>' % column
end
output << '</tr>'
end
output << '</table>'
else
output << 'No rows returned'
end
output
end
Related
I'd like to add a method in my Rails application that checks the validity of a VAT number using the EU's VIES system: http://ec.europa.eu/taxation_customs/vies/technicalInformation.html
I'm already pretty new to programming in Rails and the instructions here use xml. So I have trouble figuring this out. How should I include the code mentioned on the mentioned website in my Rails application?
In other words, what should the validate_vat(country, vatnumber) method below look like and how to process the response received from the SOAP service?
def vatsubmission
#organization = Organization.find(params[:id])
#organization.update_attributes(vat_params)
#organization.validate_vat(#organization.country, #organization.vatnumber) if (#organization.vatnumber? && #organization.vatnumber?)
# Process response
if valid == false
#organization.update_attributes(valid_vat: false)
flash.now[:danger] = "False VAT number"
render ...
elsif valid == true
#organization.update_attributes(valid_vat: true)
flash.now[:success] = "VAT number validated"
render ...
else
flash.now[:danger] = "VAT number could not be validated"
render ...
end
end
def validate_vat(country, vatnumber)
??
end
Update: I've added gem 'savon', '2.11.1' to my gemfile. In my controller I have:
def update
#organization = Organization.find(params[:id])
if #organization.check_valid == true
#organization.update_attributes(validnr: true)
else
#organization.update_attributes(validnr: false)
end
end
And I have added the following model method:
require 'savon'
def check_valid
debugger
if ["DK", "CY", "etc"].include? self.country
client = Savon.client(wsdl: 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl')
resp = client.call :check_vat do
message country_code: self.country, vat_number: self.vatnr
end
data = resp.to_hash[:check_vat_response]
data[:valid]
end
end
Error: The line message country_code: self.country, vat_number: self.vatnr fails with the error message: wrong number of arguments (1 for 2). I checked with the debugger and self.country as well as self.varnr do have values. What am I doing wrong?
For working with SOAP from Ruby I used excelent Savon gem.
With Savon v2, working code looks like this:
require 'savon'
client = Savon.client(wsdl: 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl')
resp = client.call :check_vat do
message country_code: 'AT', vat_number: '123'
end
data = resp.to_hash[:check_vat_response]
data[:valid] #=> false :)
Note Savon v3 is still in preparation.
I've just started using the ValVat gem for this and it workd beautifully so far!
here's my problem:
I need to post data from RoR server to remote PHP server, to a specific url, but before that I need to authenticate.. any help is much appreciated..
What I have done so far..
#sample data
postparams ={'id'=>1, 'name'=>'Test', 'phone'=>'123123123'}
#url - is in form http://domain.com/some/somemore
#user - contains username
#pass - contains password
require "uri"
require "net/http"
uri = URI(url)
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(postparams)
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
case res
when Net::HTTPSuccess, Net::HTTPRedirection
#all ok
else
res.value
end
Obviously I get 403.. because I'm not authorized? How do I authorize?
I also tried my luck with mechanize gem (below - using the same "sample" data\vars)
#when not logged in it renders login form
login_form = agent.get(url).forms.first
login_form.username = user
login_form.password = pass
# submit login form
agent.submit(login_form, login_form.buttons.first)
#not sure how to submit to url..
#note that accessing url will not render the from
#(I can't access it as I did with login form) - I simply need to post postparams
#to this url... and get the response code..
I think the mechanize gem is your best choice.
Here is an example showing how to post a file to flicker using mechanize.
Maybe you could easily adapt to your needs:
require 'rubygems'
require 'mechanize'
abort "#{$0} login passwd filename" if (ARGV.size != 3)
a = Mechanize.new { |agent|
# Flickr refreshes after login
agent.follow_meta_refresh = true
}
a.get('http://flickr.com/') do |home_page|
signin_page = a.click(home_page.link_with(:text => /Sign In/))
my_page = signin_page.form_with(:name => 'login_form') do |form|
form.login = ARGV[0]
form.passwd = ARGV[1]
end.submit
# Click the upload link
upload_page = a.click(my_page.link_with(:text => /Upload/))
# We want the basic upload page.
upload_page = a.click(upload_page.link_with(:text => /basic Uploader/))
# Upload the file
upload_page.form_with(:method => 'POST') do |upload_form|
upload_form.file_uploads.first.file_name = ARGV[2]
end.submit
end
I strongly suggest the use of ruby rest-client gem.
I need to send different request with headers and body to PayPal. I want to use standatd class NET::HTTP, so here is my code(ISN'T WORKING):
require "net/http"
require "uri"
header = {...}
body = {...}
url = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
uri = URI.parse(url)
args = { 'header' => header,'body' => body }
res = Net::HTTP.post_form(uri, args)
puts res.status
Gives me error:
C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock': An existing connection was forcibly closed by the remote host. (Errno::ECONNRESET)
EDIT
Second variant:
require 'httpclient'
require 'xmlsimple'
header = {"X-PAYPAL-SECURITY-USERID" => "tok261_biz_api.abc.com",
"X-PAYPAL-SECURITY-PASSWORD" => "1244612379",
"X-PAYPAL-SECURITY-SIGNATURE" => "lkfg9groingghb4uw5",
"X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
"X-PAYPAL-APPLICATION-ID" => "APP-80W284485P519543T"
}
#data to be sent in the request
data = {"emailAddress" => "denmed_1342605975_biz#gmail.com",
"firstName"=> "Den",
"lastName" => "Med",
"matchCriteria"=> "NAME",
"requestEnvelope.errorLanguage" => "en_US"}
#initialize the request
clnt = HTTPClient.new
#API end point(sandbox)
uri = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
#make the post
res = clnt.post(uri, data, header)
if res.status == 200
#xml = XmlSimple.xml_in(res.content)
if #xml['accountType']!=nil
account_type = #xml['accountType'][0]
#its pretty obvious from here init?
if account_type.to_s() == "Business"
puts "Business account!"
elseif account_type.to_s() == "Premier"
puts "Premier Account!"
end
elseif account_type.to_s() == "Personal"
puts "Personal account!"
else
puts "Account type not null but not a valid PayPal account type."
end
else
puts "Gee! sorry! something went seriously wrong"
end
This method - constantly gives me - Account type not null but not a valid PayPal account type.
But it is verified in Sandbox ! Tried to leave blank field, but it gave me the same !
Thanks for help in advance !
I am not familiar with your second code example, however I have used the first before (Net::HTTP) and I find it works quite well. I have generally only used it for GET, but I will try to advise on POST :)
First Thing: Headers should not be set as data to be sent with the request (which it appears you are doing) - instead, each header should be set as an attribute of the request:
request['X-HEADER_NAME'] = header_value
Here is how I suggest your code block should look if using Net::HTTP:
require "net/http"
require "uri"
url = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
uri = URI.parse(url)
req = Net::HTTP::Post.new(url)
req["X-PAYPAL-SECURITY-USERID"] = "tok261_biz_api.abc.com"
req["X-PAYPAL-SECURITY-PASSWORD"] = "1244612379"
req["...."] = "...." .... etc for all headers
req.set_form_data( {"emailAddress" => "denmed_1342605975_biz#gmail.com",
"firstName"=> "Den",
"lastName" => "Med",
"matchCriteria"=> "NAME",
"requestEnvelope.errorLanguage" => "en_US"} )
Net::HTTP.start(uri.host, uri.port) do |http|
response = http.request(req)
return response
end
I suggest giving something like that a try, and if it doesn't work, can you give me the exact wording of the given error?
Note
Another suggestion is that, whenever my application receives Errno::ECONNRESET, it means that the back end server is not reachable (in this case I guess that would be the paypal server) - Are you positive the server is running? And are you positive there are no firewalls or anything in place which are preventing you from connecting?
The following code
purchase = #order.authorize_payment(#credit_card, options)
is_success = purchase.success?
if is_success
...
else
flash[:notice] = "!! " + purchase.message + "" +
purchase.params['missingField'].to_s
redirect_to :action => :payment, :id => #order.id
end
results in "!! Failed with 500 Internal Server Error" in my flash[:notice]. There is no stacktrace, no webserver error, all that I know is that purchase.message is populated and purchase.success? is false.
I am really at a loss to figure out how to troubleshoot this. I think it might be an ssl requirement, but I can't either see the soap request, or test basic connectivity with cybersource (my payment gateway).
I establish my gateway with this code (after config.after_initialize do):
ActiveMerchant::Billing::Base.mode = :production # :test
ActiveMerchant::Billing::CreditCard.require_verification_value = false
ActiveMerchant::Billing::CyberSourceGateway.wiredump_device = File.new(File.join([Rails.root, "log", "cybersource.log"]), "a") # doesn't work (!)
# we need to open an external file to get the password
mypassphrase = File.open('/var/www/foo/shared/passphrase.txt').read
OrderTransaction.gateway = ActiveMerchant::Billing::CyberSourceGateway.new(:login => 'vxxxxxxx',
:password => mypassphrase.to_s,
:test => false,
:vat_reg_number => 'your VAT registration number',
# sets the states/provinces where you have a physical presense for tax purposes
:nexus => "GA OH",
# don‘t want to use AVS so continue processing even if AVS would have failed
:ignore_avs => true,
# don‘t want to use CVV so continue processing even if CVV would have failed
:ignore_cvv => true,
:money_format => :dollars
)
Can I see the soap request? Are there ways to test part of this? Any help greatly appreciated.
Best,
Tim
ActiveMerchant::Billing::CyberSourceGateway.logger = your_logger
So, late response but...
I've done a good amount of work with the Cybersource gateway, and the only way to see the SOAP request/response of the cybersource gateway currently is to open up the gem and edit it.
If you modify the commit method of lib/active_merchant/billing/gateways/cybersource.rb, you can do something like this:
def commit(request, options)
puts "*** POSTING TO: #{test? ? TEST_URL : LIVE_URL}"
request = build_request(request, options)
puts "*** POSTING:"
puts request
begin
post_response = ssl_post(test? ? TEST_URL : LIVE_URL, request)
rescue ActiveMerchant::ResponseError => e
puts "ERROR!"
puts e.response
end
puts post_response
It would be nice if there was a way to get that response without going through that hassle, I'll see if there's a way to pass that information up through the response object that's returned and add it to my fork.
LocationLabs and Loc-Aid are location aggregation services that expose REST APIs. They currently offer Java, .NET and PHP SDKs. The API is not complex but, still, as a learning tool, it would be nice to have a Ruby tutorial or example to play with, extent, etc.
After further analysis, I built my application on Loc-Aid's infrastructure. Since there weren't many Ruby examples I could find, I'm sharing a snippet from my app.
# Get SAVON soap client locaid location services
# Parameters:
# - none
# Returns: SOAP client for locaid location services
def get_location_client
Savon::Client.new do
wsdl.document = LOCAID_CONFIG['use_local_wsdl'] ?
File.expand_path(LOCAID_CONFIG['get_location_wsdl'].to_s, ::Rails.root.to_s) :
LOCAID_CONFIG['get_location_wsdl'].to_s
wsdl.endpoint = LOCAID_CONFIG['get_location_endpoint'].to_s
end
end
# Strip the return result from locaid response as a hash
# Parameters:
# - raw_response: Raw response XMLfrom locaid services
# - response_name: Response name which wrap the response return result in locaid response XML
# Returns: Hash corresponding to the key "return" in locaid soap response hash.
# Sample Raw Response:
# {:subscribe_phone_response=>{:return=>{:error=>{:error_code=>"00001", :error_message=>"Invalid or inactive user"}, :transaction_id=>"14028251"},
# :"#xmlns:ns2"=>"http://webservice.portico.locaid.net/"}}
def strip_locaid_return(raw_response, response_name)
unless raw_response.to_hash.has_key?(response_name)
raise TropoExceptions::ExternalError
end
raw_response[response_name][:return]
end
# Get location from locaid by the caller id
# Parameters:
# - #caller_id: Caller id get from scope value
# Returns: none
def location_from_locaid
client = get_location_client
client.http.read_timeout = LOCAID_CONFIG['get_location_timeout'].to_i
# Call locaider service to get location
response = client.request :wsdl, :get_locations_x do |soap|
soap.body = {
:login => LOCAID_CONFIG['login'],
:password => LOCAID_CONFIG['password'],
:class_id => LOCAID_CONFIG['class_id'],
:msisdn_list => ["1#{#caller_id}"],
:coor_type => "DECIMAL",
:location_method => LOCAID_CONFIG['location_method'],
:sync_type => "SYN",
:overage => "1"
}
end
result_hash = strip_locaid_return(response, :get_locations_x_response)
if result_hash.has_key?(:error)
raise TropoExceptions::ExternalError
end
yield result_hash[:location_response].is_a?(Array) ?
result_hash[:location_response][0] :
result_hash[:location_response]
rescue Savon::Error, Timeout::Error => e
logger.error e
yield nil
end
end