9 – YouTube Data API – Activities – insert function

This is article 9 of the YouTube API With PHP series.

We now see how to add Activities by using the API. This feature has very limited use and can only used to update the bulletin of a Channel. The reason being that all the other Activities are generated by other API entities. Eg.A video upload is done by the Video entity, adding an item to a Playlist is done with the Playlist entity  etc. Thus the Activity Insert option has just a single feature.

The Request URL is

POST https://www.googleapis.com/YouTube/v3/activities

Parameters

  • snippet
    • description (string) the message string of the bulletin
  • contentDetails
    • bulletin
      • resourceId
        • kind (string) One of the following:
          • “YouTube#channel”
          • “YouTube#playlist”
          • “YouTube#video”
        • videoId (string) only if kind is “YouTube#video”
        • playlistId (string) only if kind is “YouTube#playlist”
        • channelId (string) only if kind is “YouTube#channel”

 

Overview

 First things first. As of May 2017, you cannot see or post the bulletins in your channel, as earlier. YouTube has hidden it from channel owners for some reason. But it still works. The way to see and update the bulletin for your channel is to add a “/feed” suffix to your channel URL in the browser.

So if the channel URL is

https://www.YouTube.com/channel/UCnXmfpAZ1rLsg0Goh0bBHUA/

make it

https://www.YouTube.com/channel/UCnXmfpAZ1rLsg0Goh0bBHUA/feed

 

and you will get the bulletin messages for the channel.

You can post three kinds of bulletins

  1. A video from your own channel
  2. A playlist created by you
  3. A text only bulletin

 

When the API call is successful it returns a new Activity JSON object. Example code for all the three types are given below. The explanation of the code follows after that.

Sample code to post a video in the bulletin.

<?php
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
    set_time_limit(60 * 3);
    session_start();

    $clientId = "****";
    $clientSecret = "***";
    $g_YouTubeDataAPIKey = "****";


    $_SESSION["code_id"] = $_SERVER["PHP_SELF"];
    
    if ($_SESSION["access_token"] == null || $_SESSION["access_token"] == "") {
   	 // check for oauth response
   	 header("Location: ../../init-login.php");
   	 exit;
    }
   	 
   	 
   	 $accessToken = $_SESSION["access_token"];
    
   	 // create Activity resource for submission

   	 $resourceId = new ResourceId();
   	 $resourceId->kind = "YouTube#video";
   	 $resourceId->videoId = "****";
   	 
   	 $bulletin = new Bulletin();
   	 $bulletin->resourceId = $resourceId;

   	 $contentDetails = new ContentDetails();
   	 $contentDetails->bulletin = $bulletin;

   	 $snippet = new Snippet();
   	 $snippet->description = "Thank you for visiting my Channel. Please Like and Subscribe";

   	 $resource = new ActivityResource();
   	 $resource->snippet = $snippet;
   	 $resource->contentDetails = $contentDetails;

   	 
   	 $data = $resource;
   	 $data = json_encode($data);
   	 
   	 // make api request
   	 $url = "https://www.googleapis.com/YouTube/v3/activities?part=snippet,contentDetails&key=" .
   			 $g_YouTubeDataAPIKey . "&access_token=" . $accessToken;

   	 $curl = curl_init();
   	 curl_setopt_array($curl, array(
   				 CURLOPT_HTTPHEADER=>array('Content-Type: application/json'),
   				 CURLOPT_RETURNTRANSFER => 1,
   				 CURLOPT_URL => $url,
   				 CURLOPT_USERAGENT => 'YouTube API Tester',
   				 CURLOPT_SSL_VERIFYPEER => 1,
   				 CURLOPT_SSL_VERIFYHOST=> 0,
   				 CURLOPT_CAINFO => "../../cert/cacert.pem",
   				 CURLOPT_CAPATH => "../../cert/cacert.pem",
   				 CURLOPT_FOLLOWLOCATION => TRUE,
   				 CURLOPT_CUSTOMREQUEST=>"POST",
   				 CURLOPT_POSTFIELDS=>$data
   				 
   				 ));
   	 $resp = curl_exec($curl);

   	 curl_close($curl);

   	 var_dump($resp);
   	 
   	 if ($resp) {
   	 } // if resp


////////////////////

    class ResourceId {
   	 public $kind;
   	 public $videoId;
    }

    class Bulletin {
   	 public $resourceId;
    }
    class ContentDetails {
   	 public $bulletin;
    }
    class Snippet {
   	 public $description;
    }
    class ActivityResource {
   	 public $snippet;
   	 public $contentDetails;
    }
   	 
    

?>

Sample code to post only a text message in the bulletin:

<?php
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
    set_time_limit(60 * 3);
    session_start();

    $clientId = "692562432874-qidrmk0avkiiii1pqq3rc083jhbvft4g.apps.googleusercontent.com";
    $clientSecret = "a9ZpyLstaY2dXS5mnEZn8SD-";
    $g_YouTubeDataAPIKey = "AIzaSyCC8cvyt2UGDde7ke_r-JNU70FhksO3JpI";


    $_SESSION["code_id"] = $_SERVER["PHP_SELF"];
    
    if ($_SESSION["access_token"] == null || $_SESSION["access_token"] == "") {
   	 // check for oauth response
   	 header("Location: ../../init-login.php");
   	 exit;
    }
   	 
   	 
   	 $accessToken = $_SESSION["access_token"];
    
   	 // create Activity resource for submission

   	 $resourceId = new ResourceId();
   	 $resourceId->kind = "YouTube#channel";
   	 $resourceId->channelId = "UCnXmfpAZ1rLsg0Goh0bBHUA";
   	 
   	 $bulletin = new Bulletin();
   	 $bulletin->resourceId = $resourceId;

   	 $contentDetails = new ContentDetails();
   	 $contentDetails->bulletin = $bulletin;

   	 $snippet = new Snippet();
   	 $snippet->description = "Thank you for visiting my Channel. Please Like and Subscribe";

   	 $resource = new ActivityResource();
   	 $resource->snippet = $snippet;
   	 $resource->contentDetails = $contentDetails;

   	 
   	 $data = $resource;
   	 $data = json_encode($data);
   	 
   	 // make api request
   	 $url = "https://www.googleapis.com/YouTube/v3/activities?part=snippet,contentDetails&key=" .
   			 $g_YouTubeDataAPIKey . "&access_token=" . $accessToken;

   	 $curl = curl_init();
   	 curl_setopt_array($curl, array(
   				 CURLOPT_HTTPHEADER=>array('Content-Type: application/json'),
   				 CURLOPT_RETURNTRANSFER => 1,
   				 CURLOPT_URL => $url,
   				 CURLOPT_USERAGENT => 'YouTube API Tester',
   				 CURLOPT_SSL_VERIFYPEER => 1,
   				 CURLOPT_SSL_VERIFYHOST=> 0,
   				 CURLOPT_CAINFO => "../../cert/cacert.pem",
   				 CURLOPT_CAPATH => "../../cert/cacert.pem",
   				 CURLOPT_FOLLOWLOCATION => TRUE,
   				 CURLOPT_CUSTOMREQUEST=>"POST",
   				 CURLOPT_POSTFIELDS=>$data
   				 
   				 ));
   	 $resp = curl_exec($curl);

   	 curl_close($curl);

   	 var_dump($resp);
   	 
   	 if ($resp) {
   	 } // if resp


////////////////////

    class ResourceId {
   	 public $kind;
   	 public $channelId;
    }

    class Bulletin {
   	 public $resourceId;
    }
    class ContentDetails {
   	 public $bulletin;
    }
    class Snippet {
   	 public $description;
    }
    class ActivityResource {
   	 public $snippet;
   	 public $contentDetails;
    }
   	 


?>

Sample code to post a playlist in the bulletin:

<?php
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
    set_time_limit(60 * 3);
    session_start();

    $clientId = "692562432874-qidrmk0avkiiii1pqq3rc083jhbvft4g.apps.googleusercontent.com";
    $clientSecret = "a9ZpyLstaY2dXS5mnEZn8SD-";
    $g_YouTubeDataAPIKey = "AIzaSyCC8cvyt2UGDde7ke_r-JNU70FhksO3JpI";


    $_SESSION["code_id"] = $_SERVER["PHP_SELF"];
    
    if ($_SESSION["access_token"] == null || $_SESSION["access_token"] == "") {
   	 // check for oauth response
   	 header("Location: ../../init-login.php");
   	 exit;
    }
   	 
   	 
   	 $accessToken = $_SESSION["access_token"];
    
   	 // create Activity resource for submission

   	 $resourceId = new ResourceId();
   	 $resourceId->kind = "YouTube#playlist";
   	 $resourceId->playlistId = "PLLAs2gIR3bXM7lDN46JKGPx1smx2xXvyB";
   	 
   	 $bulletin = new Bulletin();
   	 $bulletin->resourceId = $resourceId;

   	 $contentDetails = new ContentDetails();
   	 $contentDetails->bulletin = $bulletin;

   	 $snippet = new Snippet();
   	 $snippet->description = "Thank you for visiting my Channel. Please Like and Subscribe";

   	 $resource = new ActivityResource();
   	 $resource->snippet = $snippet;
   	 $resource->contentDetails = $contentDetails;

   	 
   	 $data = $resource;
   	 $data = json_encode($data);
   	 
   	 // make api request
   	 $url = "https://www.googleapis.com/YouTube/v3/activities?part=snippet,contentDetails&key=" .
   			 $g_YouTubeDataAPIKey . "&access_token=" . $accessToken;

   	 $curl = curl_init();
   	 curl_setopt_array($curl, array(
   				 CURLOPT_HTTPHEADER=>array('Content-Type: application/json'),
   				 CURLOPT_RETURNTRANSFER => 1,
   				 CURLOPT_URL => $url,
   				 CURLOPT_USERAGENT => 'YouTube API Tester',
   				 CURLOPT_SSL_VERIFYPEER => 1,
   				 CURLOPT_SSL_VERIFYHOST=> 0,
   				 CURLOPT_CAINFO => "../../cert/cacert.pem",
   				 CURLOPT_CAPATH => "../../cert/cacert.pem",
   				 CURLOPT_FOLLOWLOCATION => TRUE,
   				 CURLOPT_CUSTOMREQUEST=>"POST",
   				 CURLOPT_POSTFIELDS=>$data
   				 
   				 ));
   	 $resp = curl_exec($curl);

   	 curl_close($curl);

   	 var_dump($resp);
   	 
   	 if ($resp) {
   	 } // if resp


////////////////////

    class ResourceId {
   	 public $kind;
   	 public $playlistId;
    }

    class Bulletin {
   	 public $resourceId;
    }
    class ContentDetails {
   	 public $bulletin;
    }
    class Snippet {
   	 public $description;
    }
    class ActivityResource {
   	 public $snippet;
   	 public $contentDetails;
    }
   	 
    

?>

Here are the updated bulletins in my channel:

1

 

 

 

 

 

 

 

 

 

 

 

 

Explanation

 If you look at the bottom of the 3 example source codes, you will see classes which have been defined for the Activity object. (Refer to the Activity Resource structure earlier in this book). We do not need to define the whole Activity resource structure – just the relevant snippet and contentDetails part.

The top level class is ActivityResource which contains contentDetails and snippet. contentDetails contains bulletin which contains resourceId.

When we create the objects in code, we start with the lowest classes first and then add them to the top level classes.

Note that in the definition of the ResourceId class, we have used different properties in all the three examples, depending on what kind of bulletin we are posting.

The actual code which sets up the data to be posted via cURL is very important , because if you miss any of the steps, your call will fail. Unlike a normal form POST , this POST has to be done differently.  The two errors which the API returns and everyone encounters initially are

  • The API says that it cannot accept form encoded input
  • The API says that there is an error in the multipart encoding

Both these errors are to do with the actual mechanics of the cURL call and what the Data API expects in the insert call.

Here is how it works:

  • Create an Activity resource object using PHP classes
  • json_encode the Activity resource object
  • Set CURLOPT_HTTPHEADER to “Content-Type: application/json”
  • Set CURLOPT_CUSTOMREQUEST to “POST”
  • Set CURLOPT_POSTFIELDS to the json_encoded Activity resource

If the call works, then an Activity resource object is returned, which the sample code dumps in the display.

This concludes on how to work with the Activities.

Be the first to comment

Leave a Reply

Your email address will not be published.


*