ダッシュボード(sinatraベースのフレームワーク)を使用してダッシュボードを作成しており、Analytics APIからデータを取得してダッシュボードに表示する必要があります。 だから私はこのエラーを以下のRubyコードを持っている、とのOAuthリターン:GoogleアナリティクスAPI:必須パラメータがありません:grant_type
{
"error" : "invalid_request",
"error_description" : "Required parameter is missing: grant_type"
}
ここでは、コードです:
require 'openssl'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
require 'google/api_client'
require 'date'
# Update these to match your own apps credentials
service_account_email = ENV['[email protected]'] # Email of service account
key_file = 'key.p12' # File containing your private key
key_secret = 'notasecret' # Password to unlock private key
profileID = ENV['77142386'] # Analytics profile ID.
# Get the Google API client
client = Google::APIClient.new(
:application_name => ENV['dashing project'],
:application_version => '0.01'
)
visitors = []
# Load your credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
:issuer => service_account_email,
:signing_key => key)
# Start the scheduler
SCHEDULER.every '5s', :first_in => 0 do
# Request a token for our service account
client.authorization.fetch_access_token!
# Get the analytics API
analytics = client.discovered_api('analytics','v3')
# Execute the query
visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
'ids' => "ga:" + profile_id,
'metrics' => "ga:activeVisitors",
})
visitors << { x: Time.now.to_i, y: visits.to_i }
# Update the dashboard
send_event('visitor_count_real_time', points: visitors)
end
私は「ルビーNoobの」だので、私は本当に私が何をすべき理解することはできません。あなたのコードを私のものに置き換えるにはどうしたらいいですか? 私は...私は何を削除すべきですか? – Chrez
すべてを削除する必要があります。私のように実行する必要があります。 jsonキーとGoogle APIクライアントの最新バージョンを用意してください。 – Eduardo