- python3
$ pip3 install google-api-client
$ copy > key.json
{ "api_key" : "your api key" }
| # -*- coding: utf-8 -*- | |
| # Sample Python code for youtube.playlistItems.list | |
| # See instructions for running these code samples locally: | |
| # https://developers.google.com/explorer-help/guides/code_samples#python | |
| import os | |
| import googleapiclient.discovery | |
| import googleapiclient.errors | |
| import json | |
| scopes = ["https://www.googleapis.com/auth/youtube.readonly"] | |
| def main(): | |
| api_service_name = "youtube" | |
| api_version = "v3" | |
| json_open = open('key.json', 'r') | |
| json_load = json.load(json_open) | |
| api_key = json_load['api_key'] | |
| youtube = googleapiclient.discovery.build( | |
| api_service_name, api_version, developerKey=api_key) | |
| token='' | |
| while True: | |
| request = youtube.playlistItems().list( | |
| part="snippet", | |
| playlistId="PLTCCfaigD_8rwf7cGbMIJxsBm0iMXPVnr", | |
| maxResults=50, | |
| pageToken=token | |
| ) | |
| response = request.execute() | |
| print(response) | |
| # if exist next page | |
| if response.get('nextPageToken'): | |
| token = response['nextPageToken'] | |
| else: | |
| break | |
| if __name__ == "__main__": | |
| main() |