我在获取每个视频的观看次数时遇到问题。到目前为止我得到的是:
首先,我以youtube用户身份登录并转到开发者控制台。我在那里授予了对Youtube API的访问权限。我还生成了一个带有p12文件的服务器密钥,我下载了它。
key_file = "***/***.p12"
issuer = "****@developer.gserviceaccount.com"
然后我使用ruby库启动youtube客户端。我知道我需要两个范围:
data_scope = "https://www.googleapis.com/auth/youtube"
analytics_scope = "https://www.googleapis.com/auth/yt-analytics.readonly"
最后是创建实际客户的时间
# Youtube Data and Analytics Client
$youtube_client = Google::APIClient.new(
:application_name => "Youtube Monitoring",
:application_version => "0.1")
$youtube_data_config = $youtube_client.discovered_api('youtube', 'v3')
$youtube_analytics_config = $youtube_client.discovered_api('youtubeAnalytics', 'v1')
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, 'notasecret')
$youtube_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 => [data_scope, analytics_scope],
:issuer => issuer,
:signing_key => key)
$youtube_client.authorization.fetch_access_token!
创建此客户端后,我想检索我上传的所有视频:
video_ids = $youtube_client.execute(:api_method => $youtube_data_config.search.list, :parameters => {:for_mine => true, :maxResults => 50, :part => "snippet"})
对于每个视频,我想检索它在特定时间范围内获得的观看次数。让我们看一下检索到的视频的最后一个视频:
video_id = video_ids.data.items.last.id.videoId
channel_id = video_ids.data.items.last.snippet.channelId
现在,对于这个视频,我想获得一些观看次数:
analytics = $youtube_client.execute(:api_method => $youtube_analytics_config.reports.query, :parameters => {"ids" => "channel==#{channel_id}", "filters" => "video==#{video_id}", "start-date" => "2013-01-01", "end-date" => "2014-01-10", "metrics" => "views"})
这是我遇到问题的地方。我正在接到一个禁止的电话:
body=>"{\n \"error\": {\n \"errors\": [\n {\n \"domain\": \"global\",\n \"reason\": \"forbidden\",\n \"message\": \"Forbidden\"\n }\n ],\n \"code\": 403,\n \"message\": \"Forbidden\"\n }\n}\
我很困惑为什么这不起作用。我已经尝试了很多次,但总是有相同的结果。我做错了什么?