CINXE.COM
YouTube Reporting API - Get Bulk Data Reports | YouTube Analytics and Reporting APIs | Google for Developers
<!doctype html> <html lang="en" dir="ltr"> <head> <meta name="google-signin-client-id" content="721724668570-nbkv1cfusk7kk4eni4pjvepaus73b13t.apps.googleusercontent.com"> <meta name="google-signin-scope" content="profile email https://www.googleapis.com/auth/developerprofiles https://www.googleapis.com/auth/developerprofiles.award"> <meta property="og:site_name" content="Google for Developers"> <meta property="og:type" content="website"><meta name="theme-color" content="#ff0000"><meta charset="utf-8"> <meta content="IE=Edge" http-equiv="X-UA-Compatible"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="manifest" href="/_pwa/developers/manifest.json" crossorigin="use-credentials"> <link rel="preconnect" href="//www.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.googleapis.com" crossorigin> <link rel="preconnect" href="//apis.google.com" crossorigin> <link rel="preconnect" href="//www.google-analytics.com" crossorigin><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Google+Sans:400,500|Roboto:400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"> <link rel="stylesheet" href="//fonts.googleapis.com/css2?family=Material+Icons&family=Material+Symbols+Outlined&display=block"><link rel="stylesheet" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/css/app.css"> <link rel="shortcut icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/favicon-new.png"> <link rel="apple-touch-icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/touchicon-180-new.png"><link rel="canonical" href="https://developers.google.com/youtube/reporting/v1/reports"><link rel="search" type="application/opensearchdescription+xml" title="Google for Developers" href="https://developers.google.com/s/opensearch.xml"> <link rel="alternate" hreflang="en" href="https://developers.google.com/youtube/reporting/v1/reports" /><link rel="alternate" hreflang="x-default" href="https://developers.google.com/youtube/reporting/v1/reports" /><link rel="alternate" hreflang="ar" href="https://developers.google.com/youtube/reporting/v1/reports?hl=ar" /><link rel="alternate" hreflang="bn" href="https://developers.google.com/youtube/reporting/v1/reports?hl=bn" /><link rel="alternate" hreflang="zh-Hans" href="https://developers.google.com/youtube/reporting/v1/reports?hl=zh-cn" /><link rel="alternate" hreflang="zh-Hant" href="https://developers.google.com/youtube/reporting/v1/reports?hl=zh-tw" /><link rel="alternate" hreflang="fa" href="https://developers.google.com/youtube/reporting/v1/reports?hl=fa" /><link rel="alternate" hreflang="fr" href="https://developers.google.com/youtube/reporting/v1/reports?hl=fr" /><link rel="alternate" hreflang="de" href="https://developers.google.com/youtube/reporting/v1/reports?hl=de" /><link rel="alternate" hreflang="he" href="https://developers.google.com/youtube/reporting/v1/reports?hl=he" /><link rel="alternate" hreflang="hi" href="https://developers.google.com/youtube/reporting/v1/reports?hl=hi" /><link rel="alternate" hreflang="id" href="https://developers.google.com/youtube/reporting/v1/reports?hl=id" /><link rel="alternate" hreflang="it" href="https://developers.google.com/youtube/reporting/v1/reports?hl=it" /><link rel="alternate" hreflang="ja" href="https://developers.google.com/youtube/reporting/v1/reports?hl=ja" /><link rel="alternate" hreflang="ko" href="https://developers.google.com/youtube/reporting/v1/reports?hl=ko" /><link rel="alternate" hreflang="pl" href="https://developers.google.com/youtube/reporting/v1/reports?hl=pl" /><link rel="alternate" hreflang="pt-BR" href="https://developers.google.com/youtube/reporting/v1/reports?hl=pt-br" /><link rel="alternate" hreflang="ru" href="https://developers.google.com/youtube/reporting/v1/reports?hl=ru" /><link rel="alternate" hreflang="es-419" href="https://developers.google.com/youtube/reporting/v1/reports?hl=es-419" /><link rel="alternate" hreflang="th" href="https://developers.google.com/youtube/reporting/v1/reports?hl=th" /><link rel="alternate" hreflang="tr" href="https://developers.google.com/youtube/reporting/v1/reports?hl=tr" /><link rel="alternate" hreflang="vi" href="https://developers.google.com/youtube/reporting/v1/reports?hl=vi" /><title>YouTube Reporting API - Get Bulk Data Reports | YouTube Analytics and Reporting APIs | Google for Developers</title> <meta property="og:title" content="YouTube Reporting API - Get Bulk Data Reports | YouTube Analytics and Reporting APIs | Google for Developers"><meta property="og:url" content="https://developers.google.com/youtube/reporting/v1/reports"><meta property="og:image" content="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/opengraph/youtube-theme.png"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="675"><meta property="og:locale" content="en"><meta name="twitter:card" content="summary_large_image"><script type="application/ld+json"> { "@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{ "@type": "ListItem", "position": 1, "name": "YouTube", "item": "https://developers.google.com/youtube" },{ "@type": "ListItem", "position": 2, "name": "Analytics and Reporting APIs", "item": "https://developers.google.com/youtube/analytics" },{ "@type": "ListItem", "position": 3, "name": "YouTube Reporting API - Get Bulk Data Reports", "item": "https://developers.google.com/youtube/reporting/v1/reports" }] } </script> <link rel="stylesheet" href="/extras.css"></head> <body class="" template="page" theme="youtube-theme" type="reference" layout="docs" concierge='closed' display-toc pending> <devsite-progress type="indeterminate" id="app-progress"></devsite-progress> <section class="devsite-wrapper"> <devsite-cookie-notification-bar></devsite-cookie-notification-bar><devsite-header role="banner"> <div class="devsite-header--inner nocontent"> <div class="devsite-top-logo-row-wrapper-wrapper"> <div class="devsite-top-logo-row-wrapper"> <div class="devsite-top-logo-row"> <button type="button" id="devsite-hamburger-menu" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Navigation menu button" visually-hidden aria-label="Open menu"> </button> <div class="devsite-product-name-wrapper"> <a href="https://developers.google.com/youtube"> <div class="devsite-product-logo-container" size="medium" > <picture> <img class="devsite-product-logo" alt="YouTube" src="https://developers.google.com/static/site-assets/logo-youtube.svg" srcset=" /static/site-assets/logo-youtube.svg" sizes="64px" loading="lazy" > </picture> </div> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" aria-label="Product breadcrumb"> <li class="devsite-breadcrumb-item "> <a href="https://developers.google.com/youtube" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Upper Header" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="YouTube" > YouTube </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/youtube/analytics" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Upper Header" data-value="2" track-type="globalNav" track-name="breadcrumb" track-metadata-position="2" track-metadata-eventdetail="YouTube Analytics and Reporting APIs" > Analytics and Reporting APIs </a> </li> </ul> </span> </div> <div class="devsite-top-logo-row-middle"> <div class="devsite-header-upper-tabs"> </div> <devsite-search enable-signin enable-search enable-suggestions enable-query-completion project-name="YouTube Analytics and Reporting APIs" tenant-name="Google for Developers" project-scope="/youtube/analytics" url-scoped="https://developers.google.com/s/results/youtube/analytics" > <form class="devsite-search-form" action="https://developers.google.com/s/results" method="GET"> <div class="devsite-search-container"> <button type="button" search-open class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Open search"></button> <div class="devsite-searchbox"> <input aria-activedescendant="" aria-autocomplete="list" aria-label="Search" aria-expanded="false" aria-haspopup="listbox" autocomplete="off" class="devsite-search-field devsite-search-query" name="q" placeholder="Search" role="combobox" type="text" value="" > <div class="devsite-search-image material-icons" aria-hidden="true"> </div> <div class="devsite-search-shortcut-icon-container" aria-hidden="true"> <kbd class="devsite-search-shortcut-icon">/</kbd> </div> </div> </div> </form> <button type="button" search-close class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Close search"></button> </devsite-search> </div> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="de" >Deutsch</a> </li> <li role="presentation"> <a role="menuitem" lang="es" >Español</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="fr" >Français</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="it" >Italiano</a> </li> <li role="presentation"> <a role="menuitem" lang="pl" >Polski</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="vi" >Tiếng Việt</a> </li> <li role="presentation"> <a role="menuitem" lang="tr" >Türkçe</a> </li> <li role="presentation"> <a role="menuitem" lang="ru" >Русский</a> </li> <li role="presentation"> <a role="menuitem" lang="he" >עברית</a> </li> <li role="presentation"> <a role="menuitem" lang="ar" >العربيّة</a> </li> <li role="presentation"> <a role="menuitem" lang="fa" >فارسی</a> </li> <li role="presentation"> <a role="menuitem" lang="hi" >हिंदी</a> </li> <li role="presentation"> <a role="menuitem" lang="bn" >বাংলা</a> </li> <li role="presentation"> <a role="menuitem" lang="th" >ภาษาไทย</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_tw" >中文 – 繁體</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> <devsite-user enable-profiles fp-auth id="devsite-user"> <span class="button devsite-top-button" aria-hidden="true" visually-hidden>Sign in</span> </devsite-user> </div> </div> </div> <div class="devsite-collapsible-section "> <div class="devsite-header-background"> <div class="devsite-doc-set-nav-row"> <devsite-tabs class="lower-tabs"> <nav class="devsite-tabs-wrapper" aria-label="Lower tabs"> <tab > <a href="https://developers.google.com/youtube/analytics" track-metadata-eventdetail="https://developers.google.com/youtube/analytics" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - home" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Home" track-name="home" > Home </a> </tab> <tab > <a href="https://developers.google.com/youtube/reporting" track-metadata-eventdetail="https://developers.google.com/youtube/reporting" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - overview" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Overview" track-name="overview" > Overview </a> </tab> <tab > <a href="https://developers.google.com/youtube/reporting/guides/authorization" track-metadata-eventdetail="https://developers.google.com/youtube/reporting/guides/authorization" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - authorization" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Authorization" track-name="authorization" > Authorization </a> </tab> <tab class="devsite-active"> <a href="https://developers.google.com/youtube/reporting/v1/reports" track-metadata-eventdetail="https://developers.google.com/youtube/reporting/v1/reports" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - bulk reports" track-metadata-module="primary nav" aria-label="Bulk Reports, selected" data-category="Site-Wide Custom Events" data-label="Tab: Bulk Reports" track-name="bulk reports" > Bulk Reports </a> </tab> <tab > <a href="https://developers.google.com/youtube/analytics/data_model" track-metadata-eventdetail="https://developers.google.com/youtube/analytics/data_model" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - targeted queries" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Targeted Queries" track-name="targeted queries" > Targeted Queries </a> </tab> <tab > <a href="https://developers.google.com/youtube/reporting/v1/code_samples" track-metadata-eventdetail="https://developers.google.com/youtube/reporting/v1/code_samples" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - resources" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Resources" track-name="resources" > Resources </a> </tab> </nav> </devsite-tabs> </div> </div> </div> </div> </devsite-header> <devsite-book-nav scrollbars > <div class="devsite-book-nav-filter" > <span class="filter-list-icon material-icons" aria-hidden="true"></span> <input type="text" placeholder="Filter" aria-label="Type to filter" role="searchbox"> <span class="filter-clear-button hidden" data-title="Clear filter" aria-label="Clear filter" role="button" tabindex="0"></span> </div> <nav class="devsite-book-nav devsite-nav nocontent" aria-label="Side menu"> <div class="devsite-mobile-header"> <button type="button" id="devsite-close-nav" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Close navigation" aria-label="Close navigation"> </button> <div class="devsite-product-name-wrapper"> <a href="https://developers.google.com/youtube"> <div class="devsite-product-logo-container" size="medium" > <picture> <img class="devsite-product-logo" alt="YouTube" src="https://developers.google.com/static/site-assets/logo-youtube.svg" srcset=" /static/site-assets/logo-youtube.svg" sizes="64px" loading="lazy" > </picture> </div> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" aria-label="Upper header breadcrumb"> <li class="devsite-breadcrumb-item "> <a href="https://developers.google.com/youtube" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Upper Header" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="YouTube" > YouTube </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/youtube/analytics" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Upper Header" data-value="2" track-type="globalNav" track-name="breadcrumb" track-metadata-position="2" track-metadata-eventdetail="YouTube Analytics and Reporting APIs" > Analytics and Reporting APIs </a> </li> </ul> </span> </div> </div> <div class="devsite-book-nav-wrapper"> <div class="devsite-mobile-nav-top"> <ul class="devsite-nav-list"> <li class="devsite-nav-item"> <a href="/youtube/analytics" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Home" track-name="home" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Home" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Home </span> </a> </li> <li class="devsite-nav-item"> <a href="/youtube/reporting" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Overview" track-name="overview" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Overview" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Overview </span> </a> </li> <li class="devsite-nav-item"> <a href="/youtube/reporting/guides/authorization" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Authorization" track-name="authorization" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Authorization" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Authorization </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> <li class="devsite-nav-item"> <a href="/youtube/reporting/v1/reports" class="devsite-nav-title gc-analytics-event devsite-nav-has-children devsite-nav-active" data-category="Site-Wide Custom Events" data-label="Tab: Bulk Reports" track-name="bulk reports" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Bulk Reports" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Bulk Reports </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> <li class="devsite-nav-item"> <a href="/youtube/analytics/data_model" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Targeted Queries" track-name="targeted queries" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Targeted Queries" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Targeted Queries </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> <li class="devsite-nav-item"> <a href="/youtube/reporting/v1/code_samples" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Resources" track-name="resources" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Resources" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Resources </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> </ul> </div> <div class="devsite-mobile-nav-bottom"> <ul class="devsite-nav-list" menu="_book"> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>YouTube Reporting API</span> </div></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Bulk Data Reports for YouTube Analytics</span> </div></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports" ><span class="devsite-nav-text" tooltip>Get Bulk Data Reports</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/dimensions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/dimensions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/dimensions" ><span class="devsite-nav-text" tooltip>Dimensions</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/metrics" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/metrics" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/metrics" ><span class="devsite-nav-text" tooltip>Metrics</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/channel_reports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/channel_reports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/channel_reports" ><span class="devsite-nav-text" tooltip>Channel Reports</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/content_owner_reports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/content_owner_reports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/content_owner_reports" ><span class="devsite-nav-text" tooltip>Content Owner Reports</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>System-Managed Reports</span> </div></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/reports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/reports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/reports" ><span class="devsite-nav-text" tooltip>Overview</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed" ><span class="devsite-nav-text" tooltip>Get System-Managed Reports</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/fields" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/fields" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/fields" ><span class="devsite-nav-text" tooltip>Fields</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/financial-summaries" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/financial-summaries" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/financial-summaries" ><span class="devsite-nav-text" tooltip>Financial Summaries</span></a></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Financial Reports</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/financial-reports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/financial-reports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/financial-reports" ><span class="devsite-nav-text" tooltip>All Financial Reports</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/ads" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/ads" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/ads" ><span class="devsite-nav-text" tooltip>Ads</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/subscriptions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/subscriptions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/subscriptions" ><span class="devsite-nav-text" tooltip>Subscriptions</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/shorts" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/shorts" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/shorts" ><span class="devsite-nav-text" tooltip>Shorts</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/taxes" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/taxes" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/taxes" ><span class="devsite-nav-text" tooltip>Taxes</span></a></li></ul></div></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/videos" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/videos" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/videos" ><span class="devsite-nav-text" tooltip>Videos</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/assets" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/assets" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/assets" ><span class="devsite-nav-text" tooltip>Assets</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/references" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/references" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/references" ><span class="devsite-nav-text" tooltip>References</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/claims" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/claims" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/claims" ><span class="devsite-nav-text" tooltip>Claims</span></a></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reports/system_managed/primetime" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reports/system_managed/primetime" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reports/system_managed/primetime" ><span class="devsite-nav-text" tooltip>Primetime</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>API Reference</span> </div></li> <li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest" ><span class="devsite-nav-text" tooltip>Overview</span></a></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Jobs</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/jobs" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/jobs" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/jobs" ><span class="devsite-nav-text" tooltip>Overview</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/jobs/create" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/jobs/create" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/jobs/create" ><span class="devsite-nav-text" tooltip>create</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/jobs/delete" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/jobs/delete" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/jobs/delete" ><span class="devsite-nav-text" tooltip>delete</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/jobs/get" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/jobs/get" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/jobs/get" ><span class="devsite-nav-text" tooltip>get</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/jobs/list" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/jobs/list" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/jobs/list" ><span class="devsite-nav-text" tooltip>list</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Reports</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/jobs.reports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/jobs.reports" ><span class="devsite-nav-text" tooltip>Overview</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports/get" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/jobs.reports/get" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/jobs.reports/get" ><span class="devsite-nav-text" tooltip>get</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports/list" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/jobs.reports/list" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/jobs.reports/list" ><span class="devsite-nav-text" tooltip>list</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>ReportTypes</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/reportTypes" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/reportTypes" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/reportTypes" ><span class="devsite-nav-text" tooltip>Overview</span></a></li><li class="devsite-nav-item"><a href="/youtube/reporting/v1/reference/rest/v1/reportTypes/list" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/v1/reference/rest/v1/reportTypes/list" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/v1/reference/rest/v1/reportTypes/list" ><span class="devsite-nav-text" tooltip>list</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-break"></li> <li class="devsite-nav-item"><a href="/youtube/reporting/revision_history" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /youtube/reporting/revision_history" track-type="bookNav" track-name="click" track-metadata-eventdetail="/youtube/reporting/revision_history" ><span class="devsite-nav-text" tooltip>Revision History</span></a></li> </ul> </div> </div> </nav> </devsite-book-nav> <section id="gc-wrapper"> <main role="main" class="devsite-main-content" has-book-nav > <devsite-content> <article class="devsite-article"> <div class="devsite-article-meta nocontent" role="navigation"> <ul class="devsite-breadcrumb-list" aria-label="Breadcrumb"> <li class="devsite-breadcrumb-item "> <a href="https://developers.google.com/" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="" > Home </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/products" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="2" track-type="globalNav" track-name="breadcrumb" track-metadata-position="2" track-metadata-eventdetail="" > Products </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/youtube" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="3" track-type="globalNav" track-name="breadcrumb" track-metadata-position="3" track-metadata-eventdetail="YouTube" > YouTube </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/youtube/analytics" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="4" track-type="globalNav" track-name="breadcrumb" track-metadata-position="4" track-metadata-eventdetail="YouTube Analytics and Reporting APIs" > Analytics and Reporting APIs </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/youtube/reporting/v1/reports" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="5" track-type="globalNav" track-name="breadcrumb" track-metadata-position="5" track-metadata-eventdetail="" > Bulk Reports </a> </li> </ul> <devsite-thumb-rating position="header"> </devsite-thumb-rating> </div> <h1 class="devsite-page-title" tabindex="-1"> YouTube Reporting API - Get Bulk Data Reports </h1> <devsite-feature-tooltip ack-key="AckCollectionsBookmarkTooltipDismiss" analytics-category="Site-Wide Custom Events" analytics-action-show="Callout Profile displayed" analytics-action-close="Callout Profile dismissed" analytics-label="Create Collection Callout" class="devsite-page-bookmark-tooltip nocontent" dismiss-button="true" id="devsite-collections-dropdown" dismiss-button-text="Dismiss" close-button-text="Got it"> <devsite-bookmark></devsite-bookmark> <span slot="popout-heading"> Stay organized with collections </span> <span slot="popout-contents"> Save and categorize content based on your preferences. </span> </devsite-feature-tooltip> <div class="devsite-page-title-meta"><devsite-view-release-notes></devsite-view-release-notes></div> <devsite-toc class="devsite-nav" depth="2" devsite-toc-embedded > </devsite-toc> <div class="devsite-article-body clearfix "> <div itemscope itemtype="http://developers.google.com/ReferenceObject"> <meta itemprop="name" content="Channel Reports"/> <div class="caution"><b>Important:</b> Daily API reports and backfill reports are available for 60 days from the time that they are generated. <a href="#historical-data">Historical data</a> reports are available for 30 days from the time that they are generated.<br><br>This policy applies globally to all reports and reporting jobs. For complete details, see the YouTube Reporting API's <a href="/youtube/reporting/revision_history#may-22,-2018">revision history</a>.</div> <p>The YouTube Reporting API supports predefined reports that contain a comprehensive set of YouTube Analytics data for a channel or content owner. These reports allow you to download the bulk data sets that you can query with the <a href="/youtube/analytics/v1">YouTube Analytics API</a> or in the <a href="https://www.youtube.com/analytics">Analytics</a> section of the Creator Studio.</p> <p>The API also supports a set of automatically generated, system-managed reports that are available to content owners that have access to the corresponding reports in the <a href="https://support.google.com/youtube/answer/7648605">Reports menu</a>. Those reports contain ad revenue data and YouTube Premium subscription revenue data. See the <a href="/youtube/reporting/v1/reports/system_managed">system-managed reports</a> documentation for more information.</p> <h2 id="overview" data-text="Overview" tabindex="-1">Overview</h2> <p>Report fields in these reports are characterized as either dimensions or metrics:</p> <ul> <li><b>Dimensions</b> are common criteria that are used to aggregate data, such as the date on which an action occurred or the country where the users were located. In a report, each row of data has a unique combination of dimension values.</li> <li><b>Metrics</b> are individual measurements related to user activity, ad performance, or estimated revenue. User activity metrics include things like video view counts and ratings (likes and dislikes).</li> </ul> <p>As an example, the basic user activity report for channels contains the following dimensions:</p> <ul> <li><a href="/youtube/reporting/v1/reports/dimensions#day">day</a>: The date on which the activity occurred.</li> <li><a href="/youtube/reporting/v1/reports/dimensions#channel">channel</a>: The YouTube channel associated with the activity.</li> <li><a href="/youtube/reporting/v1/reports/dimensions#video">video</a>: The YouTube video associated with the activity.</li> <li><a href="/youtube/reporting/v1/reports/dimensions#liveOrOnDemand">liveOrOnDemand</a>: A value that indicates whether viewers were watching a live video stream.</li> <li><a href="/youtube/reporting/v1/reports/dimensions#subscribedStatus">subscribedStatus</a>: A value that indicates whether the viewers were subscribed to the channel.</li> <li><a href="/youtube/reporting/v1/reports/dimensions#country">country</a>: The country where the viewers were located.</li> </ul> <p>The report also contains a lot of metrics, such as <a href="/youtube/reporting/v1/reports/metrics#views">views</a>, <a href="/youtube/reporting/v1/reports/metrics#likes">likes</a>, and <a href="/youtube/reporting/v1/reports/metrics#averageViewDuration">averageViewDuration</a>. After retrieving and importing the report, an application could make many different calculations based on common dimension values.</p> <p>For example, to calculate the total number of views in Germany on a specific date, sum the <a href="/youtube/reporting/v1/reports/metrics#views">views</a> metric values for all rows where the <a href="/youtube/reporting/v1/reports/dimensions#country">country</a> column value is <code translate="no" dir="ltr">DE</code> and the <a href="/youtube/reporting/v1/reports/dimensions#day">day</a> column value is the date in <code translate="no" dir="ltr">YYYY-MM-DD</code> format.</p> <h2 id="retrieving-youtube-analytics-reports" data-text="Retrieving YouTube Analytics reports" tabindex="-1">Retrieving YouTube Analytics reports</h2> <p>The following steps explain how to retrieve channel and content owner reports through the API. For instructions on how to retrieve those reports, see <a href="/youtube/reporting/v1/reports/system_managed_reports">system-managed reports</a>.</p> <h3 id="step-1:-retrieve-authorization-credentials" data-text="Step 1: Retrieve authorization credentials" tabindex="-1">Step 1: Retrieve authorization credentials</h3> <p>All YouTube Reporting API requests must be authorized. To retrieve authorization tokens through the OAuth 2.0 protocol, see <a href="/youtube/reporting/guides/authorization">Authorization guide</a>.</p> <p>YouTube Reporting API requests use the following authorization scopes:</p> <table class="responsive"> <tr> <th colspan="2">Scopes</th> </tr> <tbody> <tr> <td>https://www.googleapis.com/auth/yt-analytics.readonly</td> <td>View YouTube Analytics reports for your YouTube content. This scope provides access to user activity metrics, like view counts and rating counts.</td> </tr> <tr> <td>https://www.googleapis.com/auth/yt-analytics-monetary.readonly</td> <td>View YouTube Analytics monetary reports for your YouTube content. This scope provides access to user activity metrics and to estimated revenue and ad performance metrics.</td> </tr> </table> <h3 id="step-2:-identify-the-report-to-retrieve" data-text="Step 2: Identify the report to retrieve" tabindex="-1">Step 2: Identify the report to retrieve</h3> <p>Call the API's <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/reportTypes/list">reportTypes.list</a></code> method to retrieve a list of reports that can be generated for the channel or content owner. The method returns a list of report IDs and names. Capture the <code translate="no" dir="ltr">id</code> property value for the reports that you want to have generated. For example, the ID of the basic user activity report for channels is <code translate="no" dir="ltr">channel_basic_a1</code>.</p> <p>You can also find the report names in the documentation that defines supported <a href="/youtube/reporting/v1/reports/channel_reports">channel reports</a> and <a href="/youtube/reporting/v1/reports/content_owner_reports">content owner reports</a>.</p> <h3 id="step-3:-create-a-reporting-job" data-text="Step 3: Create a reporting job" tabindex="-1">Step 3: Create a reporting job</h3> <p>YouTube does not begin to generate your report until you create a reporting job for that report. (As such, reports are only generated for the channels and content owners that actually want to retrieve them.)</p> <p>To create a reporting job, call the API's <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs/create">jobs.create</a></code> method. Set the following values in the request body:</p> <p><ul> <li>Set the <code translate="no" dir="ltr">reportTypeId</code> property's value to the report ID that you retrieved in step 2.</li> <li>Set the <code translate="no" dir="ltr">name</code> property's value to the name that you want to associate with the report.</li> </ul></p> <p>The API response to the <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs/create">jobs.create</a></code> method contains a <code translate="no" dir="ltr">Job</code> resource, which specifies the <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs#id">ID</a></code> that uniquely identifies the job. You can start retrieving the report within 48 hours of the time that the job is created, and the first available report will be for the day that you scheduled the job.</p> <p>For example, if you schedule a job on September 1, then the report for September 1 will be ready on September 3. The report for September 2 will be posted on September 4.</p> <h3 id="step-4:-retrieve-the-job-id" data-text="Step 4: Retrieve the job ID" tabindex="-1">Step 4: Retrieve the job ID</h3> <p><b>Note:</b> If your application stored the job ID returned in step 3, then you can skip this step.</p> <p>Call the <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs/list">jobs.list</a></code> method to retrieve a list of scheduled jobs. The <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs#reportTypeId">reportTypeId</a></code> property in each returned <code translate="no" dir="ltr">Job</code> resource identifies the type of report that that job generates. Your application needs the <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs#id">id</a></code> property value from the same resource in the following step.</p> <h3 id="step-5:-retrieve-the-reports-download-url" data-text="Step 5: Retrieve the report's download URL" tabindex="-1">Step 5: Retrieve the report's download URL</h3> <p>Call the <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports/list">jobs.reports.list</a></code> method to retrieve a list of reports created for the job. In the request, set the <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports/list#jobId">jobId</a></code> parameter to the job ID of the report that you want to retrieve.</p> <p class="special"><b>Tip:</b> Use the <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports/list#createdAfter">createdAfter</a></code> parameter to indicate that the API should only return reports created after a specified time. This parameter can be used to ensure that the API only returns reports that you have not already processed.</p> <p>The API response contains a list of <code translate="no" dir="ltr">Report</code> resources for that job. Each resource refers to a report that contains data for a unique 24-hour period. Note that YouTube does generate downloadable reports for days on which no data was available. Those reports contain a header row but do not contain additional data.</p> <p> <ul> <li>The resource's <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#startTime">startTime</a></code> and <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#endTime">endTime</a></code> properties identify the time period that the report's data covers.</li> <li>The resource's <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#downloadUrl">downloadUrl</a></code> property identifies the URL from which the report can be fetched.</li> <li>The resource's <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#createTime">createTime</a></code> property specifies the date and time when the report was generated. Your application should store this value and use it to determine whether previously downloaded reports have changed.</li> </ul> </p> <h3 id="step-6:-download-the-report" data-text="Step 6: Download the report" tabindex="-1">Step 6: Download the report</h3> <p>Send an HTTP GET request to the <code translate="no" dir="ltr">downloadUrl</code> obtained in step 5 to retrieve the report.</p> <p>You can reduce the bandwidth needed to download reports by enabling gzip compression on download requests. While your application will need additional CPU time to uncompress API responses, the benefit of consuming fewer network resources usually outweighs that cost.</p> <p>To receive a gzip-encoded response, set the <code translate="no" dir="ltr">Accept-Encoding</code> HTTP request header to <code translate="no" dir="ltr">gzip</code> as shown in the following example:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Text only">Accept-Encoding: gzip</pre></devsite-code> <h2 id="processing-reports" data-text="Processing reports" tabindex="-1">Processing reports</h2> <h3 id="best-practices" data-text="Best practices" tabindex="-1">Best practices</h3> <p>Applications that use the YouTube Reporting API should <em>always</em> follow these practices:</p> <ul> <li> <p>To determine the ordering of the report's columns, use the report's header row. For example, do not assume that <a href="/youtube/reporting/v1/reports/metrics#views">views</a> will be the first metric returned in a report just because it is the first metric listed in a report description. Instead, use the report's header row to determine which column contains that data.</p> </li> <li> <p>To avoid repeatedly processing the same report, keep a record of the reports that you have downloaded. The following list suggests a couple of ways to do that.</p> <ul> <li> <p>When calling the <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports/list">reports.list</a></code> method, use the <a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports/list#createdAfter">createdAfter</a> parameter to only retrieve reports created after a certain date. (Omit the <code translate="no" dir="ltr">createdAfter</code> parameter the first time you retrieve reports.)</p> <p>Each time you retrieve and successfully process reports, store the timestamp corresponding to the <a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#createTime">date and time</a> when the newest of those reports was created. Then, update the <code translate="no" dir="ltr">createdAfter</code> parameter value on each successive call to the <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports/list">reports.list</a></code> method to ensure that you are only retrieving new reports, including new reports with backfilled data, each time you call the API.</p> <p>As a safeguard, before retrieving a report, also check to ensure that the report's <a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#id">ID</a> is not already listed in your database.</p> </li> <li> <p>Store the <a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#id">ID</a> for each report that you have downloaded and processed. You can also store additional information like the <a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#createTime">date and time</a> when each report was generated or the report's <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#startTime">startTime</a></code> and <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#endTime">endTime</a></code>, which together identify the period for which the report contains data. Note that each job will likely have many reports since each report contains data for a 24-hour period.</p> <p>Use the report ID to identify reports that you still need to download and import. However, if two new reports have the same <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#startTime">startTime</a></code> and <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#endTime">endTime</a></code> property values, only import the report with the newer <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#createTime">createTime</a></code> value.</p> </li> </ul> </li> <li> <p>Reports contain IDs associated with YouTube resources. To retrieve additional metadata for those resources, use <a href="/youtube/v3/docs">YouTube Data API</a>. As noted in the <a href="/youtube/terms/developer-policies">YouTube API Services Developer Policies</a> (sections III.E.4.b through III.E.4.d), API clients must either delete or refresh stored resource metadata from that API after 30 days.</p> </li> </ul> <h3 id="report-characteristics" data-text="Report characteristics" tabindex="-1">Report characteristics</h3> <p>API reports are versioned <code translate="no" dir="ltr">.csv</code> (comma-separated values) files that have the following characteristics:</p> <ul> <li> <p>Each report contains data for a unique 24-hour period lasting from 12:00 AM through 11:59 PM Pacific Standard Time (UTC-8). As such, in any given report, the <a href="/youtube/reporting/v1/reports/dimensions#day">day</a> dimension value is always the same.</p> </li> <li> <p>Reports are updated daily.</p> </li> <li> <p>YouTube does generate downloadable reports for days on which no data was available. Those reports will contain a header row but will not contain additional data.</p> </li> <li>API reports are available for 60 days from the time that they are generated, with the exception of historical data generated for new reporting jobs. Reports are inaccessible after they are more than 60 days old. that are already more than 60 days old.</li> <li>Reports containing <a href="#historical-data">historical data</a> are available for 30 days from the time that they are generated. Reports that contain historical data are inaccessible after they are more than 30 days old.</li> <li> <p>Report data is not filtered. As such, a channel report contains all data for a channel's videos or playlists with the exception noted in the following paragraph related to deleted resources. Similarly, a content owner report contains all data for the content owner's channels (videos, playlists, ad performance, etc) with the following exception.</p> <p>Although report data is not filtered, reports do not contain any references to YouTube resources that were deleted at least 30 days prior to the date that the report was generated.</p> </li> <li> <p>Report data is not sorted.</p> </li> <li> <p>Reports omit rows that do not have metrics. In other words, rows that do not have any metrics are excluded from the report. For example, if a video has no views in Albania on a particular day, that day's report will not contain rows for Albania.</p> </li> <li> <p>Reports do not contain rows that provide summary data for metrics, such as the total number of views for all of a channel's videos. You can calculate those total values as the sum of the values in the report, but that sum might not include metrics for deleted videos, as noted above. You can also use the <a href="/youtube/analytics/data_model">YouTube Analytics API</a> to retrieve total counts. The YouTube Analytics API does return total values that include metrics for deleted resources even though those resources are not explicitly referenced in API responses.</p> </li> </ul> <h3 id="backfill-data" data-text="Backfill data" tabindex="-1">Backfill data</h3> <p>Backfill data refers to a data set that replaces a previously delivered set. When a backfill data report is available, your application should retrieve the new report and update your stored data to match the revised data set. For example, your application could delete the previous data for the time period covered in the report and then import the new data set.</p> <p>If YouTube has backfill data, it generates a new report with a new <a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#id">report ID</a>. In that case, the report's <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#startTime">startTime</a></code> and <code translate="no" dir="ltr"><a href="/youtube/reporting/v1/reference/rest/v1/jobs.reports#endTime">endTime</a></code> property values will match the start and end times of a report that was previously available and that you might have previously downloaded.</p> <p>Backfill reports do not contain any references to YouTube resources that were deleted at least 30 days prior to the date the report was generated.</p> <h3 id="historical-data" data-text="Historical data" tabindex="-1">Historical data</h3> <p>When you schedule a new reporting job, YouTube generates historical reports from that day forward, and generates reports covering the 30-day period prior to the time that you created the job. Thus, in this documentation, <b>historical data</b> refers to a report that contains data for a time period before the reporting job was scheduled.</p> <p>Historical reports are posted as soon as they are available. Typically, all of the historical data is posted for a job within a couple of days. As explained in the <a href="#report-characteristics">Report characteristics</a> section, reports containing historical data are available for 30 days from the time that they are generated. Reports that contain non-historical data are available for 60 days.</p> <h3 id="data-anonymization" data-text="Data anonymization" tabindex="-1">Data anonymization</h3> <p>To ensure the anonymity of YouTube viewers, values for some dimensions are returned only if a metric in the same row meets a certain threshold.</p> <p>For example, in the video traffic source report for channels, each row contains a number of dimensions, including <a href="/youtube/reporting/v1/reports/dimensions#Traffic_Source_Dimensions">trafficSourceType</a> and <a href="/youtube/reporting/v1/reports/dimensions#trafficSourceDetail">trafficSourceDetail</a>. Each row also contains various metrics, including <a href="/youtube/reporting/v1/reports/metrics#views">views</a>. In rows that describe traffic that originated from a YouTube search, the <a href="/youtube/reporting/v1/reports/dimensions#trafficSourceDetail">trafficSourceDetail</a> dimension identifies the search term that led to the traffic.</p> <p>In this example, the following rules apply:</p> <ul> <li> <p>The traffic source report identifies the query term (<a href="/youtube/reporting/v1/reports/dimensions#trafficSourceDetail">trafficSourceDetail</a>) only if it led to at least a certain number of views of a particular video on a particular day. In this case, <a href="/youtube/reporting/v1/reports/metrics#views">views</a> is the metric, <a href="/youtube/reporting/v1/reports/dimensions#video">video</a> is the aggregating dimension, and <a href="/youtube/reporting/v1/reports/dimensions#trafficSourceDetail">trafficSourceDetail</a> is the anonymized dimension.</li> </li> <li> <p>The report includes an additional row that aggregates metrics for all <a href="/youtube/reporting/v1/reports/dimensions#trafficSourceDetail">trafficSourceDetail</a> values that do not meet the view count threshold. That row reports the total number of views associated with those query terms but does not identify the terms themselves.</p> </li> </ul> <p>The following tables illustrate these rules. The first table contains a hypothetical set of raw data that YouTube would use to generate a traffic source report, and the second table contains the report itself. In this example, the view count threshold is 10, meaning the report only identifies a search term if it led to at least 10 views of a particular video on a particular day. (Actual thresholds are subject to change.)</p> <h4 id="raw-youtube-search-traffic-data-for-a-video" data-text="Raw YouTube search traffic data for a video" tabindex="-1">Raw YouTube search traffic data for a video</h4> <p>Assume that the data below describes YouTube search traffic to a particular video on a particular day.</p> <table> <tr> <th>search term</th> <th>views</th> <th>estimated minutes watched</th> </tr> <tr> <td>gangnam style</td> <td>100</td> <td>200</td> </tr> <tr> <td>psy</td> <td>15</td> <td>25</td> </tr> <tr> <td>psy gangnam</td> <td>9</td> <td>15</td> </tr> <tr> <td>oppa gangnam</td> <td>5</td> <td>8</td> </tr> <tr> <td>horse riding dance</td> <td>2</td> <td>5</td> </tr> </table> <h4 id="sample-traffic-source-report" data-text="Sample traffic source report" tabindex="-1">Sample traffic source report</h4> <p>The following table shows an excerpt from the traffic source report that YouTube would generate for the raw data in the preceding section. (The actual report would contain more dimensions and metrics.) In this example, the report identifies search terms only if they led to at least 10 views. Actual thresholds are subject to change.</p> <p>In the report's third row, the <code translate="no" dir="ltr">trafficSourceDetail</code> dimension value is <code translate="no" dir="ltr">NULL</code>. The <code translate="no" dir="ltr">views</code> and <code translate="no" dir="ltr">estimatedMinutesWatched</code> metrics contain the combined views and minutes watched for the three search terms that generated fewer than 10 views.</p> <table> <tr> <th>trafficSourceDetail</th> <th>views</th> <th>estimatedMinutesWatched</th> </tr> <tr> <td>gangnam style</td> <td>100</td> <td>200</td> </tr> <tr> <td>psy</td> <td>15</td> <td>25</td> </tr> <tr> <td>NULL</td> <td>16</td> <td>28</td> </tr> </table> <h4 id="dimensions-subject-to-anonymization" data-text="Dimensions subject to anonymization" tabindex="-1">Dimensions subject to anonymization</h4> <p>The following table identifies dimension values that are anonymized if associated metric values do not meet a certain threshold. In each case, the metric's value is aggregated over another dimension. For example, if the metric is <a href="/youtube/reporting/v1/reports/metrics#views">views</a> and the aggregating dimension is <a href="/youtube/reporting/v1/reports/dimensions#video">video</a>, then the dimension value is anonymized unless the video was viewed a certain number of times.</p> <table> <tr> <th>Metric</th> <th>Aggregating dimension(s)</th> <th>Anonymized dimension</th> <th>Anonymized value</th> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#subscribersGained">subscribersGained</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#channel">channel</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#country">country</a></td> <td><code translate="no" dir="ltr">ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#subscribersGained">subscribersGained</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#channel">channel</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#province">province</a></td> <td><code translate="no" dir="ltr">US-ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#subscribersLost">subscribersLost</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#channel">channel</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#country">country</a></td> <td><code translate="no" dir="ltr">ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#subscribersLost">subscribersLost</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#channel">channel</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#province">province</a></td> <td><code translate="no" dir="ltr">US-ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#comments">comments</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#video">video</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#country">country</a></td> <td><code translate="no" dir="ltr">ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#comments">comments</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#video">video</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#province">province</a></td> <td><code translate="no" dir="ltr">US-ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#likes">likes</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#video">video</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#country">country</a></td> <td><code translate="no" dir="ltr">ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#likes">likes</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#video">video</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#province">province</a></td> <td><code translate="no" dir="ltr">US-ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#dislikes">dislikes</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#video">video</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#country">country</a></td> <td><code translate="no" dir="ltr">ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#dislikes">dislikes</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#video">video</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#province">province</a></td> <td><code translate="no" dir="ltr">US-ZZ</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#views">views</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#video">video</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#ageGroup">ageGroup</a></td> <td><code translate="no" dir="ltr">NULL</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#views">views</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#video">video</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#gender">gender</a></td> <td><code translate="no" dir="ltr">NULL</code></td> </tr> <tr> <td><a href="/youtube/reporting/v1/reports/metrics#views">views</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#video">video</a> and <a href="/youtube/reporting/v1/reports/dimensions#trafficSourceDetail">trafficSourceDetail</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#trafficSourceDetail">trafficSourceDetail</a></td> <td><code translate="no" dir="ltr">NULL</code></td> </tr> <tr> <td>Number of subscribers to channel</td> <td><a href="/youtube/reporting/v1/reports/dimensions#channel">channel</a></td> <td><a href="/youtube/reporting/v1/reports/dimensions#subscribedStatus">subscribedStatus</a></td> <td><code translate="no" dir="ltr">NULL</code></td> </tr> </table> <h2 id="code-samples" data-text="Code samples" tabindex="-1">Code samples</h2> <p>The following code samples demonstrate how to use the API to create a reporting job and then retrieve a report for that job. Two code samples are provided for each language:</p> <ol> <li> <p>The first code sample shows how to retrieve a list of available report types and then create a new reporting job.</p> </li> <li> <p>The second code sample shows how to retrieve a report for a particular job. You can begin retrieving reports within 48 hours of the time that the job is created.</p> </li> </ol> <p class="note"><strong>Note:</strong> The following code samples may not represent all supported programming languages. For the list of supported languages, see <a href="/youtube/v3/libraries">client libraries</a>.</p> <div id="code-sample" class="ds-selector-tabs" data-ds-scope="code-sample"> <section> <h3 id="java" data-text="Java" tabindex="-1">Java</h3> <p>The following samples use the <a target="_blank" href="https://code.google.com/p/google-api-java-client/">Java client library</a>:</p> <ul> <li><a href="#java-example-create-reporting-job">Example 1: Create a reporting job</a></li> <li><a href="#java-example-retrieve-report">Example 2: Retrieve a report</a></li> </ul> <p id="java-example-create-reporting-job"><b>Example 1: Create a reporting job</b></p> <p>The following code sample calls the <code translate="no" dir="ltr">reportTypes.list</code> method to retrieve a list of available report types. It then calls the <code translate="no" dir="ltr">jobs.create</code> method to create a new reporting job.</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><span class="devsite-syntax-cm">/*</span> <span class="devsite-syntax-cm"> * Copyright (c) 2015 Google Inc.</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except</span> <span class="devsite-syntax-cm"> * in compliance with the License. You may obtain a copy of the License at</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * http://www.apache.org/licenses/LICENSE-2.0</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * Unless required by applicable law or agreed to in writing, software distributed under the License</span> <span class="devsite-syntax-cm"> * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express</span> <span class="devsite-syntax-cm"> * or implied. See the License for the specific language governing permissions and limitations under</span> <span class="devsite-syntax-cm"> * the License.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-kn">package</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.samples.youtube.cmdline.reporting</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.auth.oauth2.Credential</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.googleapis.json.GoogleJsonResponseException</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.samples.youtube.cmdline.Auth</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.YouTubeReporting</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.model.Job</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.model.ListReportTypesResponse</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.model.ReportType</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.common.collect.Lists</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.BufferedReader</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.IOException</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.InputStreamReader</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.List</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * This sample creates a reporting job by:</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * 1. Listing the available report types using the "reportTypes.list" method.</span> <span class="devsite-syntax-cm"> * 2. Creating a reporting job using the "jobs.create" method.</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @author Ibrahim Ulukaya</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">class</span> <span class="devsite-syntax-nc">CreateReportingJob</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Define a global instance of a YouTube Reporting object, which will be used to make</span> <span class="devsite-syntax-cm"> * YouTube Reporting API requests.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">YouTubeReporting</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">youtubeReporting</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Create a reporting job.</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @param args command line args (not used).</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">main</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-o">[]</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/*</span> <span class="devsite-syntax-cm"> * This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for</span> <span class="devsite-syntax-cm"> * authenticated user's account. Any request that retrieves earnings or ad performance metrics must</span> <span class="devsite-syntax-cm"> * use this scope.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">List<String></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">scopes</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Lists</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newArrayList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"https://www.googleapis.com/auth/yt-analytics-monetary.readonly"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Authorize the request.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Auth</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">authorize</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">scopes</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"createreportingjob"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// This object is used to make YouTube Reporting API requests.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">youtubeReporting</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">YouTubeReporting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Auth</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">HTTP_TRANSPORT</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Auth</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">JSON_FACTORY</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setApplicationName</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"youtube-cmdline-createreportingjob-sample"</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Prompt the user to specify the name of the job to be created.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">getNameFromUser</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">listReportTypes</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">createReportingJob</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">getReportTypeIdFromUser</span><span class="devsite-syntax-p">(),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GoogleJsonResponseException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">err</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"GoogleJsonResponseException code: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getDetails</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">getCode</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">" : "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getDetails</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">getMessage</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">err</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"IOException: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getMessage</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">err</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Throwable: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getMessage</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Lists report types. (reportTypes.listReportTypes)</span> <span class="devsite-syntax-cm"> * @return true if at least one report type exists</span> <span class="devsite-syntax-cm"> * @throws IOException</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">boolean</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">listReportTypes</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Call the YouTube Reporting API's reportTypes.list method to retrieve report types.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ListReportTypesResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportTypesListResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">youtubeReporting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">reportTypes</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">list</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">execute</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">List<ReportType></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportTypeList</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportTypesListResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getReportTypes</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">reportTypeList</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">||</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportTypeList</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">isEmpty</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"No report types found."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">else</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Print information from the API response.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"\n================== Report Types ==================\n"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">ReportType</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportType</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportTypeList</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Id: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Name: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getName</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"\n-------------------------------------------------------------\n"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">true</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Creates a reporting job. (jobs.create)</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @param reportTypeId Id of the job's report type.</span> <span class="devsite-syntax-cm"> * @param name name of the job.</span> <span class="devsite-syntax-cm"> * @throws IOException</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">createReportingJob</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportTypeId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Create a reporting job with a name and a report type id.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Job</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">job</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Job</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">job</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setReportTypeId</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">reportTypeId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">job</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setName</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Call the YouTube Reporting API's jobs.create method to create a job.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Job</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">createdJob</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">youtubeReporting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">jobs</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">create</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">job</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">execute</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Print information from the API response.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"\n================== Created reporting job ==================\n"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - ID: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">createdJob</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Name: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">createdJob</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getName</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Report Type Id: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">createdJob</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getReportTypeId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Create Time: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">createdJob</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getCreateTime</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"\n-------------------------------------------------------------\n"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/*</span> <span class="devsite-syntax-cm"> * Prompt the user to enter a name for the job. Then return the name.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">getNameFromUser</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">""</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Please enter the name for the job [javaTestJob]: "</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BufferedReader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bReader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BufferedReader</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">InputStreamReader</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">in</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bReader</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">readLine</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">length</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> < </span><span class="devsite-syntax-mi">1</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// If nothing is entered, defaults to "javaTestJob".</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"javaTestJob"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"You chose "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">" as the name for the job."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/*</span> <span class="devsite-syntax-cm"> * Prompt the user to enter a report type id for the job. Then return the id.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">getReportTypeIdFromUser</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">id</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">""</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Please enter the reportTypeId for the job: "</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BufferedReader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bReader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BufferedReader</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">InputStreamReader</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">in</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">id</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bReader</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">readLine</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"You chose "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">id</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">" as the report type Id for the job."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">id</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span> <div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/youtube/api-samples/blob/07263305b59a7c3275bc7e925f9ce6cabf774022/java/src/main/java/com/google/api/services/samples/youtube/cmdline/reporting/CreateReportingJob.java" class="gc-analytics-event" data-category="github_link" data-label="youtube/api-samples/java/src/main/java/com/google/api/services/samples/youtube/cmdline/reporting/CreateReportingJob.java" data-code-snippet="true" data-github-path="youtube/api-samples/java/src/main/java/com/google/api/services/samples/youtube/cmdline/reporting/CreateReportingJob.java"><span class="devsite-syntax-n">CreateReportingJob</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></pre></devsite-code> <p id="java-example-retrieve-report"><b>Example 2: Retrieve a report</b></p> <p>The code sample calls the <code translate="no" dir="ltr">jobs.list</code> method to retrieve a list of reporting jobs. It then calls the <code translate="no" dir="ltr">reports.list</code> method with the <code translate="no" dir="ltr">jobId</code> parameter set to a specific job ID to retrieve reports created by that job. Finally, the sample prints out the download URL for each report.</li></ul> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><span class="devsite-syntax-cm">/*</span> <span class="devsite-syntax-cm"> * Copyright (c) 2015 Google Inc.</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except</span> <span class="devsite-syntax-cm"> * in compliance with the License. You may obtain a copy of the License at</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * http://www.apache.org/licenses/LICENSE-2.0</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * Unless required by applicable law or agreed to in writing, software distributed under the License</span> <span class="devsite-syntax-cm"> * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express</span> <span class="devsite-syntax-cm"> * or implied. See the License for the specific language governing permissions and limitations under</span> <span class="devsite-syntax-cm"> * the License.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-kn">package</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.samples.youtube.cmdline.reporting</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.auth.oauth2.Credential</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.googleapis.json.GoogleJsonResponseException</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.http.GenericUrl</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.samples.youtube.cmdline.Auth</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.YouTubeReporting</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.YouTubeReporting.Media.Download</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.model.Job</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.model.ListJobsResponse</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.model.ListReportsResponse</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.youtubereporting.model.Report</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.common.collect.Lists</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.BufferedReader</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.ByteArrayOutputStream</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.File</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.FileOutputStream</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.IOException</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.InputStreamReader</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.List</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">javax.print.attribute.standard.Media</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * This sample retrieves reports created by a specific job by:</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * 1. Listing the jobs using the "jobs.list" method.</span> <span class="devsite-syntax-cm"> * 2. Retrieving reports using the "reports.list" method.</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @author Ibrahim Ulukaya</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">class</span> <span class="devsite-syntax-nc">RetrieveReports</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Define a global instance of a YouTube Reporting object, which will be used to make</span> <span class="devsite-syntax-cm"> * YouTube Reporting API requests.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">YouTubeReporting</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">youtubeReporting</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Retrieve reports.</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @param args command line args (not used).</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">main</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-o">[]</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/*</span> <span class="devsite-syntax-cm"> * This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for</span> <span class="devsite-syntax-cm"> * authenticated user's account. Any request that retrieves earnings or ad performance metrics must</span> <span class="devsite-syntax-cm"> * use this scope.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">List<String></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">scopes</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Lists</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newArrayList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"https://www.googleapis.com/auth/yt-analytics-monetary.readonly"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Authorize the request.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Auth</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">authorize</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">scopes</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"retrievereports"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// This object is used to make YouTube Reporting API requests.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">youtubeReporting</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">YouTubeReporting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Auth</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">HTTP_TRANSPORT</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Auth</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">JSON_FACTORY</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setApplicationName</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"youtube-cmdline-retrievereports-sample"</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">listReportingJobs</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">retrieveReports</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">getJobIdFromUser</span><span class="devsite-syntax-p">()))</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">downloadReport</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">getReportUrlFromUser</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GoogleJsonResponseException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">err</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"GoogleJsonResponseException code: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getDetails</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">getCode</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">" : "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getDetails</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">getMessage</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">err</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"IOException: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getMessage</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">err</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Throwable: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getMessage</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Lists reporting jobs. (jobs.listJobs)</span> <span class="devsite-syntax-cm"> * @return true if at least one reporting job exists</span> <span class="devsite-syntax-cm"> * @throws IOException</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">boolean</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">listReportingJobs</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Call the YouTube Reporting API's jobs.list method to retrieve reporting jobs.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ListJobsResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">jobsListResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">youtubeReporting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">jobs</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">list</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">execute</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">List<Job></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">jobsList</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">jobsListResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getJobs</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">jobsList</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">||</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">jobsList</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">isEmpty</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"No jobs found."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">else</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Print information from the API response.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"\n================== Reporting Jobs ==================\n"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Job</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">job</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">jobsList</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Id: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">job</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Name: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">job</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getName</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Report Type Id: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">job</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getReportTypeId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"\n-------------------------------------------------------------\n"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">true</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Lists reports created by a specific job. (reports.listJobsReports)</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @param jobId The ID of the job.</span> <span class="devsite-syntax-cm"> * @throws IOException</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">boolean</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">retrieveReports</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">jobId</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Call the YouTube Reporting API's reports.list method</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// to retrieve reports created by a job.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ListReportsResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportsListResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">youtubeReporting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">jobs</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">reports</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">list</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">jobId</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">execute</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">List<Report></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportslist</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportsListResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getReports</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">reportslist</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">||</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportslist</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">isEmpty</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"No reports found."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">else</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Print information from the API response.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"\n============= Reports for the job "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">jobId</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">" =============\n"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Report</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">report</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportslist</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Id: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">report</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - From: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">report</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getStartTime</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - To: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">report</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getEndTime</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">" - Download Url: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">report</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getDownloadUrl</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"\n-------------------------------------------------------------\n"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">true</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Download the report specified by the URL. (media.download)</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @param reportUrl The URL of the report to be downloaded.</span> <span class="devsite-syntax-cm"> * @throws IOException</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">boolean</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">downloadReport</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reportUrl</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Call the YouTube Reporting API's media.download method to download a report.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Download</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">request</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">youtubeReporting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">media</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">download</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">""</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FileOutputStream</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">fop</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FileOutputStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">File</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"report"</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">request</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getMediaHttpDownloader</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">download</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenericUrl</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">reportUrl</span><span class="devsite-syntax-p">),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">fop</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">true</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/*</span> <span class="devsite-syntax-cm"> * Prompt the user to enter a job id for report retrieval. Then return the id.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">getJobIdFromUser</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">id</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">""</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Please enter the job id for the report retrieval: "</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BufferedReader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bReader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BufferedReader</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">InputStreamReader</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">in</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">id</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bReader</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">readLine</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"You chose "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">id</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">" as the job Id for the report retrieval."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">id</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/*</span> <span class="devsite-syntax-cm"> * Prompt the user to enter a URL for report download. Then return the URL.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">getReportUrlFromUser</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">url</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">""</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Please enter the report URL to download: "</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BufferedReader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bReader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BufferedReader</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">InputStreamReader</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">in</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">url</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bReader</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">readLine</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"You chose "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">url</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">" as the URL to download."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">url</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}}</span> <div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/youtube/api-samples/blob/07263305b59a7c3275bc7e925f9ce6cabf774022/java/src/main/java/com/google/api/services/samples/youtube/cmdline/reporting/RetrieveReports.java" class="gc-analytics-event" data-category="github_link" data-label="youtube/api-samples/java/src/main/java/com/google/api/services/samples/youtube/cmdline/reporting/RetrieveReports.java" data-code-snippet="true" data-github-path="youtube/api-samples/java/src/main/java/com/google/api/services/samples/youtube/cmdline/reporting/RetrieveReports.java"><span class="devsite-syntax-n">RetrieveReports</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></pre></devsite-code> </section> <section> <h3 id="php" data-text="PHP" tabindex="-1">PHP</h3> <p>The following samples use the <a target="_blank" href="https://github.com/googleapis/google-api-php-client">PHP client library</a>.</p> <ul> <li><a href="#php-example-create-reporting-job">Example 1: Create a reporting job</a></li> <li><a href="#php-example-retrieve-report">Example 2: Retrieve a report</a></li> </ul> <p id="php-example-create-reporting-job"><b>Example 1: Create a reporting job</b></p> <p>The following code sample calls the <code translate="no" dir="ltr">reportTypes.list</code> method to retrieve a list of available report types. It then calls the <code translate="no" dir="ltr">jobs.create</code> method to create a new reporting job.</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="PHP"><<span class="devsite-syntax-x">?php</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * This sample creates a reporting job by:</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * 1. Listing the available report types using the "reportTypes.list" method.</span> <span class="devsite-syntax-x"> * 2. Creating a reporting job using the "jobs.create" method.</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * @author Ibrahim Ulukaya</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Library Requirements</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * 1. Install composer (https://getcomposer.org)</span> <span class="devsite-syntax-x"> * 2. On the command line, change to this directory (api-samples/php)</span> <span class="devsite-syntax-x"> * 3. Require the google/apiclient library</span> <span class="devsite-syntax-x"> * $ composer require google/apiclient:~2.0</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">if (!file_exists(__DIR__ . '/vendor/autoload.php')) {</span> <span class="devsite-syntax-x"> throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">require_once __DIR__ . '/vendor/autoload.php';</span> <span class="devsite-syntax-x">session_start();</span> <span class="devsite-syntax-x">/*</span> <span class="devsite-syntax-x"> * You can acquire an OAuth 2.0 client ID and client secret from the</span> <span class="devsite-syntax-x"> * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}</span>> <span class="devsite-syntax-x"> * For more information about using OAuth 2.0 to access Google APIs, please see:</span> <span class="devsite-syntax-x"> * <https://developers.google.com/youtube/v3/guides/authentication></span> <span class="devsite-syntax-x"> * Please ensure that you have enabled the YouTube Data API for your project.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">$OAUTH2_CLIENT_ID = 'REPLACE_ME';</span> <span class="devsite-syntax-x">$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';</span> <span class="devsite-syntax-x">$client = new Google_Client();</span> <span class="devsite-syntax-x">$client->setClientId($OAUTH2_CLIENT_ID);</span> <span class="devsite-syntax-x">$client->setClientSecret($OAUTH2_CLIENT_SECRET);</span> <span class="devsite-syntax-x">/*</span> <span class="devsite-syntax-x"> * This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for</span> <span class="devsite-syntax-x"> * authenticated user's account. Any request that retrieves earnings or ad performance metrics must</span> <span class="devsite-syntax-x"> * use this scope.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">$client->setScopes('https://www.googleapis.com/auth/yt-analytics-monetary.readonly');</span> <span class="devsite-syntax-x">$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],</span> <span class="devsite-syntax-x"> FILTER_SANITIZE_URL);</span> <span class="devsite-syntax-x">$client->setRedirectUri($redirect);</span> <span class="devsite-syntax-x">// YouTube Reporting object used to make YouTube Reporting API requests.</span> <span class="devsite-syntax-x">$youtubeReporting = new Google_Service_YouTubeReporting($client);</span> <span class="devsite-syntax-x">// Check if an auth token exists for the required scopes</span> <span class="devsite-syntax-x">$tokenSessionKey = 'token-' . $client->prepareScopes();</span> <span class="devsite-syntax-x">if (isset($_GET['code'])) {</span> <span class="devsite-syntax-x"> if (strval($_SESSION['state']) !== strval($_GET['state'])) {</span> <span class="devsite-syntax-x"> die('The session state did not match.');</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> $client->authenticate($_GET['code']);</span> <span class="devsite-syntax-x"> $_SESSION[$tokenSessionKey] = $client->getAccessToken();</span> <span class="devsite-syntax-x"> header('Location: ' . $redirect);</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">if (isset($_SESSION[$tokenSessionKey])) {</span> <span class="devsite-syntax-x"> $client->setAccessToken($_SESSION[$tokenSessionKey]);</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">// Check to ensure that the access token was successfully acquired.</span> <span class="devsite-syntax-x">if ($client->getAccessToken()) {</span> <span class="devsite-syntax-x"> // This code executes if the user enters a name in the form</span> <span class="devsite-syntax-x"> // and submits the form. Otherwise, the page displays the form above.</span> <span class="devsite-syntax-x"> try {</span> <span class="devsite-syntax-x"> if (empty(listReportTypes($youtubeReporting, $htmlBody))) {</span> <span class="devsite-syntax-x"> $htmlBody .= sprintf('<p>No report types found.</p>');</span> <span class="devsite-syntax-x"> } else if ($_GET['reportTypeId']){</span> <span class="devsite-syntax-x"> createReportingJob($youtubeReporting, $_GET['reportTypeId'], $_GET['jobName'], $htmlBody);</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> } catch (Google_Service_Exception $e) {</span> <span class="devsite-syntax-x"> $htmlBody = sprintf('<p>A service error occurred: <code>%s</code></p>',</span> <span class="devsite-syntax-x"> htmlspecialchars($e->getMessage()));</span> <span class="devsite-syntax-x"> } catch (Google_Exception $e) {</span> <span class="devsite-syntax-x"> $htmlBody = sprintf('<p>An client error occurred: <code>%s</code></p>',</span> <span class="devsite-syntax-x"> htmlspecialchars($e->getMessage()));</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> $_SESSION[$tokenSessionKey] = $client->getAccessToken();</span> <span class="devsite-syntax-x">} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {</span> <span class="devsite-syntax-x"> $htmlBody = <<<END</span> <span class="devsite-syntax-x"> <h3>Client Credentials Required</h3></span> <span class="devsite-syntax-x"> <p></span> <span class="devsite-syntax-x"> You need to set <code>\$OAUTH2_CLIENT_ID</code> and</span> <span class="devsite-syntax-x"> <code>\$OAUTH2_CLIENT_ID</code> before proceeding.</span> <span class="devsite-syntax-x"> <p></span> <span class="devsite-syntax-x">END;</span> <span class="devsite-syntax-x">} else {</span> <span class="devsite-syntax-x"> // If the user hasn't authorized the app, initiate the OAuth flow</span> <span class="devsite-syntax-x"> $state = mt_rand();</span> <span class="devsite-syntax-x"> $client->setState($state);</span> <span class="devsite-syntax-x"> $_SESSION['state'] = $state;</span> <span class="devsite-syntax-x"> $authUrl = $client->createAuthUrl();</span> <span class="devsite-syntax-x"> $htmlBody = <<<END</span> <span class="devsite-syntax-x"> <h3>Authorization Required</h3></span> <span class="devsite-syntax-x"> <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p></span> <span class="devsite-syntax-x">END;</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Creates a reporting job. (jobs.create)</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.</span> <span class="devsite-syntax-x"> * @param string $reportTypeId Id of the job's report type.</span> <span class="devsite-syntax-x"> * @param string $name name of the job.</span> <span class="devsite-syntax-x"> * @param $htmlBody - html body.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function createReportingJob(Google_Service_YouTubeReporting $youtubeReporting, $reportTypeId,</span> <span class="devsite-syntax-x"> $name, &$htmlBody) {</span> <span class="devsite-syntax-x"> # Create a reporting job with a name and a report type id.</span> <span class="devsite-syntax-x"> $reportingJob = new Google_Service_YouTubeReporting_Job();</span> <span class="devsite-syntax-x"> $reportingJob->setReportTypeId($reportTypeId);</span> <span class="devsite-syntax-x"> $reportingJob->setName($name);</span> <span class="devsite-syntax-x"> // Call the YouTube Reporting API's jobs.create method to create a job.</span> <span class="devsite-syntax-x"> $jobCreateResponse = $youtubeReporting->jobs->create($reportingJob);</span> <span class="devsite-syntax-x"> $htmlBody .= "<h2>Created reporting job</h2><ul>";</span> <span class="devsite-syntax-x"> $htmlBody .= sprintf('<li>"%s" for reporting type "%s" at "%s"</li>',</span> <span class="devsite-syntax-x"> $jobCreateResponse['name'], $jobCreateResponse['reportTypeId'], $jobCreateResponse['createTime']);</span> <span class="devsite-syntax-x"> $htmlBody .= '</ul>';</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Returns a list of report types. (reportTypes.listReportTypes)</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.</span> <span class="devsite-syntax-x"> * @param $htmlBody - html body.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function listReportTypes(Google_Service_YouTubeReporting $youtubeReporting, &$htmlBody) {</span> <span class="devsite-syntax-x"> // Call the YouTube Reporting API's reportTypes.list method to retrieve report types.</span> <span class="devsite-syntax-x"> $reportTypes = $youtubeReporting->reportTypes->listReportTypes();</span> <span class="devsite-syntax-x"> $htmlBody .= "<h3>Report Types</h3><ul>";</span> <span class="devsite-syntax-x"> foreach ($reportTypes as $reportType) {</span> <span class="devsite-syntax-x"> $htmlBody .= sprintf('<li>id: "%s", name: "%s"</li>', $reportType['id'], $reportType['name']);</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> $htmlBody .= '</ul>';</span> <span class="devsite-syntax-x"> return $reportTypes;</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">?</span>> <<span class="devsite-syntax-x">!doctype html</span>> <<span class="devsite-syntax-x">html</span>> <<span class="devsite-syntax-x">head</span>> <<span class="devsite-syntax-x">title>Create a reporting job</title></span> <<span class="devsite-syntax-x">/head</span>> <<span class="devsite-syntax-x">body</span>> <span class="devsite-syntax-x"> <form method="GET"></span> <span class="devsite-syntax-x"> <div></span> <span class="devsite-syntax-x"> Job Name: <input type="text" id="jobName" name="jobName" placeholder="Enter Job Name"></span> <span class="devsite-syntax-x"> </div></span> <span class="devsite-syntax-x"> <br></span> <span class="devsite-syntax-x"> <div></span> <span class="devsite-syntax-x"> Report Type Id: <input type="text" id="reportTypeId" name="reportTypeId" placeholder="Enter Report Type Id"></span> <span class="devsite-syntax-x"> </div></span> <span class="devsite-syntax-x"> <br></span> <span class="devsite-syntax-x"> <input type="submit" value="Create!"></span> <span class="devsite-syntax-x"> </form></span> <span class="devsite-syntax-x"> <?=$htmlBody?></span> <<span class="devsite-syntax-x">/body</span>> <<span class="devsite-syntax-x">/html</span>> <div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/youtube/api-samples/blob/07263305b59a7c3275bc7e925f9ce6cabf774022/php/create_reporting_job.php" class="gc-analytics-event" data-category="github_link" data-label="youtube/api-samples/php/create_reporting_job.php" data-code-snippet="true" data-github-path="youtube/api-samples/php/create_reporting_job.php"><span class="devsite-syntax-x">create_reporting_job.php</span></a></div></pre></devsite-code> <p id="php-example-retrieve-report"><b>Example 2: Retrieve a report</b></p> <p>The code sample calls the <code translate="no" dir="ltr">jobs.list</code> method to retrieve a list of reporting jobs. It then calls the <code translate="no" dir="ltr">reports.list</code> method with the <code translate="no" dir="ltr">jobId</code> parameter set to a specific job ID to retrieve reports created by that job. Finally, the sample prints out the download URL for each report.</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="PHP"><<span class="devsite-syntax-x">?php</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * This sample supports the following use cases:</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * 1. Retrieve reporting jobs by content owner:</span> <span class="devsite-syntax-x"> * Ex: php retrieve_reports.php --contentOwner=="CONTENT_OWNER_ID"</span> <span class="devsite-syntax-x"> * Ex: php retrieve_reports.php --contentOwner=="CONTENT_OWNER_ID" --includeSystemManaged==True</span> <span class="devsite-syntax-x"> * 2. Retrieving list of downloadable reports for a particular job:</span> <span class="devsite-syntax-x"> * Ex: php retrieve_reports.php --contentOwner=="CONTENT_OWNER_ID" --jobId="JOB_ID"</span> <span class="devsite-syntax-x"> * 3. Download a report:</span> <span class="devsite-syntax-x"> * Ex: php retrieve_reports.php --contentOwner=="CONTENT_OWNER_ID" --downloadUrl="DOWNLOAD_URL" --outputFile="report.txt"</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Library Requirements</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * 1. Install composer (https://getcomposer.org)</span> <span class="devsite-syntax-x"> * 2. On the command line, change to this directory (api-samples/php)</span> <span class="devsite-syntax-x"> * 3. Require the google/apiclient library</span> <span class="devsite-syntax-x"> * $ composer require google/apiclient:~2.0</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">if (!file_exists(__DIR__ . '/vendor/autoload.php')) {</span> <span class="devsite-syntax-x"> throw new \Exception('please run "composer require google/apiclient:~2.2.0" in "' . __DIR__ .'"');</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">require_once __DIR__ . '/vendor/autoload.php';</span> <span class="devsite-syntax-x">session_start();</span> <span class="devsite-syntax-x">define('CREDENTIALS_PATH', '~/.credentials/youtube-php.json');</span> <span class="devsite-syntax-x">$longOptions = array(</span> <span class="devsite-syntax-x"> 'contentOwner::',</span> <span class="devsite-syntax-x"> 'downloadUrl::',</span> <span class="devsite-syntax-x"> 'includeSystemManaged::',</span> <span class="devsite-syntax-x"> 'jobId::',</span> <span class="devsite-syntax-x"> 'outputFile::',</span> <span class="devsite-syntax-x">);</span> <span class="devsite-syntax-x">$options = getopt('', $longOptions);</span> <span class="devsite-syntax-x">$CONTENT_OWNER_ID = ($options['contentOwner'] ? $options['contentOwner'] : '');</span> <span class="devsite-syntax-x">$DOWNLOAD_URL = (array_key_exists('downloadUrl', $options) ?</span> <span class="devsite-syntax-x"> $options['downloadUrl'] : '');</span> <span class="devsite-syntax-x">$INCLUDE_SYSTEM_MANAGED = (array_key_exists('includeSystemManaged', $options) ?</span> <span class="devsite-syntax-x"> $options['includeSystemManaged'] : '');</span> <span class="devsite-syntax-x">$JOB_ID = (array_key_exists('jobId', $options) ? $options['jobId'] : '');</span> <span class="devsite-syntax-x">$OUTPUT_FILE = (array_key_exists('outputFile', $options) ?</span> <span class="devsite-syntax-x"> $options['outputFile'] : '');</span> <span class="devsite-syntax-x">/*</span> <span class="devsite-syntax-x"> * You can obtain an OAuth 2.0 client ID and client secret from the</span> <span class="devsite-syntax-x"> * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}</span>> <span class="devsite-syntax-x"> * For more information about using OAuth 2.0 to access Google APIs, please see:</span> <span class="devsite-syntax-x"> * <https://developers.google.com/youtube/v3/guides/authentication></span> <span class="devsite-syntax-x"> * Please ensure that you have enabled the YouTube Data API for your project.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function getClient() {</span> <span class="devsite-syntax-x"> $client = new Google_Client();</span> <span class="devsite-syntax-x"> $client->setAuthConfigFile('client_secrets_php.json');</span> <span class="devsite-syntax-x"> $client->addScope(</span> <span class="devsite-syntax-x"> 'https://www.googleapis.com/auth/yt-analytics-monetary.readonly');</span> <span class="devsite-syntax-x"> $client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');</span> <span class="devsite-syntax-x"> $client->setAccessType('offline');</span> <span class="devsite-syntax-x"> // Load previously authorized credentials from a file.</span> <span class="devsite-syntax-x"> $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);</span> <span class="devsite-syntax-x"> if (file_exists($credentialsPath)) {</span> <span class="devsite-syntax-x"> $accessToken = json_decode(file_get_contents($credentialsPath), true);</span> <span class="devsite-syntax-x"> } else {</span> <span class="devsite-syntax-x"> // Request authorization from the user.</span> <span class="devsite-syntax-x"> $authUrl = $client->createAuthUrl();</span> <span class="devsite-syntax-x"> printf('Open the following link in your browser:\n%s\n', $authUrl);</span> <span class="devsite-syntax-x"> print 'Enter verification code: ';</span> <span class="devsite-syntax-x"> $authCode = trim(fgets(STDIN));</span> <span class="devsite-syntax-x"> // Exchange authorization code for an access token.</span> <span class="devsite-syntax-x"> $accessToken = $client->authenticate($authCode);</span> <span class="devsite-syntax-x"> $refreshToken = $client->getRefreshToken();</span> <span class="devsite-syntax-x"> // Store the credentials to disk.</span> <span class="devsite-syntax-x"> if(!file_exists(dirname($credentialsPath))) {</span> <span class="devsite-syntax-x"> mkdir(dirname($credentialsPath), 0700, true);</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> file_put_contents($credentialsPath, json_encode($accessToken));</span> <span class="devsite-syntax-x"> printf('Credentials saved to %s\n', $credentialsPath);</span> <span class="devsite-syntax-x"> //fclose($fp);</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> $client->setAccessToken($accessToken);</span> <span class="devsite-syntax-x"> // Refresh the token if it's expired.</span> <span class="devsite-syntax-x"> if ($client->isAccessTokenExpired()) {</span> <span class="devsite-syntax-x"> $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());</span> <span class="devsite-syntax-x"> file_put_contents($credentialsPath, json_encode($client->getAccessToken()));</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> return $client;</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Expands the home directory alias '~' to the full path.</span> <span class="devsite-syntax-x"> * @param string $path the path to expand.</span> <span class="devsite-syntax-x"> * @return string the expanded path.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function expandHomeDirectory($path) {</span> <span class="devsite-syntax-x"> $homeDirectory = getenv('HOME');</span> <span class="devsite-syntax-x"> if (empty($homeDirectory)) {</span> <span class="devsite-syntax-x"> $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> return str_replace('~', realpath($homeDirectory), $path);</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Returns a list of reporting jobs. (jobs.listJobs)</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.</span> <span class="devsite-syntax-x"> * @param string $onBehalfOfContentOwner A content owner ID.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function listReportingJobs(Google_Service_YouTubeReporting $youtubeReporting,</span> <span class="devsite-syntax-x"> $onBehalfOfContentOwner = '', $includeSystemManaged = False) {</span> <span class="devsite-syntax-x"> $reportingJobs = $youtubeReporting->jobs->listJobs(</span> <span class="devsite-syntax-x"> array('onBehalfOfContentOwner' => $onBehalfOfContentOwner,</span> <span class="devsite-syntax-x"> 'includeSystemManaged' => $includeSystemManaged));</span> <span class="devsite-syntax-x"> print ('REPORTING JOBS' . PHP_EOL . '**************' . PHP_EOL);</span> <span class="devsite-syntax-x"> foreach ($reportingJobs as $job) {</span> <span class="devsite-syntax-x"> print($job['reportTypeId'] . ':' . $job['id'] . PHP_EOL);</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> print(PHP_EOL);</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Lists reports created by a specific job. (reports.listJobsReports)</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.</span> <span class="devsite-syntax-x"> * @param string $jobId The ID of the job.</span> <span class="devsite-syntax-x"> * @param string $onBehalfOfContentOwner A content owner ID.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function listReportsForJob(Google_Service_YouTubeReporting $youtubeReporting,</span> <span class="devsite-syntax-x"> $jobId, $onBehalfOfContentOwner = '') {</span> <span class="devsite-syntax-x"> $reports = $youtubeReporting->jobs_reports->listJobsReports($jobId,</span> <span class="devsite-syntax-x"> array('onBehalfOfContentOwner' => $onBehalfOfContentOwner));</span> <span class="devsite-syntax-x"> print ('DOWNLOADABLE REPORTS' . PHP_EOL . '********************' . PHP_EOL);</span> <span class="devsite-syntax-x"> foreach ($reports['reports'] as $report) {</span> <span class="devsite-syntax-x"> print('Created: ' . date('d M Y', strtotime($report['createTime'])) .</span> <span class="devsite-syntax-x"> ' (' . date('d M Y', strtotime($report['startTime'])) .</span> <span class="devsite-syntax-x"> ' to ' . date('d M Y', strtotime($report['endTime'])) . ')' .</span> <span class="devsite-syntax-x"> PHP_EOL . ' ' . $report['downloadUrl'] . PHP_EOL . PHP_EOL);</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Download the report specified by the URL. (media.download)</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.</span> <span class="devsite-syntax-x"> * @param string $reportUrl The URL of the report to be downloaded.</span> <span class="devsite-syntax-x"> * @param string $outputFile The file to write the report to locally.</span> <span class="devsite-syntax-x"> * @param $htmlBody - html body.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function downloadReport(Google_Service_YouTubeReporting $youtubeReporting,</span> <span class="devsite-syntax-x"> $reportUrl, $outputFile) {</span> <span class="devsite-syntax-x"> $client = $youtubeReporting->getClient();</span> <span class="devsite-syntax-x"> // Setting the defer flag to true tells the client to return a request that</span> <span class="devsite-syntax-x"> // can be called with ->execute(); instead of making the API call immediately.</span> <span class="devsite-syntax-x"> $client->setDefer(true);</span> <span class="devsite-syntax-x"> // Call YouTube Reporting API's media.download method to download a report.</span> <span class="devsite-syntax-x"> $request = $youtubeReporting->media->download('', array('alt' => 'media'));</span> <span class="devsite-syntax-x"> $request = $request->withUri(new \GuzzleHttp\Psr7\Uri($reportUrl));</span> <span class="devsite-syntax-x"> $responseBody = '';</span> <span class="devsite-syntax-x"> try {</span> <span class="devsite-syntax-x"> $response = $client->execute($request);</span> <span class="devsite-syntax-x"> $responseBody = $response->getBody();</span> <span class="devsite-syntax-x"> } catch (Google_Service_Exception $e) {</span> <span class="devsite-syntax-x"> $responseBody = $e->getTrace()[0]['args'][0]->getResponseBody();</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> file_put_contents($outputFile, $responseBody);</span> <span class="devsite-syntax-x"> $client->setDefer(false);</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">// Define an object that will be used to make all API requests.</span> <span class="devsite-syntax-x">$client = getClient();</span> <span class="devsite-syntax-x">// YouTube Reporting object used to make YouTube Reporting API requests.</span> <span class="devsite-syntax-x">$youtubeReporting = new Google_Service_YouTubeReporting($client);</span> <span class="devsite-syntax-x">if ($CONTENT_OWNER_ID) {</span> <span class="devsite-syntax-x"> if (!$DOWNLOAD_URL && !$JOB_ID) {</span> <span class="devsite-syntax-x"> listReportingJobs($youtubeReporting, $CONTENT_OWNER_ID,</span> <span class="devsite-syntax-x"> $INCLUDE_SYSTEM_MANAGED);</span> <span class="devsite-syntax-x"> } else if ($JOB_ID) {</span> <span class="devsite-syntax-x"> listReportsForJob($youtubeReporting, $JOB_ID, $CONTENT_OWNER_ID);</span> <span class="devsite-syntax-x"> } else if ($DOWNLOAD_URL && $OUTPUT_FILE) {</span> <span class="devsite-syntax-x"> downloadReport($youtubeReporting, $DOWNLOAD_URL, $OUTPUT_FILE);</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">?</span>> <div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/youtube/api-samples/blob/07263305b59a7c3275bc7e925f9ce6cabf774022/php/retrieve_reports.php" class="gc-analytics-event" data-category="github_link" data-label="youtube/api-samples/php/retrieve_reports.php" data-code-snippet="true" data-github-path="youtube/api-samples/php/retrieve_reports.php"><span class="devsite-syntax-x">retrieve_reports.php</span></a></div></pre></devsite-code> </section> <section> <h3 id="python" data-text="Python" tabindex="-1">Python</h3> <p>The following samples use the <a target="_blank" href="https://developers.google.com/api-client-library/python/">Python client library</a>.</p> <ul> <li><a href="#python-example-create-reporting-job">Example 1: Create a reporting job</a></li> <li><a href="#python-example-retrieve-report">Example 2: Retrieve a report</a></li> </ul> <p id="python-example-create-reporting-job"><b>Example 1: Create a reporting job</b></p> <p>The following code sample calls the <code translate="no" dir="ltr">reportTypes.list</code> method to retrieve a list of available report types. It then calls the <code translate="no" dir="ltr">jobs.create</code> method to create a new reporting job.</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Python"><span class="devsite-syntax-ch">#!/usr/bin/python</span> <span class="devsite-syntax-c1"># Create a reporting job for the authenticated user's channel or</span> <span class="devsite-syntax-c1"># for a content owner that the user's account is linked to.</span> <span class="devsite-syntax-c1"># Usage example:</span> <span class="devsite-syntax-c1"># python create_reporting_job.py --name='<name>'</span> <span class="devsite-syntax-c1"># python create_reporting_job.py --content-owner='<CONTENT OWNER ID>'</span> <span class="devsite-syntax-c1"># python create_reporting_job.py --content-owner='<CONTENT_OWNER_ID>' --report-type='<REPORT_TYPE_ID>' --name='<REPORT_NAME>'</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">argparse</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">os</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">google.oauth2.credentials</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">google_auth_oauthlib.flow</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">googleapiclient.discovery</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">build</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">googleapiclient.errors</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">HttpError</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">google_auth_oauthlib.flow</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">InstalledAppFlow</span> <span class="devsite-syntax-c1"># The CLIENT_SECRETS_FILE variable specifies the name of a file that contains</span> <span class="devsite-syntax-c1"># the OAuth 2.0 information for this application, including its client_id and</span> <span class="devsite-syntax-c1"># client_secret. You can acquire an OAuth 2.0 client ID and client secret from</span> <span class="devsite-syntax-c1"># the {{ Google Cloud Console }} at</span> <span class="devsite-syntax-c1"># {{ https://cloud.google.com/console }}.</span> <span class="devsite-syntax-c1"># Please ensure that you have enabled the YouTube Data API for your project.</span> <span class="devsite-syntax-c1"># For more information about using OAuth2 to access the YouTube Data API, see:</span> <span class="devsite-syntax-c1"># https://developers.google.com/youtube/v3/guides/authentication</span> <span class="devsite-syntax-c1"># For more information about the client_secrets.json file format, see:</span> <span class="devsite-syntax-c1"># https://developers.google.com/api-client-library/python/guide/aaa_client_secrets</span> <span class="devsite-syntax-n">CLIENT_SECRETS_FILE</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s1">'client_secret.json'</span> <span class="devsite-syntax-c1"># This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for</span> <span class="devsite-syntax-c1"># authenticated user's account. Any request that retrieves earnings or ad performance metrics must</span> <span class="devsite-syntax-c1"># use this scope.</span> <span class="devsite-syntax-n">SCOPES</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'https://www.googleapis.com/auth/yt-analytics-monetary.readonly'</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-n">API_SERVICE_NAME</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s1">'youtubereporting'</span> <span class="devsite-syntax-n">API_VERSION</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s1">'v1'</span> <span class="devsite-syntax-c1"># Authorize the request and store authorization credentials.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">get_authenticated_service</span><span class="devsite-syntax-p">():</span> <span class="devsite-syntax-n">flow</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">InstalledAppFlow</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">from_client_secrets_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">CLIENT_SECRETS_FILE</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">SCOPES</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">flow</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">run_console</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">build</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">API_SERVICE_NAME</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">API_VERSION</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">credentials</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Remove keyword arguments that are not set.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">remove_empty_kwargs</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-n">good_kwargs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">{}</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">kwargs</span> <span class="devsite-syntax-ow">is</span> <span class="devsite-syntax-ow">not</span> <span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">key</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">value</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">iteritems</span><span class="devsite-syntax-p">():</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">value</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">good_kwargs</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">key</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">value</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">good_kwargs</span> <span class="devsite-syntax-c1"># Call the YouTube Reporting API's reportTypes.list method to retrieve report types.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">list_report_types</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-c1"># Provide keyword arguments that have values as request parameters.</span> <span class="devsite-syntax-n">kwargs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">remove_empty_kwargs</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">results</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">reportTypes</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">list</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">execute</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">reportTypes</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">results</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'reportTypes'</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-s1">'reportTypes'</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">results</span> <span class="devsite-syntax-ow">and</span> <span class="devsite-syntax-n">results</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'reportTypes'</span><span class="devsite-syntax-p">]:</span> <span class="devsite-syntax-n">reportTypes</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">results</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'reportTypes'</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">reportType</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">reportTypes</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-s1">'Report type id: </span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s1"> name: </span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s1">'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">reportType</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'id'</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">reportType</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'name'</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-k">else</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-s1">'No report types found'</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-kc">False</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-kc">True</span> <span class="devsite-syntax-c1"># Call the YouTube Reporting API's jobs.create method to create a job.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">create_reporting_job</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">report_type_id</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-c1"># Provide keyword arguments that have values as request parameters.</span> <span class="devsite-syntax-n">kwargs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">remove_empty_kwargs</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">reporting_job</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">jobs</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">create</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">body</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nb">dict</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">reportTypeId</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">report_type</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">name</span> <span class="devsite-syntax-p">),</span> <span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span> <span class="devsite-syntax-p">)</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">execute</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Reporting job "</span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1">" created for reporting type "</span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1">" at "</span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1">"'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">reporting_job</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'name'</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">reporting_job</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'reportTypeId'</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">reporting_job</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'createTime'</span><span class="devsite-syntax-p">]))</span> <span class="devsite-syntax-c1"># Prompt the user to enter a report type id for the job. Then return the id.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">get_report_type_id_from_user</span><span class="devsite-syntax-p">():</span> <span class="devsite-syntax-n">report_type_id</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">raw_input</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Please enter the reportTypeId for the job: '</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'You chose "</span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1">" as the report type Id for the job.'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-n">report_type_id</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">report_type_id</span> <span class="devsite-syntax-c1"># Prompt the user to set a job name</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">prompt_user_to_set_job_name</span><span class="devsite-syntax-p">():</span> <span class="devsite-syntax-n">job_name</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">raw_input</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Please set a name for the job: '</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Great! "</span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1">" is a memorable name for this job.'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-n">job_name</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">job_name</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-vm">__name__</span> <span class="devsite-syntax-o">==</span> <span class="devsite-syntax-s1">'__main__'</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">parser</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">argparse</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">ArgumentParser</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># The 'name' option specifies the name that will be used for the reporting job.</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">add_argument</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'--content-owner'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">default</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">''</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">help</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'ID of content owner for which you are retrieving jobs and reports.'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">add_argument</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'--include-system-managed'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">default</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">False</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">help</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'Whether the API response should include system-managed reports'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">add_argument</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'--name'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">default</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">''</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">help</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'Name for the reporting job. The script prompts you to set a name '</span> <span class="devsite-syntax-o">+</span> <span class="devsite-syntax-s1">'for the job if you do not provide one using this argument.'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">add_argument</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'--report-type'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">default</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">help</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'The type of report for which you are creating a job.'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">args</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">parse_args</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">youtube_reporting</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">get_authenticated_service</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-k">try</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-c1"># Prompt user to select report type if they didn't set one on command line.</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-ow">not</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">report_type</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">list_report_types</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">onBehalfOfContentOwner</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">content_owner</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">includeSystemManaged</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">include_system_managed</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">report_type</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">get_report_type_id_from_user</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Prompt user to set job name if not set on command line.</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-ow">not</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">name</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">prompt_user_to_set_job_name</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Create the job.</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">report_type</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">create_reporting_job</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">onBehalfOfContentOwner</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">content_owner</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">except</span> <span class="devsite-syntax-n">HttpError</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-s1">'An HTTP error </span><span class="devsite-syntax-si">%d</span><span class="devsite-syntax-s1"> occurred:</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1">'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">resp</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">status</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">e</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">)</span> <div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/youtube/api-samples/blob/07263305b59a7c3275bc7e925f9ce6cabf774022/python/create_reporting_job.py" class="gc-analytics-event" data-category="github_link" data-label="youtube/api-samples/python/create_reporting_job.py" data-code-snippet="true" data-github-path="youtube/api-samples/python/create_reporting_job.py"><span class="devsite-syntax-n">create_reporting_job</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></pre></devsite-code> <p id="python-example-retrieve-report"><b>Example 2: Retrieve a report</b></p> <p>The code sample calls the <code translate="no" dir="ltr">jobs.list</code> method to retrieve a list of reporting jobs. It then calls the <code translate="no" dir="ltr">reports.list</code> method with the <code translate="no" dir="ltr">jobId</code> parameter set to a specific job ID to retrieve reports created by that job. Finally, the sample prints out the download URL for each report.</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Python"><span class="devsite-syntax-ch">#!/usr/bin/python</span> <span class="devsite-syntax-c1">###</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># This script retrieves YouTube Reporting API reports. Use cases:</span> <span class="devsite-syntax-c1"># 1. If you specify a report URL, the script downloads that report.</span> <span class="devsite-syntax-c1"># 2. Otherwise, if you specify a job ID, the script retrieves a list of</span> <span class="devsite-syntax-c1"># available reports for that job and prompts you to select a report.</span> <span class="devsite-syntax-c1"># Then it retrieves that report as in case 1.</span> <span class="devsite-syntax-c1"># 3. Otherwise, the list retrieves a list of jobs for the user or,</span> <span class="devsite-syntax-c1"># if specified, the content owner that the user is acting on behalf of.</span> <span class="devsite-syntax-c1"># Then it prompts the user to select a job, and then executes case 2 and</span> <span class="devsite-syntax-c1"># then case 1.</span> <span class="devsite-syntax-c1"># Usage examples:</span> <span class="devsite-syntax-c1"># python retrieve_reports.py --content_owner_id=<CONTENT_OWNER_ID> --local_file=<LOCAL_FILE></span> <span class="devsite-syntax-c1"># python retrieve_reports.py --content_owner_id=<CONTENT_OWNER_ID> --job_id=<JOB_ID> --local_file=<LOCAL_FILE></span> <span class="devsite-syntax-c1"># python retrieve_reports.py --content_owner_id=<CONTENT_OWNER_ID> --report_url=<REPORT_URL> --local_file=<LOCAL_FILE></span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1">###</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">argparse</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">os</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">google.oauth2.credentials</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">google_auth_oauthlib.flow</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">googleapiclient.discovery</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">build</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">googleapiclient.errors</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">HttpError</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">googleapiclient.http</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">MediaIoBaseDownload</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">google_auth_oauthlib.flow</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">InstalledAppFlow</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">io</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">FileIO</span> <span class="devsite-syntax-c1"># The CLIENT_SECRETS_FILE variable specifies the name of a file that contains</span> <span class="devsite-syntax-c1"># the OAuth 2.0 information for this application, including its client_id and</span> <span class="devsite-syntax-c1"># client_secret. You can acquire an OAuth 2.0 client ID and client secret from</span> <span class="devsite-syntax-c1"># the {{ Google Cloud Console }} at</span> <span class="devsite-syntax-c1"># {{ https://cloud.google.com/console }}.</span> <span class="devsite-syntax-c1"># Please ensure that you have enabled the YouTube Data API for your project.</span> <span class="devsite-syntax-c1"># For more information about using OAuth2 to access the YouTube Data API, see:</span> <span class="devsite-syntax-c1"># https://developers.google.com/youtube/v3/guides/authentication</span> <span class="devsite-syntax-c1"># For more information about the client_secrets.json file format, see:</span> <span class="devsite-syntax-c1"># https://developers.google.com/api-client-library/python/guide/aaa_client_secrets</span> <span class="devsite-syntax-n">CLIENT_SECRETS_FILE</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s1">'client_secret.json'</span> <span class="devsite-syntax-c1"># This OAuth 2.0 access scope allows for read access to YouTube Analytics</span> <span class="devsite-syntax-c1"># monetary reports for the authenticated user's account. Any request that</span> <span class="devsite-syntax-c1"># retrieves earnings or ad performance metrics must use this scope.</span> <span class="devsite-syntax-n">SCOPES</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'https://www.googleapis.com/auth/yt-analytics-monetary.readonly'</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-n">API_SERVICE_NAME</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s1">'youtubereporting'</span> <span class="devsite-syntax-n">API_VERSION</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s1">'v1'</span> <span class="devsite-syntax-c1"># Authorize the request and store authorization credentials.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">get_authenticated_service</span><span class="devsite-syntax-p">():</span> <span class="devsite-syntax-n">flow</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">InstalledAppFlow</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">from_client_secrets_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">CLIENT_SECRETS_FILE</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">SCOPES</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">flow</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">run_console</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">build</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">API_SERVICE_NAME</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">API_VERSION</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">credentials</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Remove keyword arguments that are not set.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">remove_empty_kwargs</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-n">good_kwargs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">{}</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">kwargs</span> <span class="devsite-syntax-ow">is</span> <span class="devsite-syntax-ow">not</span> <span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">key</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">value</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">iteritems</span><span class="devsite-syntax-p">():</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">value</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">good_kwargs</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">key</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">value</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">good_kwargs</span> <span class="devsite-syntax-c1"># Call the YouTube Reporting API's jobs.list method to retrieve reporting jobs.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">list_reporting_jobs</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-c1"># Only include the onBehalfOfContentOwner keyword argument if the user</span> <span class="devsite-syntax-c1"># set a value for the --content_owner argument.</span> <span class="devsite-syntax-n">kwargs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">remove_empty_kwargs</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Retrieve the reporting jobs for the user (or content owner).</span> <span class="devsite-syntax-n">results</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">jobs</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">list</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">execute</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-s1">'jobs'</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">results</span> <span class="devsite-syntax-ow">and</span> <span class="devsite-syntax-n">results</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'jobs'</span><span class="devsite-syntax-p">]:</span> <span class="devsite-syntax-n">jobs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">results</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'jobs'</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">job</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">jobs</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Reporting job id: </span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s1"> name: </span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s1"> for reporting type: </span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s1">'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">job</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'id'</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">job</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'name'</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">job</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'reportTypeId'</span><span class="devsite-syntax-p">]))</span> <span class="devsite-syntax-k">else</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-s1">'No jobs found'</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-kc">False</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-kc">True</span> <span class="devsite-syntax-c1"># Call the YouTube Reporting API's reports.list method to retrieve reports created by a job.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">retrieve_reports</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-c1"># Only include the onBehalfOfContentOwner keyword argument if the user</span> <span class="devsite-syntax-c1"># set a value for the --content_owner argument.</span> <span class="devsite-syntax-n">kwargs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">remove_empty_kwargs</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Retrieve available reports for the selected job.</span> <span class="devsite-syntax-n">results</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">jobs</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">reports</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">list</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-o">**</span><span class="devsite-syntax-n">kwargs</span> <span class="devsite-syntax-p">)</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">execute</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-s1">'reports'</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">results</span> <span class="devsite-syntax-ow">and</span> <span class="devsite-syntax-n">results</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'reports'</span><span class="devsite-syntax-p">]:</span> <span class="devsite-syntax-n">reports</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">results</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'reports'</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">report</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">reports</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Report dates: </span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1"> to </span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s1"> download URL: </span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s1">'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">report</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'startTime'</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">report</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'endTime'</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">report</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'downloadUrl'</span><span class="devsite-syntax-p">]))</span> <span class="devsite-syntax-c1"># Call the YouTube Reporting API's media.download method to download the report.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">download_report</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">report_url</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">local_file</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-n">request</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">media</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">download</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">resourceName</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">' '</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">request</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">uri</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">report_url</span> <span class="devsite-syntax-n">fh</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">FileIO</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">local_file</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">mode</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'wb'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Stream/download the report in a single request.</span> <span class="devsite-syntax-n">downloader</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">MediaIoBaseDownload</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">fh</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">request</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">chunksize</span><span class="devsite-syntax-o">=-</span><span class="devsite-syntax-mi">1</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">done</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-kc">False</span> <span class="devsite-syntax-k">while</span> <span class="devsite-syntax-n">done</span> <span class="devsite-syntax-ow">is</span> <span class="devsite-syntax-kc">False</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">status</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">done</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">downloader</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">next_chunk</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">status</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-s1">'Download </span><span class="devsite-syntax-si">%d%%</span><span class="devsite-syntax-s1">.'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-nb">int</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">status</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">progress</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-mi">100</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-s1">'Download Complete!'</span> <span class="devsite-syntax-c1"># Prompt the user to select a job and return the specified ID.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">get_job_id_from_user</span><span class="devsite-syntax-p">():</span> <span class="devsite-syntax-n">job_id</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">raw_input</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Please enter the job id for the report retrieval: '</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'You chose "</span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1">" as the job Id for the report retrieval.'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-n">job_id</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">job_id</span> <span class="devsite-syntax-c1"># Prompt the user to select a report URL and return the specified URL.</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">get_report_url_from_user</span><span class="devsite-syntax-p">():</span> <span class="devsite-syntax-n">report_url</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">raw_input</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Please enter the report URL to download: '</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'You chose "</span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1">" to download.'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-n">report_url</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">report_url</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-vm">__name__</span> <span class="devsite-syntax-o">==</span> <span class="devsite-syntax-s1">'__main__'</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">parser</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">argparse</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">ArgumentParser</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">add_argument</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'--content_owner'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">default</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">''</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">help</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'ID of content owner for which you are retrieving jobs and reports'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">add_argument</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'--job_id'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">default</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">help</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'ID of the job for which you are retrieving reports. If not '</span> <span class="devsite-syntax-o">+</span> <span class="devsite-syntax-s1">'provided AND report_url is also not provided, then the script '</span> <span class="devsite-syntax-o">+</span> <span class="devsite-syntax-s1">'calls jobs.list() to retrieve a list of jobs.'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">add_argument</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'--report_url'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">default</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">help</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'URL of the report to retrieve. If not specified, the script '</span> <span class="devsite-syntax-o">+</span> <span class="devsite-syntax-s1">'calls reports.list() to retrieve a list of reports for the '</span> <span class="devsite-syntax-o">+</span> <span class="devsite-syntax-s1">'selected job.'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">add_argument</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'--local_file'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">default</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'yt_report.txt'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">help</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s1">'The name of the local file where the downloaded report will be written.'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">args</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">parse_args</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">youtube_reporting</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">get_authenticated_service</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-k">try</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-c1"># If the user has not specified a job ID or report URL, retrieve a list</span> <span class="devsite-syntax-c1"># of available jobs and prompt the user to select one.</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-ow">not</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">job_id</span> <span class="devsite-syntax-ow">and</span> <span class="devsite-syntax-ow">not</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">report_url</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">list_reporting_jobs</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">onBehalfOfContentOwner</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">content_owner</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">job_id</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">get_job_id_from_user</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># If the user has not specified a report URL, retrieve a list of reports</span> <span class="devsite-syntax-c1"># available for the specified job and prompt the user to select one.</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">job_id</span> <span class="devsite-syntax-ow">and</span> <span class="devsite-syntax-ow">not</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">report_url</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">retrieve_reports</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">jobId</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">job_id</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">onBehalfOfContentOwner</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">content_owner</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">report_url</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">get_report_url_from_user</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Download the selected report.</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">report_url</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">download_report</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">youtube_reporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">report_url</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">args</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">local_file</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">except</span> <span class="devsite-syntax-n">HttpError</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span> <span class="devsite-syntax-s1">'An HTTP error </span><span class="devsite-syntax-si">%d</span><span class="devsite-syntax-s1"> occurred:</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-si">%s</span><span class="devsite-syntax-s1">'</span> <span class="devsite-syntax-o">%</span> <span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">resp</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">status</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">e</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">)</span> <div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/youtube/api-samples/blob/07263305b59a7c3275bc7e925f9ce6cabf774022/python/retrieve_reports.py" class="gc-analytics-event" data-category="github_link" data-label="youtube/api-samples/python/retrieve_reports.py" data-code-snippet="true" data-github-path="youtube/api-samples/python/retrieve_reports.py"><span class="devsite-syntax-n">retrieve_reports</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></pre></devsite-code> </section> </div> </div> </div> <devsite-recommendations display="in-page" hidden yield> </devsite-recommendations> <devsite-thumb-rating position="footer"> </devsite-thumb-rating> <devsite-recommendations id="recommendations-link" yield></devsite-recommendations> <div class="devsite-floating-action-buttons"> </div> </article> <devsite-content-footer class="nocontent"> <p>Except as otherwise noted, the content of this page is licensed under the <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 License</a>, and code samples are licensed under the <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache 2.0 License</a>. For details, see the <a href="https://developers.google.com/site-policies">Google Developers Site Policies</a>. Java is a registered trademark of Oracle and/or its affiliates.</p> <p>Last updated 2024-11-04 UTC.</p> </devsite-content-footer> <devsite-notification > </devsite-notification> <div class="devsite-content-data"> <template class="devsite-content-data-template"> [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-11-04 UTC."],[],[]] </template> </div> </devsite-content> </main> <devsite-footer-promos class="devsite-footer"> <nav class="devsite-footer-promos nocontent" aria-label="Promotions"> <ul class="devsite-footer-promos-list"> <li class="devsite-footer-promo"> <a href="https://blog.youtube" class="devsite-footer-promo-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Promo Link (index 1)" > <picture> <img class="devsite-footer-promo-icon" src="/static/site-assets/logo-youtube.svg" loading="lazy" alt="Blog"> </picture> <span class="devsite-footer-promo-label"> Blog </span> </a> <div class="devsite-footer-promo-description">The latest news on the YouTube blog</div> </li> <li class="devsite-footer-promo"> <a href="https://github.com/youtube/api-samples" class="devsite-footer-promo-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Promo Link (index 2)" > <picture> <img class="devsite-footer-promo-icon" src="/static/site-assets/logo-github.svg" loading="lazy" alt="GitHub"> </picture> <span class="devsite-footer-promo-label"> GitHub </span> </a> <div class="devsite-footer-promo-description">Find API code samples and other YouTube open-source projects.</div> </li> <li class="devsite-footer-promo"> <a href="https://issuetracker.google.com/issues/new?component=186600&template=874803" class="devsite-footer-promo-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Promo Link (index 3)" > <picture> <img class="devsite-footer-promo-icon" src="/static/site-assets/developers_64dp.png" loading="lazy" alt="Issue Tracker"> </picture> <span class="devsite-footer-promo-label"> Issue Tracker </span> </a> <div class="devsite-footer-promo-description">Something wrong? Send us a bug report!</div> </li> <li class="devsite-footer-promo"> <a href="http://stackoverflow.com/questions/ask?tags=youtube-api" class="devsite-footer-promo-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Promo Link (index 4)" > <picture> <img class="devsite-footer-promo-icon" src="/static/site-assets/logo-stack-overflow.svg" loading="lazy" alt="Stack Overflow"> </picture> <span class="devsite-footer-promo-label"> Stack Overflow </span> </a> <div class="devsite-footer-promo-description">Ask a question under the youtube-api tag</div> </li> <li class="devsite-footer-promo"> <a href="https://research.youtube/" class="devsite-footer-promo-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Promo Link (index 5)" > <picture> <img class="devsite-footer-promo-icon" src="/static/site-assets/logo-youtube.svg" loading="lazy" alt="YouTube Researcher Program"> </picture> <span class="devsite-footer-promo-label"> YouTube Researcher Program </span> </a> <div class="devsite-footer-promo-description">For researchers interested in using data from YouTube’s global API</div> </li> </ul> </nav> </devsite-footer-promos> <devsite-footer-linkboxes class="devsite-footer"> <nav class="devsite-footer-linkboxes nocontent" aria-label="Footer links"> <ul class="devsite-footer-linkboxes-list"> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Tools</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="https://developers.google.com/apis-explorer/#p/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" > Google APIs Explorer </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/youtube/youtube_player_demo" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" > YouTube Player Demo </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/youtube/youtube_subscribe_button" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" > Configure a Subscribe Button </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Issue Tracker</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="https://issuetracker.google.com/issues/new?component=186600&template=874803" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" > File a bug </a> </li> <li class="devsite-footer-linkbox-item"> <a href="https://issuetracker.google.com/issues/new?component=186600&template=874803" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" > Request a feature </a> </li> <li class="devsite-footer-linkbox-item"> <a href="https://issuetracker.google.com/issues?q=componentid:186600" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" > See open issues </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Product Info</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/youtube/terms/api-services-terms-of-service" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" > Terms of Service </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/youtube/terms/developer-policies" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" > Developer Policies </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/youtube/terms/required-minimum-functionality" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" > Required Minimum Functionality </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/youtube/terms/branding-guidelines" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 4)" > Branding Guidelines </a> </li> </ul> </li> </ul> </nav> </devsite-footer-linkboxes> <devsite-footer-utility class="devsite-footer"> <div class="devsite-footer-utility nocontent"> <nav class="devsite-footer-sites" aria-label="Other Google Developers websites"> <a href="https://developers.google.com/" class="devsite-footer-sites-logo-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Google Developers Link"> <picture> <img class="devsite-footer-sites-logo" src="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/lockup-google-for-developers.svg" loading="lazy" alt="Google Developers"> </picture> </a> <ul class="devsite-footer-sites-list"> <li class="devsite-footer-sites-item"> <a href="//developer.android.com" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Android Link" > Android </a> </li> <li class="devsite-footer-sites-item"> <a href="//developer.chrome.com/home" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Chrome Link" > Chrome </a> </li> <li class="devsite-footer-sites-item"> <a href="//firebase.google.com" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Firebase Link" > Firebase </a> </li> <li class="devsite-footer-sites-item"> <a href="//cloud.google.com" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Google Cloud Platform Link" > Google Cloud Platform </a> </li> <li class="devsite-footer-sites-item"> <a href="//ai.google.dev/" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Google AI Link" > Google AI </a> </li> <li class="devsite-footer-sites-item"> <a href="/products" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer All products Link" > All products </a> </li> </ul> </nav> <nav class="devsite-footer-utility-links" aria-label="Utility links"> <ul class="devsite-footer-utility-list"> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="/terms/site-terms" data-category="Site-Wide Custom Events" data-label="Footer Terms link" > Terms </a> </li> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="//policies.google.com/privacy" data-category="Site-Wide Custom Events" data-label="Footer Privacy link" > Privacy </a> </li> <li class="devsite-footer-utility-item glue-cookie-notification-bar-control"> <a class="devsite-footer-utility-link gc-analytics-event" href="#" data-category="Site-Wide Custom Events" data-label="Footer Manage cookies link" aria-hidden="true" > Manage cookies </a> </li> <li class="devsite-footer-utility-item devsite-footer-utility-button"> <span class="devsite-footer-utility-description">Sign up for the Google for Developers newsletter</span> <a class="devsite-footer-utility-link gc-analytics-event" href="/newsletter/subscribe" data-category="Site-Wide Custom Events" data-label="Footer Subscribe link" > Subscribe </a> </li> </ul> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="de" >Deutsch</a> </li> <li role="presentation"> <a role="menuitem" lang="es" >Español</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="fr" >Français</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="it" >Italiano</a> </li> <li role="presentation"> <a role="menuitem" lang="pl" >Polski</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="vi" >Tiếng Việt</a> </li> <li role="presentation"> <a role="menuitem" lang="tr" >Türkçe</a> </li> <li role="presentation"> <a role="menuitem" lang="ru" >Русский</a> </li> <li role="presentation"> <a role="menuitem" lang="he" >עברית</a> </li> <li role="presentation"> <a role="menuitem" lang="ar" >العربيّة</a> </li> <li role="presentation"> <a role="menuitem" lang="fa" >فارسی</a> </li> <li role="presentation"> <a role="menuitem" lang="hi" >हिंदी</a> </li> <li role="presentation"> <a role="menuitem" lang="bn" >বাংলা</a> </li> <li role="presentation"> <a role="menuitem" lang="th" >ภาษาไทย</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_tw" >中文 – 繁體</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> </nav> </div> </devsite-footer-utility> <devsite-panel></devsite-panel> <devsite-concierge data-info-panel data-ai-panel data-api-explorer-panel > </devsite-concierge> </section></section> <devsite-sitemask></devsite-sitemask> <devsite-snackbar></devsite-snackbar> <devsite-tooltip ></devsite-tooltip> <devsite-heading-link></devsite-heading-link> <devsite-analytics> <script type="application/json" analytics>[{"dimensions": {"dimension3": false, "dimension4": "YouTube Analytics and Reporting APIs", "dimension5": "en", "dimension6": "en", "dimension1": "Signed out", "dimension11": false}, "gaid": "UA-24532603-1", "metrics": {"ratings_count": "metric2", "ratings_value": "metric1"}, "purpose": 1}]</script> <script type="application/json" tag-management>{"at": "True", "ga4": [{"id": "G-272J68FCRF", "purpose": 1}], "ga4p": [{"id": "G-272J68FCRF", "purpose": 1}], "gtm": [], "parameters": {"internalUser": "False", "language": {"machineTranslated": "False", "requested": "en", "served": "en"}, "pageType": "reference", "projectName": "YouTube Analytics and Reporting APIs", "signedIn": "False", "tenant": "developers", "recommendations": {"sourcePage": "", "sourceType": 0, "sourceRank": 0, "sourceIdenticalDescriptions": 0, "sourceTitleWords": 0, "sourceDescriptionWords": 0, "experiment": ""}, "experiment": {"ids": ""}}}</script> </devsite-analytics> <devsite-badger></devsite-badger> <script nonce="zAYCbH5dlhE6lnijuQn7TRW2S5buFd"> (function(d,e,v,s,i,t,E){d['GoogleDevelopersObject']=i; t=e.createElement(v);t.async=1;t.src=s;E=e.getElementsByTagName(v)[0]; E.parentNode.insertBefore(t,E);})(window, document, 'script', 'https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/js/app_loader.js', '[1,"en",null,"/js/devsite_app_module.js","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers","https://developers-dot-devsite-v2-prod.appspot.com",1,null,["/_pwa/developers/manifest.json","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/images/video-placeholder.svg","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/favicon-new.png","https://fonts.googleapis.com/css?family=Google+Sans:400,500|Roboto:400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"],1,null,[1,6,8,12,14,17,21,25,50,52,63,70,75,76,80,87,91,92,93,97,98,100,101,102,103,104,105,107,108,109,110,112,113,117,118,120,122,124,125,126,127,129,130,131,132,133,134,135,136,138,140,141,147,148,149,151,152,156,157,158,159,161,163,164,168,169,170,179,180,182,183,186,191,193,196],"AIzaSyAP-jjEJBzmIyKR4F-3XITp8yM9T1gEEI8","AIzaSyB6xiKGDR5O3Ak2okS4rLkauxGUG7XP0hg","developers.google.com","AIzaSyAQk0fBONSGUqCNznf6Krs82Ap1-NV6J4o","AIzaSyCCxcqdrZ_7QMeLCRY20bh_SXdAYqy70KY",null,null,null,["Search__enable_suggestions_from_borg","Cloud__enable_cloudx_ping","Profiles__enable_awarding_url","Profiles__enable_dashboard_curated_recommendations","MiscFeatureFlags__emergency_css","TpcFeatures__enable_required_headers","Cloud__enable_cloudx_experiment_ids","Search__enable_ai_search_summaries","Search__enable_page_map","Experiments__reqs_query_experiments","Profiles__require_profile_eligibility_for_signin","Profiles__enable_release_notes_notifications","Cloud__enable_cloud_dlp_service","Concierge__enable_pushui","Cloud__enable_cloud_shell","Profiles__enable_page_saving","CloudShell__cloud_code_overflow_menu","Concierge__enable_concierge","BookNav__enable_tenant_cache_key","TpcFeatures__enable_mirror_tenant_redirects","MiscFeatureFlags__developers_footer_image","Search__enable_ai_search_summaries_restricted","Cloud__enable_cloud_facet_chat","MiscFeatureFlags__enable_firebase_utm","Profiles__enable_complete_playlist_endpoint","MiscFeatureFlags__enable_variable_operator","MiscFeatureFlags__developers_footer_dark_image","DevPro__enable_cloud_innovators_plus","MiscFeatureFlags__enable_project_variables","Profiles__enable_completecodelab_endpoint","CloudShell__cloud_shell_button","Search__enable_dynamic_content_confidential_banner","EngEduTelemetry__enable_engedu_telemetry","Profiles__enable_profile_collections","Profiles__enable_recognition_badges","Profiles__enable_developer_profiles_callout","Significatio__enable_by_tenant","Cloud__enable_free_trial_server_call","Analytics__enable_clearcut_logging","DevPro__enable_developer_subscriptions","Cloud__enable_llm_concierge_chat","Cloud__enable_legacy_calculator_redirect","MiscFeatureFlags__enable_explain_this_code","MiscFeatureFlags__enable_view_transitions","Cloud__enable_cloud_shell_fte_user_flow","Concierge__enable_concierge_restricted","Search__enable_ai_eligibility_checks","Profiles__enable_public_developer_profiles"],null,null,"AIzaSyBLEMok-5suZ67qRPzx0qUtbnLmyT_kCVE","https://developerscontentserving-pa.clients6.google.com","AIzaSyCM4QpTRSqP5qI4Dvjt4OAScIN8sOUlO-k","https://developerscontentsearch-pa.clients6.google.com",1,4,null,"https://developerprofiles-pa.clients6.google.com",[1,"developers","Google for Developers","developers.google.com",null,"developers-dot-devsite-v2-prod.appspot.com",null,null,[1,1,[1],null,null,null,null,null,null,null,null,[1],null,null,null,null,null,null,[1],[1,null,null,[1,20],"/recommendations/information"],null,null,null,[1,1,1],[1,1,null,1,1]],null,[null,null,null,null,null,null,"/images/lockup-new.svg","/images/touchicon-180-new.png",null,null,null,null,1,null,null,null,null,null,null,null,null,1,null,null,null,"/images/lockup-dark-theme-new.svg",[]],[],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,[6,1,14,15,20,22,23,29,32,36],null,[[null,null,null,[3,7,10,2,39,17,4,32,24,11,12,13,34,15,25],null,null,[1,[["docType","Choose a content type",[["Tutorial",null,null,null,null,null,null,null,null,"Tutorial"],["Guide",null,null,null,null,null,null,null,null,"Guide"],["Sample",null,null,null,null,null,null,null,null,"Sample"]]],["product","Choose a product",[["Android",null,null,null,null,null,null,null,null,"Android"],["ARCore",null,null,null,null,null,null,null,null,"ARCore"],["ChromeOS",null,null,null,null,null,null,null,null,"ChromeOS"],["Firebase",null,null,null,null,null,null,null,null,"Firebase"],["Flutter",null,null,null,null,null,null,null,null,"Flutter"],["Assistant",null,null,null,null,null,null,null,null,"Google Assistant"],["GoogleCloud",null,null,null,null,null,null,null,null,"Google Cloud"],["GoogleMapsPlatform",null,null,null,null,null,null,null,null,"Google Maps Platform"],["GooglePay",null,null,null,null,null,null,null,null,"Google Pay & Google Wallet"],["GooglePlay",null,null,null,null,null,null,null,null,"Google Play"],["Tensorflow",null,null,null,null,null,null,null,null,"TensorFlow"]]],["category","Choose a topic",[["AiAndMachineLearning",null,null,null,null,null,null,null,null,"AI and Machine Learning"],["Data",null,null,null,null,null,null,null,null,"Data"],["Enterprise",null,null,null,null,null,null,null,null,"Enterprise"],["Gaming",null,null,null,null,null,null,null,null,"Gaming"],["Mobile",null,null,null,null,null,null,null,null,"Mobile"],["Web",null,null,null,null,null,null,null,null,"Web"]]]]]],[1,1],null,1],[[["UA-24532603-1"],["UA-22084204-5"],null,null,["UA-24532603-5"],null,null,[["G-272J68FCRF"],null,null,[["G-272J68FCRF",2]]],[["UA-24532603-1",2]],null,[["UA-24532603-5",2]],null,1],[[14,11],[3,2],[5,4],[11,8],[15,12],[1,1],[4,3],[12,9],[16,13],[6,5],[13,10]],[[1,1],[2,2]]],null,4,null,null,null,null,null,null,null,null,null,null,null,null,null,"developers.devsite.google"],null,"pk_live_5170syrHvgGVmSx9sBrnWtA5luvk9BwnVcvIi7HizpwauFG96WedXsuXh790rtij9AmGllqPtMLfhe2RSwD6Pn38V00uBCydV4m"]') </script> <devsite-a11y-announce></devsite-a11y-announce> </body> </html>