使用youtube API v3时订单不起作用

时间:2019-03-16 13:27:42

标签: python youtube-api youtube-data-api

我正在尝试获取频道的最后10个视频。当我运行以下代码时:

from apiclient.discovery import build
API_SERVICE_NAME = "youtube"
API_VERSION = "v3"

def youtubeTest():
    KEY = "my key here"
    service = build(API_SERVICE_NAME, API_VERSION , developerKey=KEY)           
    args = {}
    args['part']='snippet'
    args['maxResults']='10'
    args['channelId']='UCq-Fj5jknLsUf-MWSy4_brA'
    args['order']='date'
    args['type']='video'
    results = service.search().list(**args).execute()

    items = results['items']
    for item in items:
        print item['snippet']['publishedAt']


youtubeTest()

这是我得到的结果

2018-03-13T10:33:45.000Z
2018-03-07T10:19:59.000Z
2017-11-22T04:30:00.000Z
2012-05-06T07:47:37.000Z 
2014-10-08T13:26:35.000Z
2017-08-10T13:39:17.000Z
2018-07-28T08:45:00.000Z
2018-12-26T05:53:46.000Z
2014-07-11T13:36:08.000Z
2018-07-12T05:30:09.000Z

我想要最后十个视频,但这不是正确的顺序。这段代码已经使用了几个月,但是最近我遇到了问题。

2 个答案:

答案 0 :(得分:4)

有关问题#128673552,https://issuetracker.google.com/issues/128673552的跟踪。

答案 1 :(得分:1)

您可以使用解决方法。取而代之的是使用search.list请求,检索上传的视频。

通过使用channel_id更改字母,如下所述:

  

channel_id:U C q-Fj5jknLsUf-MWSy4_brA

     

upload_playlist_id:U U q-Fj5jknLsUf-MWSy4_brA

使用PlaylistItems.list请求从给定频道检索上传的视频。

这是URL请求:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=<UPLOAD_PLAYLIST_ID>&fields=items(contentDetails(videoId%2CvideoPublishedAt)%2Csnippet(position%2Ctitle)%2Cstatus)&key={YOUR_API_KEY}

结果如下:

{
 "items": [
  {
   "snippet": {
    "title": "Notebook | Main Taare | Teaser | Salman Khan | Pranutan Bahl | Zaheer Iqbal | Vishal Mishra",
    "position": 0
   },
   "contentDetails": {
    "videoId": "_wXRw1BMifw",
    "videoPublishedAt": "2019-03-16T04:51:10.000Z"
   }
  },
  {
   "snippet": {
    "title": "Finito Full Song | AMAVAS | Sachiin J Joshi, Vivan, Navneet | Jubin Nautiyal, Sukriti Kakar, Ikka",
    "position": 1
   },
   "contentDetails": {
    "videoId": "dSzjNuV4R3g",
    "videoPublishedAt": "2019-03-15T19:30:00.000Z"
   }
  },
  {
   "snippet": {
    "title": "T-SERIES MIXTAPE SEASON 2 Trailer l Bhushan Kumar | Abhijit Vaghani | Ahmed Khan",
    "position": 2
   },
   "contentDetails": {
    "videoId": "5itcXsszOiA",
    "videoPublishedAt": "2019-03-15T09:27:59.000Z"
   }
  },
  {
   "snippet": {
    "title": "Bheege Bheege Full Video | AMAVAS | Sachiin J Joshi & Nargis Fakhri |  Ankit Tiwari",
    "position": 3
   },
   "contentDetails": {
    "videoId": "3pY845c95AE",
    "videoPublishedAt": "2019-03-16T11:00:01.000Z"
   }
  },
  {
   "snippet": {
    "title": "Jab Se Mera Dil Full Video | AMAVAS |Sachiin J Joshi & Nargis Fakhri |Armaan Malik,Palak Muchhal",
    "position": 4
   },
   "contentDetails": {
    "videoId": "XNPbW9BmBSo",
    "videoPublishedAt": "2019-03-15T14:00:05.000Z"
   }
  }
 ]
}

这是Google API Explorer demo,可用于指导自己。