2012-07-25 81 views

回答

3

不幸的是,我們還沒有準備AdSense Management API的任何代碼示例裏!正如你指出,雖然客戶端庫是通用的,應該與任何新的谷歌的API的工作,所以其他一些樣品中可能會有幫助。

如果你正在運行到任何具體問題,請創建專注於那些問題,指出我的話,我會盡我所能來幫助。

如果您想要快速入門,我可以爲您做好準備,但我們應確保您遇到的問題與AdSense Management API本身有關,而不僅僅是客戶端庫,就像你連接的那個一樣。

[編輯] 這裏有一個快速的樣品,根據西納特拉:

#!/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 
+0

嗨塞爾吉奧。我認爲我現在面臨的問題是法拉第衝突。不過,我會真的** **讚賞我怎麼能使用AdSense API通過渠道來獲取所有個人收入總額的任何指導的代碼。有沒有可能向我展示如何開始? – 2012-07-26 09:56:09

+2

向響應添加了一個示例。 – 2012-07-26 11:48:46

+0

非常感謝塞爾吉奧。我不想給你一個約會,但是有沒有關於Rails例子的粗略估計? – 2012-07-26 19:09:10

相關問題