YouTube Reporting API - Data Model

重要: 日次 API レポートとバックフィル レポートは、生成後 60 日間利用できます。過去のデータレポートは、生成後 30 日間利用できます。

このポリシーは、すべてのレポートとレポートジョブにグローバルに適用されます。詳しくは、YouTube Reporting API の変更履歴をご覧ください。

YouTube Reporting API は、チャンネルまたはコンテンツ所有者の包括的な YouTube アナリティクス データ一式を含む事前定義レポートをサポートしています。このレポートでは、YouTube Analytics API または YouTube Studio の [アナリティクス] セクションでクエリを実行できる一括データセットをダウンロードすることができます。

この API では、システム管理の自動生成レポートもサポートしています。このレポートは、[レポート] メニューから対応するレポートにアクセスできるコンテンツ所有者が使用できます。これらのレポートには、広告収益データと YouTube Premium サブスクリプション収益データが含まれます。詳しくは、システム管理レポートのドキュメントをご覧ください。

概要

レポート フィールドは、ディメンションまたは指標のいずれかで分類されます。

  • ディメンションとは、データの集計に使用される一般的な条件(アクションが発生した日付やユーザーが所在する国など)です。レポートでは、データの各行にはディメンション値の組み合わせが一意ではありません。
  • 指標とは、ユーザーの行動、広告のパフォーマンス、推定収益に関連する個々の測定値のことです。ユーザー アクティビティの指標には、動画の視聴回数や評価(高評価と低評価)などがあります。

たとえば、チャネルの基本的なユーザー アクティビティ レポートには、次のディメンションが含まれています。

  • day: アクティビティが発生した日付。
  • channel: アクティビティに関連付けられた YouTube チャンネル。
  • video: アクティビティに関連付けられた YouTube 動画。
  • liveOrOnDemand: 視聴者がライブ動画ストリームを視聴しているかどうかを示す値。
  • subscribedStatus: 視聴者がチャンネル登録したかどうかを示す値。
  • country: 視聴者が所在する国。

このレポートには、視聴回数高評価数averageViewDuration などのさまざまな指標も含まれています。レポートを取得してインポートすると、アプリケーションで一般的なディメンション値に基づいてさまざまな計算を行うことができます。

たとえば、特定の日付のドイツにおける合計視聴回数を計算するには、country 列の値が DE で、day 列の値が YYYY-MM-DD 形式の日付であるすべての行について、views 指標の値を合計します。

YouTube アナリティクス レポートの取得

API を使用してチャンネル レポートとコンテンツ所有者レポートを取得する手順は次のとおりです。システム管理レポートを取得する手順については、システム管理レポートのドキュメントをご覧ください。

ステップ 1: 認証情報を取得する

すべての YouTube Reporting API リクエストには承認が必要です。認可ガイドでは、OAuth 2.0 プロトコルを使用して認可トークンを取得する方法を説明しています。

YouTube Reporting API リクエストでは、次の承認スコープが使用されます。

スコープ
https://www.googleapis.com/auth/yt-analytics.readonly YouTube コンテンツの YouTube アナリティクス レポートを表示します。このスコープは再生回数や評価数など、ユーザー アクティビティの指標へのアクセスを提供します。
https://www.googleapis.com/auth/yt-analytics-monetary.readonly YouTube コンテンツに関する YouTube アナリティクス収益レポートを表示します。このスコープでは、ユーザー アクティビティの指標、推定収益、広告パフォーマンスの指標にアクセスできます。

ステップ 2: 取得するレポートを特定する

API の reportTypes.list メソッドを呼び出して、チャンネルまたはコンテンツ所有者に対して生成できるレポートのリストを取得します。このメソッドは、レポート ID と名前のリストを返します。作成するレポートの id プロパティ値を取得します。たとえば、チャネルの基本ユーザー アクティビティ レポートの ID は channel_basic_a1 です。

レポート名は、サポートされているチャンネル レポートコンテンツ所有者レポートの定義に関するドキュメントでもご確認いただけます。

ステップ 3: レポートジョブを作成する

レポート作成ジョブが作成されるまで、レポートの作成は開始されません。(そのため、実際にレポートの取得を希望しているチャンネルとコンテンツ所有者に対してのみ、レポートが生成されます)。

レポートジョブを作成するには、API の jobs.create メソッドを呼び出します。リクエストの本文に次の値を設定します。

  • reportTypeId プロパティの値を、手順 2 で取得したレポート ID に設定します。
  • name プロパティの値を、レポートに関連付ける名前に設定します。

jobs.create メソッドに対する API レスポンスには、ジョブを一意に識別する ID を指定する Job リソースが含まれます。レポートの取得は、ジョブが作成されてから 48 時間以内に開始できます。最初に利用可能なレポートは、ジョブをスケジュールした日以降のものになります。

たとえば、9 月 1 日にジョブのスケジュールを設定すると、9 月 1 日のレポートは 9 月 3 日に使用できるようになります。9 月 2 日のレポートは 9 月 4 日に掲載されます。

ステップ 4: ジョブ ID を取得する

注: 手順 3 で返されたジョブ ID がアプリケーションに保存されている場合は、この手順をスキップできます。

jobs.list メソッドを呼び出して、スケジュールされたジョブのリストを取得します。返される各 Job リソースの reportTypeId プロパティは、そのジョブが生成するレポートのタイプを識別します。次のステップでは、アプリケーションで同じリソースの id プロパティ値が必要になります。

ステップ 5: レポートのダウンロード URL を取得する

jobs.reports.list メソッドを呼び出して、ジョブに対して作成されたレポートのリストを取得します。リクエストで、jobId パラメータを、取得するレポートのジョブ ID に設定します。

ヒント: createdAfter パラメータを使用して、指定した時間以降に作成されたレポートのみを API が返すように指定します。このパラメータを使用すると、API が、まだ処理していないレポートのみを返すようになります。

API レスポンスには、そのジョブの Report リソースのリストが含まれます。各リソースは、一意の 24 時間のデータを含むレポートを参照します。なお、データが利用できない日については、ダウンロード可能なレポートが生成されます。これらのレポートにはヘッダー行が含まれますが、追加データは含まれません。

  • リソースの startTime プロパティと endTime プロパティを使用して、レポートのデータの対象となる期間を指定します。
  • レポートを取得できる URL は、リソースの downloadUrl プロパティで指定します。
  • リソースの createTime プロパティは、レポートが生成された日時を指定します。アプリケーションはこの値を格納し、それを使用して、以前にダウンロードしたレポートが変更されたかどうかを判断する必要があります。

ステップ 6: レポートをダウンロードする

手順 5 で取得した downloadUrl に HTTP GET リクエストを送信してレポートを取得します。

ダウンロード リクエストで gzip 圧縮を有効にすると、レポートのダウンロードに必要な帯域幅を減らすことができます。API レスポンスの解凍には追加の CPU 時間が必要になりますが、ネットワーク リソースの消費量低下には、そのコストを補うだけの利点があります。

gzip でエンコードされたレスポンスを受信するには、次の例のように Accept-Encoding HTTP リクエスト ヘッダーを gzip に設定します。

Accept-Encoding: gzip

レポートの処理

ベスト プラクティス

YouTube Reporting API を使用するアプリケーションでは、常に以下の方法を使用する必要があります。

  • レポートの列の順序は、レポートのヘッダー行を使用して判断できます。たとえば、「ビュー」がレポートの説明で最初にリストされているという理由で、その指標がレポートで最初に返されるとは想定しないでください。代わりに、レポートのヘッダー行を使用して、そのデータを含む列を特定します。

  • 同じレポートを繰り返し処理することがないよう、ダウンロードしたレポートは記録しておいてください。次のリストでは、これを行う方法をいくつか提案しています。

    • reports.list メソッドを呼び出す際に、createdAfter パラメータを使用すると、特定の日付以降に作成されたレポートのみを取得できます。(初めてレポートを取得する場合は、createdAfter パラメータを省略します)。

      レポートを取得して正常に処理するたびに、そのレポートの最新が作成された日時に対応するタイムスタンプを保存します。次に、reports.list メソッドの後続の呼び出しごとに createdAfter パラメータ値を更新して、API を呼び出すたびに、バックフィルされたデータが含まれる新しいレポートを含む新しいレポートのみを取得するようにします。

      安全措置として、レポートを取得する前に、レポートの ID がデータベースにまだ登録されていないことも確認してください。

    • ダウンロードして処理した各レポートの ID を保存します。また、各レポートが生成された日時や、レポートの startTimeendTime などの追加情報を保存することで、レポートに含まれるデータの期間を識別できます。各レポートには 24 時間分のデータが含まれるため、各ジョブには多数のレポートが含まれる可能性があります。

      レポート ID を使用して、ダウンロードとインポートの必要があるレポートを特定します。ただし、2 つの新しいレポートで startTime プロパティ値と endTime プロパティ値が同じである場合は、新しい createTime 値を持つレポートのみをインポートしてください。

  • レポートには YouTube リソースに関連付けられた ID が含まれており、YouTube Data API を使用してこれらのリソースの追加メタデータを取得できます。YouTube API サービスのデベロッパー ポリシー(セクション III.E.4.b から III.E.4.d のセクション)に記載されているように、API クライアントは、30 日後にその API から保存済みリソース メタデータを削除または更新する必要があります。

レポートの特性

API レポートは、バージョニングされた .csv(カンマ区切り値)ファイルで、次の特性を持ちます。

  • 各レポートには、午前 0 時から午後 11 時 59 分(太平洋時間)までの 24 時間分のデータが含まれます。そのため、どのレポートでも「日」ディメンションの値は、常に同じになります。

  • レポートは毎日更新されます。

  • データが利用できない日については、ダウンロード可能なレポートが生成されます。これらのレポートにはヘッダー行が含まれますが、追加データは含まれません。

  • API レポートは、新しいレポートジョブに対して生成される履歴データを除き、生成後 60 日間利用できます。レポートは、作成から 60 日を経過するとアクセスできなくなります。
  • 過去のデータを含むレポートは、作成後 30 日間利用できます。過去のデータを含むレポートは、30 日以上経過するとアクセスできなくなります。
  • レポートデータがフィルタされません。そのため、チャンネル レポートには、削除されたリソースに関連するチャンネルの動画または再生リストのすべてのデータが含まれますが、次の段落に記載されているデータは除きます。同様に、コンテンツ所有者レポートには、コンテンツ所有者のチャンネル(動画、再生リスト、広告のパフォーマンスなど)に関するすべてのデータが含まれますが、以下の例外があります。

    レポートデータはフィルタされませんが、レポート生成日の 30 日以上前に削除された YouTube リソースへの参照はレポートに含まれません。

  • レポートデータが並べ替えられていません。

  • レポートでは、指標のない行は省略されます。つまり、指標がない行はレポートから除外されます。たとえば、ある動画がアルバニアで 1 回も視聴されなかった場合、その日のレポートにはアルバニアの行は含まれません。

  • レポートには、チャンネルのすべての動画の合計視聴回数など、指標の概要データを示す行は含まれません。これらの合計値はレポートの値の合計として計算できますが、その合計には上記のように削除された動画の指標が含まれていない場合があります。YouTube Analytics API を使用して総数を取得することもできます。YouTube Analytics API は、リソースが API レスポンスで明示的に参照されていなくても、削除されたリソースの指標を含む合計値を返します。

バックフィル データ

バックフィル データとは、以前に配信されたデータセットを置き換えるデータセットのことです。バックフィル データ レポートが利用可能になったら、アプリケーションで新しいレポートを取得し、変更されたデータセットと一致するように保存データを更新する必要があります。たとえば、アプリケーションで、レポートの対象期間の以前のデータを削除してから、新しいデータセットをインポートできます。

バックフィル データがある場合は、新しいレポート ID を持つ新しいレポートが生成されます。その場合、レポートの startTime プロパティと endTime プロパティ値は、以前に利用可能だったレポートおよび以前にダウンロードしたレポートの開始時間と終了時間と一致します。

バックフィル レポートには、レポート生成日の 30 日以上前に削除された YouTube リソースへの参照は含まれません。

過去のデータ

新しいレポートジョブのスケジュールを設定すると、その日以降の履歴レポートが生成され、ジョブを作成した時点の 30 日前までのレポートが生成されます。したがって、このドキュメントで履歴データとは、レポートジョブがスケジュールされる前の期間のデータを含むレポートを指します。

過去のレポートは、利用可能になり次第投稿されます。通常、過去のデータはすべて数日以内にジョブに送信されます。レポートの特性のセクションで説明したように、過去のデータを含むレポートは、生成後 30 日間利用できます。履歴データ以外のデータを含むレポートは、60 日間利用できます。

データの匿名化

YouTube 視聴者の匿名性を確保するため、一部のディメンションの値は、同じ行の指標が特定のしきい値を満たしている場合にのみ返されます。

たとえば、チャンネルの動画トラフィック ソース レポートでは、各行に trafficSourceTypetrafficSourceDetail などの複数のディメンションが含まれます。各行には、視聴回数などのさまざまな指標も含まれています。YouTube 検索から発生したトラフィックを示す行では、トラフィックにつながった検索語句が trafficSourceDetail ディメンションによって特定されます。

この例では、次のルールが適用されます。

  • トラフィック ソース レポートでクエリ語句(trafficSourceDetail)が特定されるのは、特定の日に特定の動画が一定回数以上視聴された場合に限られます。この場合、views は指標、video は集計ディメンション、trafficSourceDetail は匿名化されたディメンションです。

  • レポートには、視聴回数のしきい値を満たさないすべての trafficSourceDetail 値の指標を集計する行が追加されます。この行には、クエリ語句に関連付けられた閲覧の合計数が表示されますが、語句自体が特定されるわけではありません。

次の表に、これらのルールを示します。1 つ目の表にはトラフィック ソース レポートの生成に使用すると仮定した元データのセットが含まれ、2 つ目の表にはレポート自体が含まれます。この例では、視聴回数のしきい値が 10 に設定されています。つまり、特定の日に特定の動画が 10 回以上視聴された場合にのみ、検索語句がレポートに表示されます。(実際のしきい値は変更される可能性があります)。

動画の未加工の YouTube 検索トラフィック データ

以下のデータが、特定の日における特定の動画への YouTube 検索トラフィックを表しています。

検索語句 推定再生時間(分)
ガンナムスタイル 100 200
サイ 15 25
サイ ガンナム 9 15
オッパ ガンナム 5 8
乗馬ダンス 2 5

トラフィック ソース レポートの例

次の表は、前のセクションの元データに対して YouTube が生成するトラフィック ソース レポートの抜粋です。(実際のレポートにはさらに多くのディメンションと指標が含まれます)。この例では、閲覧回数が 10 回以上の検索語句のみがレポートに表示されます。実際のしきい値は変更される可能性があります。

レポートの 3 行目では、trafficSourceDetail ディメンション値が NULL になっています。views 指標と estimatedMinutesWatched 指標には、視聴回数が 10 回未満だった 3 つの検索語句について、合計視聴回数と再生時間(分)が含まれます。

trafficSourceDetail estimatedMinutesWatched
ガンナムスタイル 100 200
サイ 15 25
NULL 16 28

匿名化の対象となるディメンション

次の表は、関連付けられた指標値が特定のしきい値を満たさない場合に匿名化されるディメンション値を示しています。いずれの場合も、指標の値は別のディメンションで集計されます。たとえば、指標が「視聴回数」で、集計ディメンションが「動画」の場合、動画が一定回数視聴されていない限り、ディメンション値は匿名化されます。

指標 ディメンションの集計 匿名化されたディメンション 匿名化された値
subscribersGained channel country ZZ
subscribersGained channel province US-ZZ
subscribersLost channel country ZZ
subscribersLost channel province US-ZZ
comments 動画 country ZZ
comments 動画 province US-ZZ
高評価 動画 country ZZ
高評価 動画 province US-ZZ
低評価 動画 country ZZ
低評価 動画 province US-ZZ
視聴回数 動画 ageGroup NULL
視聴回数 動画 性別 NULL
視聴回数 videotrafficSourceDetail trafficSourceDetail NULL
チャンネル登録者数 channel subscribedStatus NULL

コードサンプル

次のコードサンプルは、API を使用してレポートジョブを作成し、そのジョブのレポートを取得する方法を示しています。言語ごとに次の 2 つのコードサンプルが提供されています。

  1. 最初のコードサンプルは、利用可能なレポートタイプのリストを取得して、新しいレポートジョブを作成する方法を示しています。

  2. 2 つ目のコードサンプルは、特定のジョブのレポートを取得する方法を示しています。レポートの取得は、ジョブが作成されてから 48 時間以内に開始できます。

注: 次のコードサンプルは、サポートされているプログラミング言語の一部を示しているわけではありません。サポートされている言語の一覧については、クライアント ライブラリのドキュメントをご覧ください。

Java

次のサンプルでは、Java クライアント ライブラリを使用しています。

例 1: レポートジョブを作成する

次のコードサンプルでは、reportTypes.list メソッドを呼び出して、利用可能なレポートタイプのリストを取得します。次に、jobs.create メソッドを呼び出して、新しいレポートジョブを作成します。

/*
 * Copyright (c) 2015 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.services.samples.youtube.cmdline.reporting;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.samples.youtube.cmdline.Auth;
import com.google.api.services.youtubereporting.YouTubeReporting;
import com.google.api.services.youtubereporting.model.Job;
import com.google.api.services.youtubereporting.model.ListReportTypesResponse;
import com.google.api.services.youtubereporting.model.ReportType;
import com.google.common.collect.Lists;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * This sample creates a reporting job by:
 *
 * 1. Listing the available report types using the "reportTypes.list" method.
 * 2. Creating a reporting job using the "jobs.create" method.
 *
 * @author Ibrahim Ulukaya
 */
public class CreateReportingJob {

    /**
     * Define a global instance of a YouTube Reporting object, which will be used to make
     * YouTube Reporting API requests.
     */
    private static YouTubeReporting youtubeReporting;


    /**
     * Create a reporting job.
     *
     * @param args command line args (not used).
     */
    public static void main(String[] args) {

        /*
         * This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for
         * authenticated user's account. Any request that retrieves earnings or ad performance metrics must
         * use this scope.
         */
        List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/yt-analytics-monetary.readonly");

        try {
            // Authorize the request.
            Credential credential = Auth.authorize(scopes, "createreportingjob");

            // This object is used to make YouTube Reporting API requests.
            youtubeReporting = new YouTubeReporting.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
                    .setApplicationName("youtube-cmdline-createreportingjob-sample").build();

            // Prompt the user to specify the name of the job to be created.
            String name = getNameFromUser();

            if (listReportTypes()) {
              createReportingJob(getReportTypeIdFromUser(), name);
            }
        } catch (GoogleJsonResponseException e) {
            System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode()
                    + " : " + e.getDetails().getMessage());
            e.printStackTrace();

        } catch (IOException e) {
            System.err.println("IOException: " + e.getMessage());
            e.printStackTrace();
        } catch (Throwable t) {
            System.err.println("Throwable: " + t.getMessage());
            t.printStackTrace();
        }
    }

    /**
     * Lists report types. (reportTypes.listReportTypes)
     * @return true if at least one report type exists
     * @throws IOException
     */
    private static boolean listReportTypes() throws IOException {
        // Call the YouTube Reporting API's reportTypes.list method to retrieve report types.
        ListReportTypesResponse reportTypesListResponse = youtubeReporting.reportTypes().list()
            .execute();
        List<ReportType> reportTypeList = reportTypesListResponse.getReportTypes();

        if (reportTypeList == null || reportTypeList.isEmpty()) {
          System.out.println("No report types found.");
          return false;
        } else {
            // Print information from the API response.
            System.out.println("\n================== Report Types ==================\n");
            for (ReportType reportType : reportTypeList) {
                System.out.println("  - Id: " + reportType.getId());
                System.out.println("  - Name: " + reportType.getName());
                System.out.println("\n-------------------------------------------------------------\n");
           }
        }
        return true;
    }

    /**
     * Creates a reporting job. (jobs.create)
     *
     * @param reportTypeId Id of the job's report type.
     * @param name name of the job.
     * @throws IOException
     */
    private static void createReportingJob(String reportTypeId, String name)
        throws IOException {
        // Create a reporting job with a name and a report type id.
        Job job = new Job();
        job.setReportTypeId(reportTypeId);
        job.setName(name);

        // Call the YouTube Reporting API's jobs.create method to create a job.
        Job createdJob = youtubeReporting.jobs().create(job).execute();

        // Print information from the API response.
        System.out.println("\n================== Created reporting job ==================\n");
        System.out.println("  - ID: " + createdJob.getId());
        System.out.println("  - Name: " + createdJob.getName());
        System.out.println("  - Report Type Id: " + createdJob.getReportTypeId());
        System.out.println("  - Create Time: " + createdJob.getCreateTime());
        System.out.println("\n-------------------------------------------------------------\n");
    }

    /*
     * Prompt the user to enter a name for the job. Then return the name.
     */
    private static String getNameFromUser() throws IOException {

        String name = "";

        System.out.print("Please enter the name for the job [javaTestJob]: ");
        BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
        name = bReader.readLine();

        if (name.length() < 1) {
            // If nothing is entered, defaults to "javaTestJob".
          name = "javaTestJob";
        }

        System.out.println("You chose " + name + " as the name for the job.");
        return name;
    }

    /*
     * Prompt the user to enter a report type id for the job. Then return the id.
     */
    private static String getReportTypeIdFromUser() throws IOException {

        String id = "";

        System.out.print("Please enter the reportTypeId for the job: ");
        BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
        id = bReader.readLine();

        System.out.println("You chose " + id + " as the report type Id for the job.");
        return id;
    }
}

例 2: レポートを取得する

コードサンプルは jobs.list メソッドを呼び出して、レポートジョブのリストを取得します。次に、jobId パラメータを特定のジョブ ID に設定して reports.list メソッドを呼び出し、そのジョブによって作成されたレポートを取得します。最後に、各レポートのダウンロード URL が出力されます。

/*
 * Copyright (c) 2015 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.services.samples.youtube.cmdline.reporting;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.GenericUrl;
import com.google.api.services.samples.youtube.cmdline.Auth;
import com.google.api.services.youtubereporting.YouTubeReporting;
import com.google.api.services.youtubereporting.YouTubeReporting.Media.Download;
import com.google.api.services.youtubereporting.model.Job;
import com.google.api.services.youtubereporting.model.ListJobsResponse;
import com.google.api.services.youtubereporting.model.ListReportsResponse;
import com.google.api.services.youtubereporting.model.Report;

import com.google.common.collect.Lists;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import javax.print.attribute.standard.Media;

/**
 * This sample retrieves reports created by a specific job by:
 *
 * 1. Listing the jobs using the "jobs.list" method.
 * 2. Retrieving reports using the "reports.list" method.
 *
 * @author Ibrahim Ulukaya
 */
public class RetrieveReports {

    /**
     * Define a global instance of a YouTube Reporting object, which will be used to make
     * YouTube Reporting API requests.
     */
    private static YouTubeReporting youtubeReporting;


    /**
     * Retrieve reports.
     *
     * @param args command line args (not used).
     */
    public static void main(String[] args) {

        /*
         * This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for
         * authenticated user's account. Any request that retrieves earnings or ad performance metrics must
         * use this scope.
         */
        List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/yt-analytics-monetary.readonly");

        try {
            // Authorize the request.
            Credential credential = Auth.authorize(scopes, "retrievereports");

            // This object is used to make YouTube Reporting API requests.
            youtubeReporting = new YouTubeReporting.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
                    .setApplicationName("youtube-cmdline-retrievereports-sample").build();

            if (listReportingJobs()) {
              if(retrieveReports(getJobIdFromUser())) {
                downloadReport(getReportUrlFromUser());
              }
            }
        } catch (GoogleJsonResponseException e) {
            System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode()
                    + " : " + e.getDetails().getMessage());
            e.printStackTrace();

        } catch (IOException e) {
            System.err.println("IOException: " + e.getMessage());
            e.printStackTrace();
        } catch (Throwable t) {
            System.err.println("Throwable: " + t.getMessage());
            t.printStackTrace();
        }
    }

    /**
     * Lists reporting jobs. (jobs.listJobs)
     * @return true if at least one reporting job exists
     * @throws IOException
     */
    private static boolean listReportingJobs() throws IOException {
        // Call the YouTube Reporting API's jobs.list method to retrieve reporting jobs.
        ListJobsResponse jobsListResponse = youtubeReporting.jobs().list().execute();
        List<Job> jobsList = jobsListResponse.getJobs();

        if (jobsList == null || jobsList.isEmpty()) {
          System.out.println("No jobs found.");
          return false;
        } else {
            // Print information from the API response.
            System.out.println("\n================== Reporting Jobs ==================\n");
            for (Job job : jobsList) {
                System.out.println("  - Id: " + job.getId());
                System.out.println("  - Name: " + job.getName());
                System.out.println("  - Report Type Id: " + job.getReportTypeId());
                System.out.println("\n-------------------------------------------------------------\n");
            }
        }
        return true;
    }

    /**
     * Lists reports created by a specific job. (reports.listJobsReports)
     *
     * @param jobId The ID of the job.
     * @throws IOException
     */
    private static boolean retrieveReports(String jobId)
        throws IOException {
        // Call the YouTube Reporting API's reports.list method
        // to retrieve reports created by a job.
        ListReportsResponse reportsListResponse = youtubeReporting.jobs().reports().list(jobId).execute();
        List<Report> reportslist = reportsListResponse.getReports();

        if (reportslist == null || reportslist.isEmpty()) {
            System.out.println("No reports found.");
            return false;
        } else {
            // Print information from the API response.
            System.out.println("\n============= Reports for the job " + jobId + " =============\n");
            for (Report report : reportslist) {
                System.out.println("  - Id: " + report.getId());
                System.out.println("  - From: " + report.getStartTime());
                System.out.println("  - To: " + report.getEndTime());
                System.out.println("  - Download Url: " + report.getDownloadUrl());
                System.out.println("\n-------------------------------------------------------------\n");
            }
        }
        return true;
    }

    /**
     * Download the report specified by the URL. (media.download)
     *
     * @param reportUrl The URL of the report to be downloaded.
     * @throws IOException
     */
    private static boolean downloadReport(String reportUrl)
        throws IOException {
        // Call the YouTube Reporting API's media.download method to download a report.
        Download request = youtubeReporting.media().download("");
        FileOutputStream fop = new FileOutputStream(new File("report"));
        request.getMediaHttpDownloader().download(new GenericUrl(reportUrl), fop);
        return true;
    }

    /*
     * Prompt the user to enter a job id for report retrieval. Then return the id.
     */
    private static String getJobIdFromUser() throws IOException {

        String id = "";

        System.out.print("Please enter the job id for the report retrieval: ");
        BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
        id = bReader.readLine();

        System.out.println("You chose " + id + " as the job Id for the report retrieval.");
        return id;
    }

    /*
     * Prompt the user to enter a URL for report download. Then return the URL.
     */
    private static String getReportUrlFromUser() throws IOException {

        String url = "";

        System.out.print("Please enter the report URL to download: ");
        BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
        url = bReader.readLine();

        System.out.println("You chose " + url + " as the URL to download.");
        return url;
    }}

PHP

次のサンプルでは、PHP クライアント ライブラリを使用しています。

例 1: レポートジョブを作成する

次のコードサンプルでは、reportTypes.list メソッドを呼び出して、利用可能なレポートタイプのリストを取得します。次に、jobs.create メソッドを呼び出して、新しいレポートジョブを作成します。

<?php

/**
 * This sample creates a reporting job by:
 *
 * 1. Listing the available report types using the "reportTypes.list" method.
 * 2. Creating a reporting job using the "jobs.create" method.
 *
 * @author Ibrahim Ulukaya
 */

/**
 * Library Requirements
 *
 * 1. Install composer (https://getcomposer.org)
 * 2. On the command line, change to this directory (api-samples/php)
 * 3. Require the google/apiclient library
 *    $ composer require google/apiclient:~2.0
 */
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
  throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
}

require_once __DIR__ . '/vendor/autoload.php';
session_start();

/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);

/*
 * This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for
 * authenticated user's account. Any request that retrieves earnings or ad performance metrics must
 * use this scope.
 */
$client->setScopes('https://www.googleapis.com/auth/yt-analytics-monetary.readonly');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// YouTube Reporting object used to make YouTube Reporting API requests.
$youtubeReporting = new Google_Service_YouTubeReporting($client);

// Check if an auth token exists for the required scopes
$tokenSessionKey = 'token-' . $client->prepareScopes();
if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION[$tokenSessionKey] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION[$tokenSessionKey])) {
  $client->setAccessToken($_SESSION[$tokenSessionKey]);
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
  // This code executes if the user enters a name in the form
  // and submits the form. Otherwise, the page displays the form above.
  try {
    if (empty(listReportTypes($youtubeReporting, $htmlBody))) {
      $htmlBody .= sprintf('<p>No report types found.</p>');
    } else if ($_GET['reportTypeId']){
      createReportingJob($youtubeReporting, $_GET['reportTypeId'], $_GET['jobName'], $htmlBody);
    }
  } catch (Google_Service_Exception $e) {
    $htmlBody = sprintf('<p>A service error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  } catch (Google_Exception $e) {
    $htmlBody = sprintf('<p>An client error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  }
  $_SESSION[$tokenSessionKey] = $client->getAccessToken();
} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {
  $htmlBody = <<<END
  <h3>Client Credentials Required</h3>
  <p>
    You need to set <code>\$OAUTH2_CLIENT_ID</code> and
    <code>\$OAUTH2_CLIENT_ID</code> before proceeding.
  <p>
END;
} else {
  // If the user hasn't authorized the app, initiate the OAuth flow
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}


/**
 * Creates a reporting job. (jobs.create)
 *
 * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
 * @param string $reportTypeId Id of the job's report type.
 * @param string $name name of the job.
 * @param $htmlBody - html body.
 */
function createReportingJob(Google_Service_YouTubeReporting $youtubeReporting, $reportTypeId,
    $name, &$htmlBody) {
  # Create a reporting job with a name and a report type id.
  $reportingJob = new Google_Service_YouTubeReporting_Job();
  $reportingJob->setReportTypeId($reportTypeId);
  $reportingJob->setName($name);

  // Call the YouTube Reporting API's jobs.create method to create a job.
  $jobCreateResponse = $youtubeReporting->jobs->create($reportingJob);

  $htmlBody .= "<h2>Created reporting job</h2><ul>";
  $htmlBody .= sprintf('<li>"%s" for reporting type "%s" at "%s"</li>',
      $jobCreateResponse['name'], $jobCreateResponse['reportTypeId'], $jobCreateResponse['createTime']);
  $htmlBody .= '</ul>';
}


/**
 * Returns a list of report types. (reportTypes.listReportTypes)
 *
 * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
 * @param $htmlBody - html body.
 */
function listReportTypes(Google_Service_YouTubeReporting $youtubeReporting, &$htmlBody) {
  // Call the YouTube Reporting API's reportTypes.list method to retrieve report types.
  $reportTypes = $youtubeReporting->reportTypes->listReportTypes();

  $htmlBody .= "<h3>Report Types</h3><ul>";
  foreach ($reportTypes as $reportType) {
    $htmlBody .= sprintf('<li>id: "%s", name: "%s"</li>', $reportType['id'], $reportType['name']);
  }
  $htmlBody .= '</ul>';

  return $reportTypes;
}
?>

<!doctype html>
<html>
<head>
<title>Create a reporting job</title>
</head>
<body>
  <form method="GET">
    <div>
      Job Name: <input type="text" id="jobName" name="jobName" placeholder="Enter Job Name">
    </div>
    <br>
    <div>
      Report Type Id: <input type="text" id="reportTypeId" name="reportTypeId" placeholder="Enter Report Type Id">
    </div>
    <br>
    <input type="submit" value="Create!">
  </form>
  <?=$htmlBody?>
</body>
</html>

例 2: レポートを取得する

コードサンプルは jobs.list メソッドを呼び出して、レポートジョブのリストを取得します。次に、jobId パラメータを特定のジョブ ID に設定して reports.list メソッドを呼び出し、そのジョブによって作成されたレポートを取得します。最後に、各レポートのダウンロード URL が出力されます。

<?php

/**
 * This sample supports the following use cases:
 *
 * 1. Retrieve reporting jobs by content owner:
 *    Ex: php retrieve_reports.php  --contentOwner=="CONTENT_OWNER_ID"
 *    Ex: php retrieve_reports.php  --contentOwner=="CONTENT_OWNER_ID" --includeSystemManaged==True
 * 2. Retrieving list of downloadable reports for a particular job:
 *    Ex: php retrieve_reports.php  --contentOwner=="CONTENT_OWNER_ID" --jobId="JOB_ID"
 * 3. Download a report:
 *    Ex: php retrieve_reports.php  --contentOwner=="CONTENT_OWNER_ID" --downloadUrl="DOWNLOAD_URL" --outputFile="report.txt"
 */

/**
 * Library Requirements
 *
 * 1. Install composer (https://getcomposer.org)
 * 2. On the command line, change to this directory (api-samples/php)
 * 3. Require the google/apiclient library
 *    $ composer require google/apiclient:~2.0
 */
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
  throw new \Exception('please run "composer require google/apiclient:~2.2.0" in "' . __DIR__ .'"');
}

require_once __DIR__ . '/vendor/autoload.php';
session_start();


define('CREDENTIALS_PATH', '~/.credentials/youtube-php.json');

$longOptions = array(
  'contentOwner::',
  'downloadUrl::',
  'includeSystemManaged::',
  'jobId::',
  'outputFile::',
);

$options = getopt('', $longOptions);

$CONTENT_OWNER_ID = ($options['contentOwner'] ? $options['contentOwner'] : '');
$DOWNLOAD_URL = (array_key_exists('downloadUrl', $options) ?
                 $options['downloadUrl'] : '');
$INCLUDE_SYSTEM_MANAGED = (array_key_exists('includeSystemManaged', $options) ?
                           $options['includeSystemManaged'] : '');
$JOB_ID = (array_key_exists('jobId', $options) ? $options['jobId'] : '');
$OUTPUT_FILE = (array_key_exists('outputFile', $options) ?
                $options['outputFile'] : '');

/*
 * You can obtain an OAuth 2.0 client ID and client secret from the
 * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
function getClient() {
  $client = new Google_Client();
  $client->setAuthConfigFile('client_secrets_php.json');
  $client->addScope(
      'https://www.googleapis.com/auth/yt-analytics-monetary.readonly');
  $client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
  $client->setAccessType('offline');

  // Load previously authorized credentials from a file.
  $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
  if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
  } else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf('Open the following link in your browser:\n%s\n', $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for an access token.
    $accessToken = $client->authenticate($authCode);
    $refreshToken = $client->getRefreshToken();

    // Store the credentials to disk.
    if(!file_exists(dirname($credentialsPath))) {
      mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, json_encode($accessToken));
    printf('Credentials saved to %s\n', $credentialsPath);

    //fclose($fp);
  }
  $client->setAccessToken($accessToken);

  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  }

  return $client;
}

/**
 * Expands the home directory alias '~' to the full path.
 * @param string $path the path to expand.
 * @return string the expanded path.
 */
function expandHomeDirectory($path) {
  $homeDirectory = getenv('HOME');
  if (empty($homeDirectory)) {
    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
  }
  return str_replace('~', realpath($homeDirectory), $path);
}

/**
 * Returns a list of reporting jobs. (jobs.listJobs)
 *
 * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
 * @param string $onBehalfOfContentOwner A content owner ID.
 */
function listReportingJobs(Google_Service_YouTubeReporting $youtubeReporting,
    $onBehalfOfContentOwner = '', $includeSystemManaged = False) {
  $reportingJobs = $youtubeReporting->jobs->listJobs(
      array('onBehalfOfContentOwner' => $onBehalfOfContentOwner,
            'includeSystemManaged' => $includeSystemManaged));
  print ('REPORTING JOBS' . PHP_EOL . '**************' . PHP_EOL);
  foreach ($reportingJobs as $job) {
    print($job['reportTypeId'] . ':' . $job['id'] . PHP_EOL);
  }
  print(PHP_EOL);
}

/**
 * Lists reports created by a specific job. (reports.listJobsReports)
 *
 * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
 * @param string $jobId The ID of the job.
 * @param string $onBehalfOfContentOwner A content owner ID.
 */
function listReportsForJob(Google_Service_YouTubeReporting $youtubeReporting,
    $jobId, $onBehalfOfContentOwner = '') {
  $reports = $youtubeReporting->jobs_reports->listJobsReports($jobId,
      array('onBehalfOfContentOwner' => $onBehalfOfContentOwner));
  print ('DOWNLOADABLE REPORTS' . PHP_EOL . '********************' . PHP_EOL);
  foreach ($reports['reports'] as $report) {
    print('Created: ' . date('d M Y', strtotime($report['createTime'])) .
          ' (' . date('d M Y', strtotime($report['startTime'])) .
          ' to ' . date('d M Y', strtotime($report['endTime'])) . ')' .
          PHP_EOL .  '    ' . $report['downloadUrl'] . PHP_EOL . PHP_EOL);
  }
}

/**
 * Download the report specified by the URL. (media.download)
 *
 * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
 * @param string $reportUrl The URL of the report to be downloaded.
 * @param string $outputFile The file to write the report to locally.
 * @param $htmlBody - html body.
 */
function downloadReport(Google_Service_YouTubeReporting $youtubeReporting,
    $reportUrl, $outputFile) {
  $client = $youtubeReporting->getClient();
  // Setting the defer flag to true tells the client to return a request that
  // can be called with ->execute(); instead of making the API call immediately.
  $client->setDefer(true);

  // Call YouTube Reporting API's media.download method to download a report.
  $request = $youtubeReporting->media->download('', array('alt' => 'media'));
  $request = $request->withUri(new \GuzzleHttp\Psr7\Uri($reportUrl));
  $responseBody = '';
  try {
    $response = $client->execute($request);
    $responseBody = $response->getBody();
  } catch (Google_Service_Exception $e) {
    $responseBody = $e->getTrace()[0]['args'][0]->getResponseBody();
  }
  file_put_contents($outputFile, $responseBody);
  $client->setDefer(false);
}

// Define an object that will be used to make all API requests.
$client = getClient();
// YouTube Reporting object used to make YouTube Reporting API requests.
$youtubeReporting = new Google_Service_YouTubeReporting($client);

if ($CONTENT_OWNER_ID) {
  if (!$DOWNLOAD_URL && !$JOB_ID) {
    listReportingJobs($youtubeReporting, $CONTENT_OWNER_ID,
                      $INCLUDE_SYSTEM_MANAGED);
  } else if ($JOB_ID) {
    listReportsForJob($youtubeReporting, $JOB_ID, $CONTENT_OWNER_ID);
  } else if ($DOWNLOAD_URL && $OUTPUT_FILE) {
    downloadReport($youtubeReporting, $DOWNLOAD_URL, $OUTPUT_FILE);
  }
}

?>

Python

次のサンプルでは、Python クライアント ライブラリを使用しています。

例 1: レポートジョブを作成する

次のコードサンプルでは、reportTypes.list メソッドを呼び出して、利用可能なレポートタイプのリストを取得します。次に、jobs.create メソッドを呼び出して、新しいレポートジョブを作成します。

#!/usr/bin/python

# Create a reporting job for the authenticated user's channel or
# for a content owner that the user's account is linked to.
# Usage example:
# python create_reporting_job.py --name='<name>'
# python create_reporting_job.py --content-owner='<CONTENT OWNER ID>'
# python create_reporting_job.py --content-owner='<CONTENT_OWNER_ID>' --report-type='<REPORT_TYPE_ID>' --name='<REPORT_NAME>'

import argparse
import os

import google.oauth2.credentials
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow


# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains

# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the {{ Google Cloud Console }} at
# {{ https://cloud.google.com/console }}.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
#   https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
#   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE = 'client_secret.json'

# This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for
# authenticated user's account. Any request that retrieves earnings or ad performance metrics must
# use this scope.
SCOPES = ['https://www.googleapis.com/auth/yt-analytics-monetary.readonly']
API_SERVICE_NAME = 'youtubereporting'
API_VERSION = 'v1'

# Authorize the request and store authorization credentials.
def get_authenticated_service():
  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
  credentials = flow.run_console()
  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

# Remove keyword arguments that are not set.
def remove_empty_kwargs(**kwargs):
  good_kwargs = {}
  if kwargs is not None:
    for key, value in kwargs.iteritems():
      if value:
        good_kwargs[key] = value
  return good_kwargs

# Call the YouTube Reporting API's reportTypes.list method to retrieve report types.
def list_report_types(youtube_reporting, **kwargs):
  # Provide keyword arguments that have values as request parameters.
  kwargs = remove_empty_kwargs(**kwargs)
  results = youtube_reporting.reportTypes().list(**kwargs).execute()
  reportTypes = results['reportTypes']

  if 'reportTypes' in results and results['reportTypes']:
    reportTypes = results['reportTypes']
    for reportType in reportTypes:
      print 'Report type id: %s\n name: %s\n' % (reportType['id'], reportType['name'])
  else:
    print 'No report types found'
    return False

  return True


# Call the YouTube Reporting API's jobs.create method to create a job.
def create_reporting_job(youtube_reporting, report_type_id, **kwargs):
  # Provide keyword arguments that have values as request parameters.
  kwargs = remove_empty_kwargs(**kwargs)

  reporting_job = youtube_reporting.jobs().create(
    body=dict(
      reportTypeId=args.report_type,
      name=args.name
    ),
    **kwargs
  ).execute()

  print ('Reporting job "%s" created for reporting type "%s" at "%s"'
         % (reporting_job['name'], reporting_job['reportTypeId'],
             reporting_job['createTime']))


# Prompt the user to enter a report type id for the job. Then return the id.
def get_report_type_id_from_user():
  report_type_id = raw_input('Please enter the reportTypeId for the job: ')
  print ('You chose "%s" as the report type Id for the job.' % report_type_id)
  return report_type_id

# Prompt the user to set a job name
def prompt_user_to_set_job_name():
  job_name = raw_input('Please set a name for the job: ')
  print ('Great! "%s" is a memorable name for this job.' % job_name)
  return job_name


if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  # The 'name' option specifies the name that will be used for the reporting job.
  parser.add_argument('--content-owner', default='',
      help='ID of content owner for which you are retrieving jobs and reports.')
  parser.add_argument('--include-system-managed', default=False,
      help='Whether the API response should include system-managed reports')
  parser.add_argument('--name', default='',
    help='Name for the reporting job. The script prompts you to set a name ' +
         'for the job if you do not provide one using this argument.')
  parser.add_argument('--report-type', default=None,
    help='The type of report for which you are creating a job.')
  args = parser.parse_args()

  youtube_reporting = get_authenticated_service()

  try:
    # Prompt user to select report type if they didn't set one on command line.
    if not args.report_type:
      if list_report_types(youtube_reporting,
                           onBehalfOfContentOwner=args.content_owner,
                           includeSystemManaged=args.include_system_managed):
        args.report_type = get_report_type_id_from_user()
    # Prompt user to set job name if not set on command line.
    if not args.name:
      args.name = prompt_user_to_set_job_name()
    # Create the job.
    if args.report_type:
      create_reporting_job(youtube_reporting,
                           args,
                           onBehalfOfContentOwner=args.content_owner)
  except HttpError, e:
    print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)

例 2: レポートを取得する

コードサンプルは jobs.list メソッドを呼び出して、レポートジョブのリストを取得します。次に、jobId パラメータを特定のジョブ ID に設定して reports.list メソッドを呼び出し、そのジョブによって作成されたレポートを取得します。最後に、各レポートのダウンロード URL が出力されます。

#!/usr/bin/python

###
#
# This script retrieves YouTube Reporting API reports. Use cases:
# 1. If you specify a report URL, the script downloads that report.
# 2. Otherwise, if you specify a job ID, the script retrieves a list of
#    available reports for that job and prompts you to select a report.
#    Then it retrieves that report as in case 1.
# 3. Otherwise, the list retrieves a list of jobs for the user or,
#    if specified, the content owner that the user is acting on behalf of.
#    Then it prompts the user to select a job, and then executes case 2 and
#    then case 1.
# Usage examples:
# python retrieve_reports.py --content_owner_id=<CONTENT_OWNER_ID> --local_file=<LOCAL_FILE>
# python retrieve_reports.py --content_owner_id=<CONTENT_OWNER_ID> --job_id=<JOB_ID> --local_file=<LOCAL_FILE>
# python retrieve_reports.py --content_owner_id=<CONTENT_OWNER_ID> --report_url=<REPORT_URL> --local_file=<LOCAL_FILE>
#
###

import argparse
import os

import google.oauth2.credentials
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseDownload
from google_auth_oauthlib.flow import InstalledAppFlow
from io import FileIO


# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the {{ Google Cloud Console }} at
# {{ https://cloud.google.com/console }}.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
#   https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
#   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE = 'client_secret.json'

# This OAuth 2.0 access scope allows for read access to YouTube Analytics
# monetary reports for the authenticated user's account. Any request that
# retrieves earnings or ad performance metrics must use this scope.
SCOPES = ['https://www.googleapis.com/auth/yt-analytics-monetary.readonly']
API_SERVICE_NAME = 'youtubereporting'
API_VERSION = 'v1'

# Authorize the request and store authorization credentials.
def get_authenticated_service():
  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
  credentials = flow.run_console()
  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

# Remove keyword arguments that are not set.
def remove_empty_kwargs(**kwargs):
  good_kwargs = {}
  if kwargs is not None:
    for key, value in kwargs.iteritems():
      if value:
        good_kwargs[key] = value
  return good_kwargs

# Call the YouTube Reporting API's jobs.list method to retrieve reporting jobs.
def list_reporting_jobs(youtube_reporting, **kwargs):
  # Only include the onBehalfOfContentOwner keyword argument if the user
  # set a value for the --content_owner argument.
  kwargs = remove_empty_kwargs(**kwargs)

  # Retrieve the reporting jobs for the user (or content owner).
  results = youtube_reporting.jobs().list(**kwargs).execute()

  if 'jobs' in results and results['jobs']:
    jobs = results['jobs']
    for job in jobs:
      print ('Reporting job id: %s\n name: %s\n for reporting type: %s\n'
        % (job['id'], job['name'], job['reportTypeId']))
  else:
    print 'No jobs found'
    return False

  return True

# Call the YouTube Reporting API's reports.list method to retrieve reports created by a job.
def retrieve_reports(youtube_reporting, **kwargs):
  # Only include the onBehalfOfContentOwner keyword argument if the user
  # set a value for the --content_owner argument.
  kwargs = remove_empty_kwargs(**kwargs)

  # Retrieve available reports for the selected job.
  results = youtube_reporting.jobs().reports().list(
    **kwargs
  ).execute()

  if 'reports' in results and results['reports']:
    reports = results['reports']
    for report in reports:
      print ('Report dates: %s to %s\n       download URL: %s\n'
        % (report['startTime'], report['endTime'], report['downloadUrl']))


# Call the YouTube Reporting API's media.download method to download the report.
def download_report(youtube_reporting, report_url, local_file):
  request = youtube_reporting.media().download(
    resourceName=' '
  )
  request.uri = report_url
  fh = FileIO(local_file, mode='wb')
  # Stream/download the report in a single request.
  downloader = MediaIoBaseDownload(fh, request, chunksize=-1)

  done = False
  while done is False:
    status, done = downloader.next_chunk()
    if status:
      print 'Download %d%%.' % int(status.progress() * 100)
  print 'Download Complete!'


# Prompt the user to select a job and return the specified ID.
def get_job_id_from_user():
  job_id = raw_input('Please enter the job id for the report retrieval: ')
  print ('You chose "%s" as the job Id for the report retrieval.' % job_id)
  return job_id

# Prompt the user to select a report URL and return the specified URL.
def get_report_url_from_user():
  report_url = raw_input('Please enter the report URL to download: ')
  print ('You chose "%s" to download.' % report_url)
  return report_url

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--content_owner', default='',
      help='ID of content owner for which you are retrieving jobs and reports')
  parser.add_argument('--job_id', default=None,
      help='ID of the job for which you are retrieving reports. If not ' +
           'provided AND report_url is also not provided, then the script ' +
           'calls jobs.list() to retrieve a list of jobs.')
  parser.add_argument('--report_url', default=None,
      help='URL of the report to retrieve. If not specified, the script ' +
           'calls reports.list() to retrieve a list of reports for the ' +
           'selected job.')
  parser.add_argument('--local_file', default='yt_report.txt',
      help='The name of the local file where the downloaded report will be written.')
  args = parser.parse_args()

  youtube_reporting = get_authenticated_service()
  try:
    # If the user has not specified a job ID or report URL, retrieve a list
    # of available jobs and prompt the user to select one.
    if not args.job_id and not args.report_url:
      if list_reporting_jobs(youtube_reporting,
                             onBehalfOfContentOwner=args.content_owner):
        args.job_id = get_job_id_from_user()

    # If the user has not specified a report URL, retrieve a list of reports
    # available for the specified job and prompt the user to select one.
    if args.job_id and not args.report_url:
      retrieve_reports(youtube_reporting,
                       jobId=args.job_id,
                       onBehalfOfContentOwner=args.content_owner)
      args.report_url = get_report_url_from_user()

    # Download the selected report.
    if args.report_url:
      download_report(youtube_reporting, args.report_url, args.local_file)
  except HttpError, e:
    print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)