Przykłady kodu Java

Poniżej znajdziesz przykładowy kod, który korzysta z biblioteki klienta interfejsów API Google dla Java. Są one dostępne dla interfejsów YouTube Reporting API i YouTube Analytics API. Przykładowy kod możesz pobrać z folderu java repozytorium przykładowego kodu interfejsów API YouTube na GitHubie.

Raporty zbiorcze

Pobieranie raportów

Ten przykład pokazuje, jak pobrać raporty utworzone przez określone zadanie. Wywołuje on metodę jobs.list do pobierania zadań raportowania. Następnie wywołuje metodę reports.list z parametrem jobId ustawionym na konkretny identyfikator zadania, aby pobrać raporty utworzone przez to zadanie. Na koniec w przykładzie wydrukowany jest adres URL pobierania każdego raportu.

/*
 * 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.