YouTube Reporting API - Data Model

Lưu ý quan trọng: Báo cáo API hằng ngày và báo cáo thay thế được cung cấp trong vòng 60 ngày kể từ thời điểm tạo. Các báo cáo có dữ liệu trong quá khứ được cung cấp trong vòng 30 ngày kể từ lúc tạo.

Chính sách này áp dụng trên toàn cầu cho mọi báo cáo và công việc báo cáo. Để biết toàn bộ thông tin chi tiết, hãy xem nhật ký sửa đổi của API Báo cáo YouTube.

API Báo cáo của YouTube hỗ trợ các báo cáo tạo sẵn chứa một tập hợp dữ liệu toàn diện trong YouTube Analytics của một kênh hoặc chủ sở hữu nội dung. Các báo cáo này cho phép bạn tải hàng loạt tập dữ liệu xuống mà bạn có thể truy vấn bằng API YouTube Analytics hoặc trong phần Analytics của YouTube Studio.

API này cũng hỗ trợ tập hợp các báo cáo do hệ thống quản lý được tạo tự động. Chủ sở hữu nội dung có quyền truy cập vào báo cáo tương ứng trong trình đơn Báo cáo. Các báo cáo đó chứa dữ liệu về doanh thu từ quảng cáo và dữ liệu về doanh thu từ gói thuê bao YouTube Premium. Hãy xem tài liệu về báo cáo do hệ thống quản lý để biết thêm thông tin.

Tổng quan

Các trường báo cáo trong những báo cáo này được mô tả dưới dạng phương diện hoặc chỉ số:

  • Phương diện là những tiêu chí phổ biến dùng để tổng hợp dữ liệu, chẳng hạn như ngày xảy ra một hành động hoặc quốc gia nơi người dùng sinh sống. Trong một báo cáo, mỗi hàng dữ liệu có một tổ hợp giá trị phương diện riêng biệt.
  • Chỉ số là các số liệu đo lường riêng lẻ liên quan đến hoạt động của người dùng, hiệu suất quảng cáo hoặc doanh thu ước tính. Chỉ số hoạt động của người dùng bao gồm các thông tin như số lượt xem video và điểm xếp hạng (lượt thích và lượt không thích).

Ví dụ: báo cáo hoạt động cơ bản của người dùng cho kênh chứa các phương diện sau:

  • day (ngày): Ngày xảy ra hoạt động.
  • channel: Kênh YouTube liên kết với hoạt động.
  • video: Video trên YouTube liên quan đến hoạt động này.
  • liveOrOnDemand: Một giá trị cho biết liệu người xem có đang xem một sự kiện phát video trực tiếp hay không.
  • subscribedStatus: Giá trị cho biết người xem đã đăng ký kênh hay chưa.
  • country: Quốc gia nơi người xem sinh sống.

Báo cáo này cũng chứa nhiều chỉ số, chẳng hạn như số lượt xem, số lượt thíchaverageViewDuration. Sau khi truy xuất và nhập báo cáo, ứng dụng có thể thực hiện nhiều phép tính khác nhau dựa trên các giá trị phương diện phổ biến.

Ví dụ: để tính tổng số lượt xem ở Đức vào một ngày cụ thể, hãy tính tổng giá trị chỉ số lượt xem cho tất cả các hàng trong đó giá trị cột countryDE và giá trị cột ngày là ngày ở định dạng YYYY-MM-DD.

Truy xuất báo cáo trong YouTube Analytics

Các bước sau đây sẽ giải thích cách truy xuất báo cáo của chủ sở hữu nội dung và kênh qua API. Hãy xem tài liệu về báo cáo do hệ thống quản lý để biết hướng dẫn về cách truy xuất các báo cáo đó.

Bước 1: Truy xuất thông tin xác thực uỷ quyền

Tất cả yêu cầu API Báo cáo của YouTube đều phải được cho phép. Hướng dẫn uỷ quyền giải thích cách sử dụng giao thức OAuth 2.0 để truy xuất mã thông báo uỷ quyền.

Các yêu cầu API Báo cáo của YouTube sử dụng các phạm vi uỷ quyền sau:

Phạm vi
https://www.googleapis.com/auth/yt-analytics.readonly Xem các báo cáo trong YouTube Analytics về nội dung của bạn trên YouTube. Phạm vi này cung cấp quyền truy cập vào các chỉ số hoạt động của người dùng, chẳng hạn như số lượt xem và số lượt xếp hạng.
https://www.googleapis.com/auth/yt-analytics-monetary.readonly Xem báo cáo tiền trong YouTube Analytics cho nội dung của bạn trên YouTube. Phạm vi này cung cấp quyền truy cập vào các chỉ số hoạt động của người dùng, cũng như doanh thu ước tính và các chỉ số hiệu suất quảng cáo.

Bước 2: Xác định báo cáo cần truy xuất

Gọi phương thức reportTypes.list của API để truy xuất danh sách các báo cáo có thể tạo cho kênh hoặc chủ sở hữu nội dung. Phương thức này trả về danh sách tên và mã báo cáo. Thu thập giá trị thuộc tính id cho các báo cáo mà bạn muốn tạo. Ví dụ: mã nhận dạng của báo cáo hoạt động cơ bản của người dùng cho các kênh là channel_basic_a1.

Bạn cũng có thể tìm thấy tên báo cáo trong tài liệu xác định báo cáo kênhbáo cáo của chủ sở hữu nội dung được hỗ trợ.

Bước 3: Tạo công việc báo cáo

YouTube không bắt đầu tạo báo cáo của bạn cho đến khi bạn tạo công việc báo cáo cho báo cáo đó. (Do đó, báo cáo chỉ được tạo cho các kênh và chủ sở hữu nội dung thực sự muốn truy xuất các báo cáo đó.)

Để tạo một công việc báo cáo, hãy gọi phương thức jobs.create của API. Thiết lập các giá trị sau trong phần nội dung yêu cầu:

  • Đặt giá trị của tài sản reportTypeId thành mã báo cáo mà bạn truy xuất ở bước 2.
  • Đặt giá trị của tài sản name thành tên mà bạn muốn liên kết với báo cáo.

Phản hồi của API cho phương thức jobs.create chứa tài nguyên Job chỉ định ID xác định duy nhất công việc. Bạn có thể bắt đầu truy xuất báo cáo trong vòng 48 giờ sau khi tạo công việc và báo cáo có sẵn đầu tiên sẽ là cho ngày mà bạn đã lên lịch công việc.

Ví dụ: nếu bạn lên lịch cho công việc vào ngày 1 tháng 9, thì báo cáo cho ngày 1 tháng 9 sẽ sẵn sàng vào ngày 3 tháng 9. Báo cáo cho ngày 2 tháng 9 sẽ được đăng vào ngày 4 tháng 9, v.v.

Bước 4: Truy xuất mã công việc

Lưu ý: Nếu ứng dụng của bạn đã lưu trữ mã công việc được trả về ở bước 3 thì bạn có thể bỏ qua bước này.

Gọi phương thức jobs.list để truy xuất danh sách các công việc theo lịch. Thuộc tính reportTypeId trong mỗi tài nguyên Job được trả về sẽ xác định loại báo cáo mà công việc đó tạo ra. Ứng dụng của bạn cần giá trị thuộc tính id từ cùng một tài nguyên ở bước sau.

Bước 5: Truy xuất URL tải xuống của báo cáo

Gọi phương thức jobs.reports.list để truy xuất danh sách báo cáo đã tạo cho công việc. Trong yêu cầu, hãy đặt tham số jobId thành mã công việc của báo cáo mà bạn muốn truy xuất.

Lưu ý: Hãy dùng tham số createdAfter để cho biết rằng API chỉ nên trả về các báo cáo được tạo sau một khoảng thời gian nhất định. Bạn có thể dùng tham số này để đảm bảo rằng API chỉ trả về các báo cáo mà bạn chưa xử lý.

Phản hồi của API chứa danh sách tài nguyên Report cho công việc đó. Mỗi tài nguyên là một báo cáo chứa dữ liệu trong khoảng thời gian 24 giờ duy nhất. Xin lưu ý rằng YouTube có tạo báo cáo có thể tải xuống cho những ngày không có dữ liệu. Những báo cáo đó chứa hàng tiêu đề nhưng không chứa thêm dữ liệu.

  • Các thuộc tính startTimeendTime của tài nguyên này xác định khoảng thời gian chứa dữ liệu của báo cáo.
  • Thuộc tính downloadUrl của tài nguyên xác định URL mà từ đó báo cáo có thể được tìm nạp.
  • Thuộc tính createTime của tài nguyên chỉ định ngày và giờ tạo báo cáo. Ứng dụng của bạn nên lưu trữ giá trị này và dùng giá trị này để xác định xem các báo cáo đã tải xuống trước đó có thay đổi hay không.

Bước 6: Tải báo cáo xuống

Gửi yêu cầu HTTP GET tới downloadUrl có được ở bước 5 để truy xuất báo cáo.

Bạn có thể giảm băng thông cần thiết để tải báo cáo xuống bằng cách bật tính năng nén gzip đối với các yêu cầu tải xuống. Mặc dù ứng dụng của bạn sẽ cần thêm thời gian của CPU để giải nén các phản hồi API, nhưng lợi ích của việc tiêu thụ ít tài nguyên mạng hơn thường lớn hơn chi phí đó.

Để nhận phản hồi được mã hoá bằng gzip, hãy đặt tiêu đề của yêu cầu HTTP Accept-Encoding thành gzip như trong ví dụ sau:

Accept-Encoding: gzip

Xử lý báo cáo

Các phương pháp hay nhất

Ứng dụng dùng API Báo cáo của YouTube luôn phải tuân theo những phương pháp sau:

  • Để xác định thứ tự các cột của báo cáo, hãy sử dụng hàng tiêu đề của báo cáo. Ví dụ: đừng giả định rằng lượt xem sẽ là chỉ số đầu tiên được trả về trong một báo cáo chỉ vì đó là chỉ số đầu tiên được liệt kê trong phần mô tả của báo cáo. Thay vào đó, hãy sử dụng hàng tiêu đề của báo cáo để xác định cột nào chứa dữ liệu đó.

  • Để tránh liên tục xử lý cùng một báo cáo, hãy lưu giữ bản ghi của các báo cáo mà bạn đã tải xuống. Danh sách sau đây đề xuất một số cách để thực hiện việc đó.

    • Khi gọi phương thức reports.list, hãy sử dụng tham số createdAfter để chỉ truy xuất các báo cáo được tạo sau một ngày nhất định. (Hãy bỏ qua tham số createdAfter trong lần đầu tiên bạn truy xuất báo cáo.)

      Mỗi khi bạn truy xuất và xử lý thành công báo cáo, hãy lưu trữ dấu thời gian tương ứng với ngày và giờ mà báo cáo mới nhất trong số đó được tạo. Sau đó, hãy cập nhật giá trị thông số createdAfter trong mỗi lệnh gọi liên tiếp đến phương thức reports.list để đảm bảo rằng bạn chỉ truy xuất các báo cáo mới (bao gồm cả các báo cáo mới có dữ liệu được bổ sung) mỗi khi bạn gọi API này.

      Để bảo vệ, trước khi truy xuất một báo cáo, bạn cũng nên kiểm tra để đảm bảo rằng mã nhận dạng của báo cáo đó chưa được liệt kê trong cơ sở dữ liệu của bạn.

    • Lưu trữ mã nhận dạng cho mỗi báo cáo mà bạn đã tải xuống và xử lý. Bạn cũng có thể lưu trữ thông tin bổ sung như ngày và giờ khi mỗi báo cáo được tạo hoặc startTimeendTime của báo cáo để cùng xác định khoảng thời gian báo cáo chứa dữ liệu. Lưu ý rằng mỗi công việc có thể sẽ có nhiều báo cáo do mỗi báo cáo đều chứa dữ liệu cho khoảng thời gian 24 giờ.

      Sử dụng mã báo cáo để xác định những báo cáo mà bạn vẫn cần tải xuống và nhập. Tuy nhiên, nếu hai báo cáo mới có cùng giá trị thuộc tính startTimeendTime, thì bạn chỉ nên nhập báo cáo có giá trị createTime mới hơn.

  • Báo cáo chứa ID được liên kết với các tài nguyên của YouTube. Để truy xuất siêu dữ liệu bổ sung cho các tài nguyên đó, hãy sử dụng YouTube Data API. Như đã nêu trong Chính sách dành cho nhà phát triển dịch vụ API YouTube (mục III.E.4.b đến III.E.4.d), các ứng dụng API phải xoá hoặc làm mới siêu dữ liệu tài nguyên được lưu trữ của API đó sau 30 ngày.

Đặc điểm báo cáo

Báo cáo API là các tệp .csv (giá trị được phân tách bằng dấu phẩy) được tạo phiên bản có các đặc điểm sau:

  • Mỗi báo cáo chứa dữ liệu cho một khoảng thời gian 24 giờ duy nhất kéo dài từ 12:00 sáng đến 11:59 đêm theo giờ Thái Bình Dương. Do đó, trong bất kỳ báo cáo nhất định nào, giá trị thứ nguyên ngày luôn giống nhau.

  • Báo cáo được cập nhật hằng ngày.

  • YouTube tạo báo cáo có thể tải xuống cho những ngày không có dữ liệu. Các báo cáo đó sẽ chứa hàng tiêu đề nhưng không chứa thêm dữ liệu.

  • Báo cáo API được cung cấp trong vòng 60 ngày kể từ ngày tạo, ngoại trừ dữ liệu trong quá khứ được tạo cho các công việc báo cáo mới. Không thể truy cập báo cáo sau khi đã quá 60 ngày. Báo cáo đã quá 60 ngày.
  • Báo cáo có chứa dữ liệu trong quá khứ được cung cấp trong vòng 30 ngày kể từ thời điểm tạo. Bạn sẽ không thể truy cập vào những báo cáo chứa dữ liệu trong quá khứ sau khi chúng đã quá 30 ngày.
  • Dữ liệu báo cáo không được lọc. Do đó, báo cáo kênh chứa tất cả dữ liệu về video hoặc danh sách phát của kênh với ngoại lệ được ghi chú trong đoạn sau liên quan đến tài nguyên đã xóa. Tương tự, báo cáo của chủ sở hữu nội dung chứa toàn bộ dữ liệu về kênh của chủ sở hữu nội dung (video, danh sách phát, hiệu suất quảng cáo, v.v.) với ngoại lệ sau.

    Mặc dù dữ liệu báo cáo không được lọc, nhưng báo cáo không chứa bất kỳ tham chiếu nào đến các tài nguyên trên YouTube đã bị xóa ít nhất 30 ngày trước ngày tạo báo cáo.

  • Dữ liệu báo cáo không được sắp xếp.

  • Báo cáo bỏ qua các hàng không có chỉ số. Nói cách khác, những hàng không có bất kỳ chỉ số nào sẽ bị loại trừ khỏi báo cáo. Ví dụ: nếu video không có lượt xem nào ở Albania vào một ngày cụ thể, báo cáo của ngày đó sẽ không chứa các hàng cho Albania.

  • Báo cáo không chứa các hàng cung cấp dữ liệu tóm tắt về các chỉ số, chẳng hạn như tổng số lượt xem cho tất cả video của một kênh. Bạn có thể tính tổng giá trị đó dưới dạng tổng giá trị trong báo cáo, nhưng tổng đó có thể không bao gồm các chỉ số của video bị xóa, như đã nêu ở trên. Bạn cũng có thể sử dụng API YouTube Analytics để truy xuất tổng số. API YouTube Analytics trả về tổng giá trị bao gồm chỉ số cho các tài nguyên đã xoá mặc dù những tài nguyên đó không được đề cập rõ ràng trong các phản hồi của API.

Bổ sung dữ liệu cũ

Dữ liệu bổ sung là một tập dữ liệu thay thế tập dữ liệu đã phân phối trước đó. Khi có báo cáo dữ liệu thay thế, ứng dụng của bạn sẽ truy xuất báo cáo mới và cập nhật dữ liệu đã lưu trữ cho phù hợp với tập dữ liệu đã sửa đổi. Ví dụ: ứng dụng của bạn có thể xoá dữ liệu trước đó trong khoảng thời gian được đề cập trong báo cáo, sau đó nhập tập dữ liệu mới.

Nếu có dữ liệu thay thế, YouTube sẽ tạo một báo cáo mới có mã báo cáo mới. Trong trường hợp đó, giá trị thuộc tính startTimeendTime của báo cáo sẽ khớp với thời gian bắt đầu và thời gian kết thúc của một báo cáo đã có sẵn và có thể bạn đã tải xuống trước đó.

Báo cáo bổ sung không chứa bất kỳ thông tin tham chiếu nào đến các tài nguyên trên YouTube đã bị xoá ít nhất 30 ngày trước ngày tạo báo cáo.

Dữ liệu trong quá khứ

Khi bạn lên lịch cho một công việc báo cáo mới, YouTube sẽ tạo các báo cáo trước đây từ ngày đó trở đi và tạo các báo cáo cho khoảng thời gian 30 ngày trước thời điểm bạn tạo công việc. Do đó, trong tài liệu này, dữ liệu trong quá khứ là báo cáo chứa dữ liệu trong một khoảng thời gian trước khi công việc báo cáo được lên lịch.

Các báo cáo trong quá khứ sẽ được đăng ngay khi có sẵn. Thông thường, tất cả dữ liệu trong quá khứ sẽ được đăng cho một công việc trong vòng vài ngày. Như được giải thích trong phần Đặc điểm báo cáo, báo cáo chứa dữ liệu trong quá khứ được cung cấp trong 30 ngày kể từ thời điểm tạo. Những báo cáo chứa dữ liệu không phải trong quá khứ được cung cấp trong 60 ngày.

Ẩn danh dữ liệu

Để đảm bảo tính ẩn danh của người xem YouTube, hệ thống chỉ trả về giá trị của một số phương diện nếu một chỉ số trong cùng một hàng đáp ứng một ngưỡng nhất định.

Ví dụ: trong báo cáo nguồn lưu lượng truy cập video cho các kênh, mỗi hàng chứa một số phương diện, bao gồm trafficSourceTypetrafficSourceDetail. Mỗi hàng cũng chứa các chỉ số khác nhau, bao gồm cả số lượt xem. Trong các hàng mô tả lưu lượng truy cập bắt nguồn từ một tìm kiếm trên YouTube, phương diện trafficSourceDetail sẽ xác định cụm từ tìm kiếm đã dẫn đến lưu lượng truy cập đó.

Trong ví dụ này, các quy tắc sau sẽ áp dụng:

  • Báo cáo nguồn lưu lượng truy cập chỉ xác định cụm từ truy vấn (trafficSourceDetail) nếu cụm từ đó mang lại ít nhất một số lượt xem nhất định của một video cụ thể vào một ngày cụ thể. Trong trường hợp này, lượt xem là chỉ số, video là phương diện tổng hợp và trafficSourceDetail là phương diện ẩn danh.

  • Báo cáo này bao gồm một hàng bổ sung để tổng hợp các chỉ số cho tất cả giá trị trafficSourceDetail không đáp ứng ngưỡng số lượt xem. Hàng đó báo cáo tổng số lượt xem có liên quan đến các cụm từ truy vấn đó nhưng không xác định chính các cụm từ.

Các bảng sau minh hoạ các quy tắc này. Bảng đầu tiên chứa tập dữ liệu thô giả định mà YouTube sẽ sử dụng để tạo báo cáo nguồn lưu lượng truy cập và bảng thứ hai chứa chính báo cáo đó. Trong ví dụ này, ngưỡng số lượt xem là 10, nghĩa là báo cáo chỉ xác định một cụm từ tìm kiếm nếu cụm từ đó mang lại ít nhất 10 lượt xem cho một video cụ thể vào một ngày cụ thể. (Ngưỡng thực tế có thể thay đổi.)

Dữ liệu thô về lưu lượng truy cập tìm kiếm trên YouTube cho một video

Giả sử rằng dữ liệu dưới đây mô tả lưu lượng tìm kiếm trên YouTube cho một video cụ thể vào một ngày cụ thể.

cụm từ tìm kiếm views số phút đã xem ước tính
phong cách gangnam 100 200
tâm thần 15 25
tâm thần gangnam 9 15
oppa gangnam 5 8
nhảy ngựa 2 5

Báo cáo nguồn lưu lượng truy cập mẫu

Bảng sau đây hiển thị phần trích dẫn từ báo cáo nguồn lưu lượng truy cập mà YouTube sẽ tạo cho dữ liệu thô trong phần trước. (Báo cáo thực tế sẽ chứa nhiều phương diện và chỉ số hơn.) Trong ví dụ này, báo cáo chỉ xác định cụm từ tìm kiếm nếu các cụm từ đó mang lại ít nhất 10 lượt xem. Các ngưỡng thực tế có thể thay đổi.

Trong hàng thứ ba của báo cáo, giá trị phương diện trafficSourceDetailNULL. Chỉ số viewsestimatedMinutesWatched bao gồm cả số lượt xem và số phút đã xem đối với ba cụm từ tìm kiếm tạo ra dưới 10 lượt xem.

trafficSourceDetail views estimatedMinutesWatched
phong cách gangnam 100 200
tâm thần 15 25
NULL 16 28

Các phương diện được ẩn danh

Bảng sau đây xác định các giá trị phương diện sẽ bị ẩn danh nếu các giá trị chỉ số có liên quan không đáp ứng một ngưỡng nhất định. Trong mỗi trường hợp, giá trị của chỉ số này được tổng hợp qua một phương diện khác. Ví dụ: nếu chỉ số là số lượt xem và phương diện tổng hợp là video, thì giá trị phương diện sẽ được ẩn danh, trừ phi video đó được xem nhiều lần.

Chỉ số Tổng hợp(các) phương diện Phương diện ẩn danh Giá trị ẩn danh
subscribersGained kênh country ZZ
subscribersGained kênh province US-ZZ
subscribersLost kênh country ZZ
subscribersLost kênh province US-ZZ
bình luận video country ZZ
bình luận video province US-ZZ
lượt thích video country ZZ
lượt thích video province US-ZZ
lượt không thích video country ZZ
lượt không thích video province US-ZZ
lượt xem video ageGroup NULL
lượt xem video gender [giới_tính] NULL
lượt xem videotrafficSourceDetail trafficSourceDetail NULL
Số người đăng ký kênh kênh subscribedStatus NULL

Mã mẫu

Các mã mẫu sau đây minh hoạ cách sử dụng API để tạo một công việc báo cáo rồi truy xuất báo cáo cho công việc đó. Hai mã mẫu được cung cấp cho từng ngôn ngữ:

  1. Mã mẫu đầu tiên cho biết cách truy xuất danh sách các loại báo cáo có sẵn, sau đó tạo công việc báo cáo mới.

  2. Mã mẫu thứ hai cho biết cách truy xuất báo cáo cho một công việc cụ thể. Bạn có thể bắt đầu truy xuất báo cáo trong vòng 48 giờ sau khi tạo công việc.

Lưu ý: Các mã mẫu sau có thể không đại diện cho tất cả các ngôn ngữ lập trình được hỗ trợ. Hãy xem tài liệu về thư viện ứng dụng để biết danh sách các ngôn ngữ được hỗ trợ.

Java

Các mẫu sau đây sử dụng thư viện ứng dụng Java:

Ví dụ 1: Tạo công việc báo cáo

Mã mẫu sau đây gọi phương thức reportTypes.list để truy xuất danh sách các loại báo cáo có sẵn. Sau đó, phương thức này sẽ gọi phương thức jobs.create để tạo một công việc báo cáo mới.

/*
 * 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;
    }
}

Ví dụ 2: Truy xuất báo cáo

Mã mẫu này gọi phương thức jobs.list để truy xuất danh sách công việc báo cáo. Sau đó, phương thức này sẽ gọi phương thức reports.list có tham số jobId được đặt thành một mã công việc cụ thể để truy xuất các báo cáo do công việc đó tạo. Cuối cùng, mẫu in ra URL tải xuống cho mỗi báo cáo.

/*
 * 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;
    }}

1.199

Các mẫu sau đây sử dụng thư viện ứng dụng PHP.

Ví dụ 1: Tạo công việc báo cáo

Mã mẫu sau đây gọi phương thức reportTypes.list để truy xuất danh sách các loại báo cáo có sẵn. Sau đó, phương thức này sẽ gọi phương thức jobs.create để tạo một công việc báo cáo mới.

<?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>

Ví dụ 2: Truy xuất báo cáo

Mã mẫu này gọi phương thức jobs.list để truy xuất danh sách công việc báo cáo. Sau đó, phương thức này sẽ gọi phương thức reports.list có tham số jobId được đặt thành một mã công việc cụ thể để truy xuất các báo cáo do công việc đó tạo. Cuối cùng, mẫu in ra URL tải xuống cho mỗi báo cáo.

<?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

Các mẫu sau sử dụng thư viện ứng dụng Python.

Ví dụ 1: Tạo công việc báo cáo

Mã mẫu sau đây gọi phương thức reportTypes.list để truy xuất danh sách các loại báo cáo có sẵn. Sau đó, phương thức này sẽ gọi phương thức jobs.create để tạo một công việc báo cáo mới.

#!/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)

Ví dụ 2: Truy xuất báo cáo

Mã mẫu này gọi phương thức jobs.list để truy xuất danh sách công việc báo cáo. Sau đó, phương thức này sẽ gọi phương thức reports.list có tham số jobId được đặt thành một mã công việc cụ thể để truy xuất các báo cáo do công việc đó tạo. Cuối cùng, mẫu in ra URL tải xuống cho mỗi báo cáo.

#!/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)