gpt4 book ai didi

ruby-on-rails - 是否可以使用 ruby​​ 访问 AdSense API?

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:34 25 4
gpt4 key购买 nike

我正在尝试使用 ruby​​ 访问 AdSense Management API。他们建议使用他们的通用 Google-API 客户端库:

http://code.google.com/p/google-api-ruby-client/#Google_AdSense_Management_API

这不是很有帮助,我遇到了错误:

Faraday conflicts in google_drive and google-api-client

我应该从哪里开始才能访问我的 AdSense 数据?

提前致谢。

最佳答案

很遗憾,我们还没有为 AdSense Management API 准备任何示例代码……还没有!不过,正如您指出的那样,客户端库是通用的,应该可以与任何较新的 Google API 一起使用,因此其他一些示例可能会有所帮助。

如果您遇到任何具体问题,请针对这些问题创建一个问题并向我指出,我会尽力提供帮助。

如果您想要快速入门示例,我可以为您制作,但我们应该确保您遇到的问题与 AdSense Management API 本身有关,而不仅仅是客户端库,作为您链接到的那个。

[编辑]这是一个基于 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

关于ruby-on-rails - 是否可以使用 ruby​​ 访问 AdSense API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11658527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com