Sam
Sam

Reputation: 452

YouTube: Is there a way I can get the list of videos viewed by the user?

I want to build a video recommender for the user of my Android application. I have Google OAuth to login in my app. Can I get the data about the videos viewed by the user of my app on YouTube?

Upvotes: 0

Views: 98

Answers (1)

JAL
JAL

Reputation: 42489

This is a two-step process with the v3 API:

1. Hit Channels.list method with an OAuth2 request of the logged in user. Pass in contentDetails for the part value and mine = true.

Request:

HTTP GET: https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&key={YOUR_API_KEY}

Response:

{
 "kind": "youtube#channelListResponse",
 "etag": "\"xmg9xJZuZD438sF4hb-VcBBREXc/41Jk5t11dHi2JfUXrF4jZJhaQnE\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {

   "kind": "youtube#channel",
   "etag": "\"xmg9xJZuZD438sF4hb-VcBBREXc/wfaiZMzCHBlyq_ZFJU5pMR4q4sg\"",
   "id": "UCt7iVnJwjBsof8IPLJHCTgQ",
   "contentDetails": {
    "relatedPlaylists": {
     "likes": "LLt7iVnJwjBsof8IPLJHCTgQ",
     "favorites": "FLt7iVnJwjBsof8IPLJHCTgQ",
     "uploads": "UUt7iVnJwjBsof8IPLJHCTgQ",
     "watchHistory": "HLt7iVnJwjBsof8IPLJHCTgQ",
     "watchLater": "WLt7iVnJwjBsof8IPLJHCTgQ"
    },
    "googlePlusUserId": "103927393533363914063"
   }
  }
 ]
}

The object for the watchHistory key is the playlistId of your viewing history. Keep this value for use in step 2.

2. Hit the playlistItems.list method with the watchHistory playlistId from step 1.

Request:

HTTP GET: https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&id=HLt7iVnJwjBsof8IPLJHCTgQ&key={YOUR_API_KEY}

Response:

{
 "kind": "youtube#playlistItemListResponse",
 "etag": "\"xmg9xJZuZD438sF4hb-VcBBREXc/ozXLhcdE2ZWgvJu3ywqxtfQeL7o\"",
 "pageInfo": {
  "totalResults": 0,
  "resultsPerPage": 5
 },
 "items": [
 ]
}

The contents of the items array will have your viewing history.

Please note that there is a known bug with the v3 API where your history will not be returned. This has been reported internally to YouTube's team and was supposed to be fixed two years ago.

Upvotes: 2

Related Questions