我一直在尝试在 ruby 中使用 dfa 报告 API(Google API)列出报告,但我不断收到“ArgumentError:缺少授权代码”。
这是我的代码,有什么建议吗?
require 'google/api_client'
require 'oauth2'
CLIENT_ID = XX
CLIENT_SECRET = XX
REDIRECT_URL = XX
SCOPE = 'https://www.googleapis.com/auth/dfareporting'
refresh_client_obj = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET, {:site => 'https://accounts.google.com', :authorize_url => '/o/oauth2/auth', :token_url => '/o/oauth2/token'})
refresh_token = OAuth2::AccessToken.new(refresh_client_obj, XX, {refresh_token: XX})
refresh_token.refresh!
puts "Oauth Access Token " + refresh_token.token
puts "Oauth Refresh Token " + refresh_token.refresh_token
client = Google::APIClient.new
client.authorization.client_id = CLIENT_ID
client.authorization.client_secret = CLIENT_SECRET
client.authorization.redirect_uri = REDIRECT_URL
client.authorization.scope = SCOPE
client.authorization.access_token = refresh_token.token
redirect_uri = client.authorization.authorization_uri
dfa = client.discovered_api('dfareporting')
result = client.execute(
:api_method => dfa.reports.list,
:parameters =>
{'profileId' => 'XX'
}
)