youtube / api-samples

Code samples for YouTube APIs, including the YouTube Data API, YouTube Analytics API, and YouTube Live Streaming API. The repo contains language-specific directories that contain the samples.
5.44k stars 2.97k forks source link

Getting View Count in youtube api V3 in search video list #28

Open irfanshehzad opened 9 years ago

irfanshehzad commented 9 years ago

I want to get videos view counts plus number of likes and dislikes Here is my code in ruby

opts = Trollop::options do opt :q, 'Search term', :type => String, :default => "#{search_params}" opt :maxResults, 'Max results', :type => :int, :default => 25 end

client = Google::APIClient.new(:key => DEVELOPER_KEY,:authorization => nil)
youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
opts[:part] = 'id,snippet,status,statistics'
opts[:pageToken] = session[:nextPageToken]

search_response = client.execute!(
  :api_method => youtube.search.list,
  :parameters => opts
)

It works fine if I set opts[:part] = 'id,snippet' but getting this error "Google::APIClient::ClientError (status)" on setting opts[:part] = 'id,snippet,status,statistics'

Please help me and tell me what should I do to get number of views , likes and dislikes in search list

Thanks in Advance

khanetor commented 8 years ago

You will need OAuth2 access. Developer_key alone does not have enough permission to get the statistics.

To make it easier to have OAuth2 auth, you should look into refreshToken. Again, I don't have the Ruby code, but here is how to create an auth object in Java and I think you can adapt easily to your project.

new GoogleCredential.Builder()
    .setJsonFactory(JSON_FACTORY)
    .setTransport(HTTP_TRANSPORT)
    .setClientSecrets(clientId, clientSecret)
    .build()
    .setRefreshToken(refreshToken)
augnustin commented 6 years ago

Where is this documented?