如何使用YouTube API获取视频观看次数到Google Spreadsheet? [英] How to get number of video views with YouTube API into a Google Spreadsheet?

查看:151
本文介绍了如何使用YouTube API获取视频观看次数到Google Spreadsheet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都想通过YouTube API获取多少视频观看次数到Google Docs?



直到最近,我还是成功地使用了 YouTube在Google电子表格中查看次数博客文章大卫玩具



但它最近停止工作,我开始得到以下错误:


  https://gdata.youtube.com/feeds/api/videos/Prv2-9U39K8?v=2&alt=json返回码410请求失败。
截断的服务器响应:<错误xmlns ='http://schemas.google.com/g/2005'>
< error>< domain> GData< / domain>< code> NoLongerAvailableException< / code>
< internalReason>不再avai ...
(使用muteHttpExceptions选项检查完整响应)(第2行,文件Code)


有没有人遇到过这个问题?如果有的话,关于如何解决这个问题的想法会非常有帮助。



谢谢!

解决方案

现在已被弃用和关闭的API。新版本将会是:

pre $ function getYoutubeViews(videoId){
var url =https:// www。 googleapis.com/youtube/v3/videos?part=statistics&id=+ videoId;
url = url +& key = {YOUR-API-KEY};
var videoListResponse = UrlFetchApp.fetch(url);
var json = JSON.parse(videoListResponse.getContentText());
return json [items] [0] [statistics] [viewCount];

$ / code>

为了实现这个功能,您需要一个API密钥。 此处是YouTube开发人员网站上的指南,介绍如何获得密钥。一旦获得密钥,只需在上面的代码中将 {YOUR-API-KEY} 替换即可。



要从Google电子表格中调用该功能,请选择一个单元格并输入: = getYoutubeViews({YOUR-VIDEO-ID})。例如, = getYoutubeViews(IiPJsI8pl8Q)


Anyone figured out a way to get number of video views with YouTube API into Google Docs?

Up until recently I was successfully using the method outlined in the YouTube View Count in Google Spreadsheet blog post by David Toy.

But it stopped working recently and I began to get the below error:

Request failed for https://gdata.youtube.com/feeds/api/videos/Prv2-9U39K8?v=2&alt=json returned code 410.
Truncated server response: <errors xmlns='http://schemas.google.com/g/2005'>
<error><domain>GData</domain><code>NoLongerAvailableException</code>
<internalReason>No longer avai... 
(use muteHttpExceptions option to examine full response) (line 2, file "Code")

Has anyone run into this? If so any ideas on how to fix it would be really helpful.

Thanks!

解决方案

The script from that blog post uses v2 of the API which has now been deprecated and switched off. The new version would be:

function getYoutubeViews(videoId){
  var url = "https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" + videoId;
  url = url + "&key={YOUR-API-KEY}";
  var videoListResponse = UrlFetchApp.fetch(url);
  var json = JSON.parse(videoListResponse.getContentText());
  return json["items"][0]["statistics"]["viewCount"];
}

In order to make this work you will need an API key. Here is the guide on the YouTube developers site which explains how to get the key. Once you have obtained your key simply replace {YOUR-API-KEY} in the code above with it.

To call the function from a Google spreadsheet, select a cell and enter: =getYoutubeViews("{YOUR-VIDEO-ID}"). For instance, =getYoutubeViews("IiPJsI8pl8Q").

这篇关于如何使用YouTube API获取视频观看次数到Google Spreadsheet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆