如何使用API​​在YouTube上切换带有广告选项的Monetise

时间:2017-06-08 05:56:11

标签: php youtube

我正在构建一个使用PHP Google客户端库将视频上传到YouTube的应用程序。我能够上传视频,但无法在Monetise复选框上切换。网上的信息非常少。

我发现此链接很有帮助。 http://www.techtonet.com/youtube-upload-videos-with-partner-api-in-php/#ipt_kb_toc_51_6

但我无法做到这一点。

请帮忙解决这个问题。

// 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));

2 个答案:

答案 0 :(得分:1)

我正在使用此代码

$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,
    ]);

如果你没有工作,那么在上传视频后,将所有代码放在这里,一旦你从Youtube获得videoId。 您只需要更改$ videoId和$ contentOwnerId

答案 1 :(得分:0)

上面显示的代码是仅对应于获利的部分。您需要将视频上传到youtube。如果您拥有视频和contentOwnerId的ID,则代码应该可以在不更改任何其他内容的情况下运行。只尝试这部分代码,如果它不适合你,请将toutube的答案提交给以下请求

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

如果成功,此方法将在响应正文中返回声明资源。否则返回错误。如果它返回错误,请告诉我错误,所以我可以告诉你发生了什么。