floCoder
floCoder

Reputation: 431

How to toggle on Monetise with ads option on YouTube with API

I am building an application that uploads videos to YouTube using PHP Google Client library. I was able to upload videos but couldn't toggle on the Monetise checkbox. There is very little information on the web.

I found this link to be helpful. http://www.techtonet.com/youtube-upload-videos-with-partner-api-in-php/#ipt_kb_toc_51_6

But I couldn't managed to do this.

Please help with this.

// Create a claim resource. Identify the video being claimed, the asset
 // that represents the claimed content, the type of content being claimed,
 // and the policy that you want to apply to the claimed video.
 $claim = new Google_Service_YouTubePartner_Claim();
 $claim->setAssetId($assetId);
 $claim->setVideoId($videoId);
 $claim->setPolicy($policy);
 $claim->setContentType("audiovisual");

 // Insert the created claim.
 $claimInsertResponse = $this->_youtubePartner->claims->insert($claim,
 array('onBehalfOfContentOwner' => $contentOwnerId));

 # Enable ads for the video. This example enables the TrueView ad format.
 $option = new Google_Service_YouTubePartner_VideoAdvertisingOption();
 //$option->setAdFormats(array("overlay", "trueview_instream", "standard_instream"));
 $option->setAdFormats(array("trueview_instream", "standard_instream"));
 $setAdvertisingResponse = $this->_youtubePartner->videoAdvertisingOptions->update(
 $videoId, $option, array('onBehalfOfContentOwner' => $contentOwnerId));

Upvotes: 0

Views: 412

Answers (2)

Julian
Julian

Reputation: 46

The code that shows you above is the part that corresponds only to the monetization. You need to have the video already uploaded to youtube. If you have the id of the video and contentOwnerId the code should work without changing anything else. Try only this part of the code and if it does not work for you, get me the answer of toutube to the following request

$claimInsertResponse = $youtubePartner->claims->insert($claim,
                array('onBehalfOfContentOwner' => $contentOwnerId));

If successful, this method returns a claim resource in the response body. Otherwise it returns an error. If it returns an error, give me the error, so I can tell you what is happening.

Upvotes: 0

Julian
Julian

Reputation: 46

For me is working with this code

$youtubePartner = new \Google_Service_YouTubePartner($this->client);

$asset = new \Google_Service_YouTubePartner_Asset();
    $metadata = new \Google_Service_YouTubePartner_Metadata();
    $youtubePartner = new \Google_Service_YouTubePartner($this->client);
    $metadata->setTitle("Asset Title"));    
    $metadata->setDescription("AssetDescription");
    $asset->setMetadata($metadata);
    $asset->setType("web");

    $assetInsertResponse = $youtubePartner->assets->insert($asset, [
        'onBehalfOfContentOwner' => $this->contentOwnerId
    ]);

    $assetId = $assetInsertResponse['id'];

    $ratio = 100;
    $type = "exclude";
    $territories = [];

$owners = new \Google_Service_YouTubePartner_TerritoryOwners();
    $owners->setOwner($this->contentOwnerId);
    $owners->setRatio($ratio);
    $owners->setType($type);
    $owners->setTerritories($territories);
    $ownership = new \Google_Service_YouTubePartner_RightsOwnership();
    $ownership->setGeneral([$owners]);
    $youtubePartner->ownership->update($assetId, $ownership, ['onBehalfOfContentOwner' => $contentOwnerId]);

$policy = new \Google_Service_YouTubePartner_Policy();
    $policyRule = new \Google_Service_YouTubePartner_PolicyRule();
    $policyRule->setAction("monetize");
    $policy->setRules(array($policyRule));

$claim = new \Google_Service_YouTubePartner_Claim();
    $claim->setAssetId($assetId);
    $claim->setVideoId($videoId);
    $claim->setPolicy($policy);
    $claim->setContentType("audiovisual");


    $claimInsertResponse = $youtubePartner->claims->insert($claim, [
        'onBehalfOfContentOwner' => $contentOwnerId,
    ]);

If is not working for you put all code here, after uploading video, once you have videoId from Youtube. You only need to change $videoId and $contentOwnerId

Upvotes: 1

Related Questions