2
votes

Following Google's documentation you should be able to use the YouTube Reporting API without user authentication by using a Service Account.

Currently I have a very simple sample that should give me a list of available Jobs, could be empty though. But I get a 403 permission denied.

Request:

$client = new \Google_Client();
$client->setAuthConfig('service-account-key.json');

$client->addScope(Google_Service_YouTubeReporting::YT_ANALYTICS_MONETARY_READONLY);
$client->addScope(Google_Service_YouTubeReporting::YT_ANALYTICS_READONLY);

$youtube_reporting_service = new \Google_Service_YouTubeReporting($client);

$jobs = $youtube_reporting_service->jobs->listJobs();

Response:

Google_Service_Exception

{
    "error": {
        "code": 403,
        "message": "The caller does not have permission",
        "errors": [
            {
                "message": "The caller does not have permission",
                "domain": "global",
                "reason": "forbidden"
            }
        ],
        "status": "PERMISSION_DENIED"
    }
}

I verified that the following APIs were enabled:

  • YouTube Reporting API
  • YouTube Data API v3
  • YouTube Analytics API
  • Google+ API (Having my doubts on this one if that is really necessary)

The thing I don't understand is how the relation should be between projects in Google Cloud Platform and the YouTube accounts.

Has this to do with Delegating domain-wide authority to the service account?

Are you then required to have G Suite enabled?

I see a similar question How to grant access to Youtube Reporting API for a google service account? exists, but that one may be a bit aged since the last response. I hope my samples and additions will shine a new light over this question.

Is there someone with a working sample? May be in other languages. As long as the flow is clear.

1

1 Answers

1
votes

In general, service accounts can be used to impersonate a user account. Making authorized calls is mentioned in Preparing to make an authorized API call.

But to answer the question, Service Accounts do not work with the YouTube API:

Service accounts do not work for YouTube Data API calls because service accounts require an associated YouTube channel, and you cannot associate new or existing channels with service accounts. If you use a service account to call the YouTube Data API, the API server returns an error with the error type set to unauthorized and the reason set to youtubeSignupRequired.