Developer key required for this operation

2019-08-04 14:05发布

问题:

I dont find the solution for my "Developer key required for this operation" problem.

here is my code:

require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();

Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 


$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = 
  Zend_Gdata_ClientLogin::getHttpClient(
              $username = '************@gmail.com',
              $password = '************',
              $service = 'youtube',
              $client = null,
              $source = '************', // a short string identifying your application
              $loginToken = null,
              $loginCaptcha = null,
              $authenticationURL);

// Note that this example creates an unversioned service object.
// You do not need to specify a version number to upload content
// since the upload behavior is the same for all API versions.
$yt = new Zend_Gdata_YouTube($httpClient);

// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

// create a new Zend_Gdata_App_MediaFileSource object
$filesource = $yt->newMediaFileSource('asd.avi');
$filesource->setContentType('video/x-ms-wmv');
// set slug header
$filesource->setSlug('asd.avi');

// add the filesource to the video entry
$myVideoEntry->setMediaSource($filesource);

$myVideoEntry->setVideoTitle('My Test Movie');
$myVideoEntry->setVideoDescription('My Test Movie');
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory('Autos');

// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags('cars, funny');

// set some developer tags -- this is optional
// (see Searching by Developer Tags for more details)
$myVideoEntry->setVideoDeveloperTags(array('mydevtag', 'anotherdevtag'));

// set the video's location -- this is also optional
$yt->registerPackage('Zend_Gdata_Geo');
$yt->registerPackage('Zend_Gdata_Geo_Extension');
$where = $yt->newGeoRssWhere();
$position = $yt->newGmlPos('37.0 -122.0');
$where->point = $yt->newGmlPoint($position);
$myVideoEntry->setWhere($where);

// upload URI for the currently authenticated user
$uploadUrl = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';

// try to upload the video, catching a Zend_Gdata_App_HttpException, 
// if available, or just a regular Zend_Gdata_App_Exception otherwise
try {
  $newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry');
} catch (Zend_Gdata_App_HttpException $httpException) {
  echo $httpException->getRawResponseBody();
} catch (Zend_Gdata_App_Exception $e) {
    echo $e->getMessage();
}             

i try to use my devkey with the following lines, but where i need to put this?

$developerKey = 'ABC123 ... ';
$applicationId = 'Video uploader v1';
$clientId = 'My video upload client - v1';

$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);

i try to find a fully working PHP code to upload video to youtube, but dont find anything........

回答1:

Instead of

$yt = new Zend_Gdata_YouTube($httpClient);

you need to do the following:

$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);

Note that $applicationId and $clientId must each be valid ids. Unless already available, you can create your application and client ids here: https://code.google.com/apis/console/

You can obtain your developer key by registering here: https://code.google.com/apis/youtube/dashboard/gwt/index.html#settings



回答2:

How to get Developer Key

The developer key can be obtained by creating a product in Google YouTube API.

Login to your Google Account:

It will automatically redirect you to the create developer profile if it's your first time:

After creating your developer profile, go ahead and create a product, it could be everything you want.

After creating your product you will get the developer key in the censored textbox ("Developer key here")

Copy that to your code, and if you are using version 3 of the API you can get a client key down there.

How to get Client ID

In order to get a client id you need to create an API project in Google API's console.

Google API's Console

After logging in you will see this page! Press create project and create your project:

It will automatically create project for you, and now on you need to select the services you want to use. If you are going to use YouTube, select YouTube Data API.

Now you will be able to go the API Access tab. Create an OAuth ID there.

Enter your product info:

Choose its type:

Get your client id! :D