Using the YouTube Analytics API with a CMS account - youtube-api

I'm getting a 403 Forbidden error when trying to get youtube analytics api data using a CMS account.
Just to confirm, is a CMS account the same thing as a Service account?
I can get analytics data for channels that are owned by the oauth2 user but I get the 403 on any other channels that I have access to through my CMS account but am not the content owner of. (I have Administrator account level on the CMS account and the channels I get 403 error on have "Managed" relationship)
Question: Are there any plans to have youtube.analytics api support for querying channels managed under a CMS account?
It seems that since I can use that account to get all the analytics data for these channels using the CMS UI that I should be able to do the same using the API.

A YouTube Content Management System account is not the same thing as a Service Account. You won't get far with the YouTube APIs if you authenticate as a Service Account, since that Service Account won't have access to any actual YouTube channels. Authenticating as a CMS account, however, will give you what you want.
A lot of this recently made it into production, so let me lay out the full steps here explaining how folks with YouTube CMS access can run YouTube Analytics API reports against the channels and videos they manage.
(Optional) While authorized via OAuth 2 as the CMS account, using both the https://www.googleapis.com/auth/youtube.readonly and https://www.googleapis.com/auth/youtubepartner scopes, make a YouTube Data API v3 channels.list() call with the following parameters: part=snippet,contentDetails, managedByMe=true, maxResults=50, onBehalfOfContentOwner=CONTENT_OWNER_ID. CONTENT_OWNER_ID is the one value that you'll have to hardcode here, and it should be set to the "partner code" for your YouTube CMS account. This will give you back a list of up to 50 channels that your CMS account manages. (If you need more than 50, you'll need to page through the results.) The id of each channel will be returned in channel.id, and other useful information (like the uploads list id, if you want to get the list of videos in that channel) will also be returned.
If you already know the UC... channel id for the channel you want to run reports against, you could skip that step and go directly to the Analytics report.
To run a channel-level report on a managed channel, while authorized as the CMS account, make a YouTube Analytics API request with the following parameters: ids=contentOwner==CONTENT_OWNER_ID, filters=channel==UC..., and then any other report parameters you want.
To run a video-level report, set ids=contentOwner==CONTENT_OWNER_ID and filters=video==VIDEO_ID, where VIDEO_ID is the id of any video in any channel that the CMS account manages.
There are more details about content owner reports in the docs.

Related

How do I use the YouTube Data API to upload video to secondary channel on personal account

I have looked around Stack Overflow and seen a few posts about this but none of the solutions help.
I have a Google account which I use in YouTube. I have created a second channel on that YouTube account so that I can upload videos with a specific theme to separate them from the main videos.
Trying to use the Google API to upload the videos so that I can run it via a Python script, I keep hitting brick walls with Google who is looking for app verification, privacy policies and web page links - none of which I have.
This application is a Python script that's not available to the public and doesn't gather any public information. All I am trying to do is upload videos to my own personal YouTube account.
So I'm beginning to think it is something else I should be using rather than the API (the uploading web page isn't suitable for use in a script).
My two question are:
Can I use the YouTube API to upload a video directly to the second channel on my personal YouTube account?
Is there another simpler mechanism I should be using to upload videos via a script to my personal YouTube account? The reason I have to do it via script is that the device is unattended.
Thanks,
David
You have to acknowledge that each and every app (this to be understood in a broad sense that includes even a small script like this one from Google upload_video.py) must be verified and approved by Google prior to be able to make videos publicly available via the YouTube site.
Answer to question no. 1: yes, that is perfectly possible.
As part of the OAuth 2.0 authentication/authorization flow, you will be presented, within the browser, with the option of selecting to which account your app is to be given access rights.
You may well exercise this behavior, prior to making use of your script, with the help of Google Developers OAuth 2.0 Playground.
Upon a successful OAuth flow, you may verify (and also revoke) the permissions granted by your account on the account's permissions page.
Answer to question no. 2: no, there's no way to upload programmatically videos on YouTube that's in compliance with YouTube's DTOS, other than using the Videos.insert API endpoint.
Addendum
Since by now you have at least two credentials sets, it may be of need to know to which of your YouTube channels a given credentials object is associated.
If using the Google APIs Client Library for Python, you may easily obtain from the API the channel ID to which a given credentials object CREDENTIALS is associated by issuing a call to the Channels.list API endpoint, passing to it the parameter mine as mine=true:
from googleapiclient.discovery import build
youtube = build(
'youtube', 'v3',
credentials = CREDENTIALS)
response = youtube.channels().list(
mine = 'true',
part = 'id',
fields = 'items(id)',
maxResults = 1
).execute()
channel_id = response['items'][0]['id']
Note that the code above uses the fields request parameter for to obtain from the Channels.list endpoint only the channel's ID info (it is always good to ask from the API only the info that is of actual use).
A caveat using the above procedure is the following: if a given CREDENTIALS instance has its scopes containing only:
https://www.googleapis.com/auth/youtube.upload,
then the API will respond with an error of type insufficientPermissions and of message Request had insufficient authentication scopes.
For to invoke successfully the Channels.list it would be sufficient that the scopes attached to CREDENTIALS to include either of the one below:
https://www.googleapis.com/auth/youtube.readonly,
https://www.googleapis.com/auth/youtube.

Youtube Content ID API has not been used - error 403

I'm trying to get my content-owner-id for upload videos to other channels that I have, as described here.
I'm using the code below to get content-owner-id: (PHP)
$youtubePartner = new Google_Service_YouTubePartner($this->client);
$contentOwnersListResponse = $youtubePartner->contentOwners->listContentOwners(
array('fetchMine' => true));
$contentOwnerId = $contentOwnersListResponse['items'][0]['id'];
And this is the error I got:
A service error occurred: Error calling GET
googleapis.com/youtube/partner/v1/contentOwners?fetchMine=true:
(403) Access Not Configured. Youtube Content ID API has not been used
in project XXXXXXXXXXXXX before or it is disabled. Enable it by
visiting
console.developers.google.com/apis/api/youtubePartner/overview?project=XXXXXXXXXX
then retry. If you enabled this API recently, wait a few minutes for
the action to propagate to our systems and retry.
I tried to go the the link https://console.developers.google.com/apis/api/youtubePartner/overview?project=XXXXXXXXXX but I get "The API doesn't exist or you don't have permission to access it".
Then I searched on google and find out that I need to become a CMS youtube account (I'm also a youtube partner). I also tried to go to cms.youtube.com and log in with my youtube account but I got:
Your Google Account is not linked with YouTube Content Manager. Please
try a different account or contact your Technical Account Manager for
assistance.
Can anyone explain me how to connect to CMS account?
Based from this thread:
You don't see it in the list auf available APIs, unless your account is connected to a CMS and some time has past... It takes 7-14 days unless the Content ID API is available for your account. This is a information i got from the support, but they told me, that it is an automated step.
Your account needs to be connected with a content owner. Be noted that a content owner and a partnered channel are not the same thing.
From this documentation, to request data from a YouTube Content Owner, you need to set the ids parameter value to contentOwner==OWNER_NAME, where OWNER_NAME is the content owner ID.
Check these related SO questions:
How do I get revenue reports from a YouTube CMS account using an API (for an MCN)?
Using the YouTube Analytics API with a CMS account

Unable to OAuth with account that owns YouTube channel

I have a google account that I was previously uploading videos to using the v2 YouTube API. This account I guess has two accounts linked to it, because when I go through OAuth flows I have to choose which account.
The 1st account, the one I setup the API in developer console, gives me the option to authorize YouTube API controls.
The 2nd account, the one that actually is the YouTube channel, only says that I'm giving my application offline access, there is no permissions specified relating to YouTube.
I did my OAuth using the 1st account, since that one actually listed youtube permissions. Got my refresh token and added it to the application. However, when I go to attempt to insert a video to YouTube I get the following error:
Code 401 Unauthorized: youtubeSignupRequired
It's as though I'm not linking at all to the YouTube channel account even though they are both on the same Google account.
Has anyone encountered this before?
By the way, I know with certainty that my API calls work properly, as we were able to upload from the dev environment. The dev account is only a single account though that owns the channel on the same account as the API.
I was able to resolve this.
It turned out I needed to do the OAuth with the 2nd account, the one for the YouTube channel. Despite it not requesting any permissions related to YouTube and only getting offline access, I was able to successfully upload using the refresh token I received.
Definitely a case of the most illogical solution being the correct one.

YouTube API to upload to Business Channel

Is there any way of using the YouTube API so that users can upload videos directly to my business YouTube channel? Users would grant permission to upload to my channel on the website. I want to avoid uploading to another server, then to YouTube if possible.
Thanks
You can use service account OAuth2 to authorize requests from your app on your behalf to a channel that your account is authorized to access/the owner of.
Create service account credentials: (documentation here)
and make sure the YouTube Data API v3 is enabled on your API
project (getting started guide here, if you're new to the API).
Use your service account OAuth credentials to call the upload API: (documentation here) use the auth in step 1 to upload the video your users share with you and specify your business YouTube channel ID in the video resource. Sample code in various languages here (just search for upload examples after you choose your preferred language).
Things to keep in mind:
If you enable uploads to your channel by end users but don't do any checking/validation there is potential for abuse
service account OAuth is valid for 1 year, so you'd need to refresh the service account credentials every year
It will look like these videos are being uploaded by your business channel and not other user's channels (because that's what's happening via the API).

Get YouTube playlist videos anonymously

I would like to display a list of videos from a YouTube playlist in an intranet application.
Is it possible to get the list of videos from a YouTube playlist using the Data api (or any other way) without requiring the user to login?
Everything that I have read so far in the YouTube data api requires the user to be signed in to authenticate.
unfortunately, you can't access youtube data API with anonymously user.
Your application must have authorization credentials to be able to use the YouTube Data API.
The Developers Console associates your credentials with the APIs that you indicate that your application will use. Note that the Developers Console does not allow you to select the YouTube Data API (v2). However, authorization credentials for the v3 API will also work for the v2 API.
If possible, you should actually use YouTube Data API (v3) rather than the v2 API in your application. The YouTube API blog explains some of the benefits that the newer API offers, and we have added a year's worth of additional functionality to the API since that blog post!
related link : https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Client_Side_Web_Applications_Flow

Resources