[英]Is it possible to access the AdSense API using ruby?
I am trying to access the AdSense Management API using ruby. 我正在尝试使用ruby访问AdSense Management API。 They recommend using their generic Google-API client library:
他们建议使用他们的通用Google-API客户端库:
http://code.google.com/p/google-api-ruby-client/#Google_AdSense_Management_API 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 google_drive和google-api-client中的法拉第冲突
Where should I start in order to get access to my AdSense data? 我应该从哪里开始才能访问我的AdSense数据?
Thanks in advance. 提前致谢。
Unfortunately, we haven't prepared any sample code for the AdSense Management API... yet! 很遗憾,我们还没有为AdSense Management API准备任何示例代码! 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.
但是,正如您所指出的,客户端库是通用的,并且应该与任何较新的Google API一起使用,因此其他一些示例可能会有所帮助。
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. 如果您想快速开始采样,我可以为您做好准备,但我们应该确保您遇到的问题与AdSense Management API本身有关,而不仅仅是客户端库,因为你正在链接到。
[Edit] Here's a quick sample, based on Sinatra: [编辑]这是一个基于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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.