PHP Zend YoutTube API上载 - 此操作错误需要开发人员密钥

时间:2013-01-15 23:22:47

标签: php api zend-framework upload youtube

我正在尝试在YouTube上进行基于浏览器的上传。我正在为ZF2使用PHP,Zend Framework 2和ZendGData Client Library。我已根据Google开发者指南进行了设置,并成功实现了未经授权的请求 - 即搜索视频。我也能够提出授权请求 - 即从我的YouTube帐户中检索我的全名。

当我尝试上传时 - 检索上传令牌我收到错误:

预期回复代码200,得到403

此操作需要开发人员密钥

错误403

我的上传器控制器如下所示。我已按照教程中的说明使用https://code.google.com/apis/youtube/dashboard设置了开发人员密钥,并将其包含在我的代码中。还提供相应的用户/电子邮件和密码。我使用Curl连接到开发指南中未涉及的API,但我不认为这是问题。

`

namespace Uploader\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use ZendGData\YouTube;
use ZendGData\ClientLogin;
use Zend\Http\Client\Adapter;


class UploaderController extends AbstractActionController
{
    public function indexAction()
    {   
        $adapter = new Adapter\Curl();
        $curl = new \ZendGData\HttpClient();
        $curl->setAdapter($adapter);

        $adapter->setOptions(array(
            'curloptions' => array(
                CURLOPT_SSL_VERIFYPEER => FALSE,
                CURLOPT_SSL_VERIFYHOST => FALSE,
            )
        ));

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

        $developerKey = 'AI39si55e**********************************************************************nY9p5NJ8y-8PwS9d8Jw';
        $applicationId = 'Testing';
        $clientId = "Youtube Tester V1";

        $yt = new YouTube($httpClient, $applicationId, $clientId, $developerKey);
        $yt->setMajorProtocolVersion(2);

        $myVideoEntry = new YouTube\VideoEntry;

        $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');

        $tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
        $tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);

        $data = array('tokenValue' => $tokenArray['token'], 'postUrl' => $tokenArray['url']);

        return new ViewModel( $data );
    }

}

`

据我所知,我几乎完全遵循开发人员指南并拥有开发人员密钥但仍然出错。任何想法可能是什么问题?

2 个答案:

答案 0 :(得分:2)

$tokenArray = $yt->getFormUploadToken($myVideoEntry, $clientId, $developerKey);

// vendor/zendframework/zendgdata/library/ZendGData/YouTube.php
public function getFormUploadToken(
        $videoEntry, $clientId, $developerKey,
        $url='https://gdata.youtube.com/action/GetUploadToken')
    {
        //$extraHeaders = $this->getHttpClient()->getRequest()->getHeaders()->toArray();
        $extraHeaders = array(
            'X-GData-Client' => $clientId,
            'X-GData-Key' => 'key='. $developerKey
        );

        if ($url != null && is_string($url)) {
            // $response is a Zend\Http\Response object
            $response = $this->post($videoEntry, $url, null, null, $extraHeaders);
            return self::parseFormUploadTokenResponse($response->getBody());
        } else {
            throw new App\Exception(
                'Url must be provided as a string URL');
        }
    }

答案 1 :(得分:1)

所以在我自己挣扎了一段时间之后,我终于找到了一个工作得很好的hacky解决方法。看起来好像在实例化YouTube对象时提供的dev密钥没有插入到标题中。要解决此问题,请在执行操作时提供标头。这适用于任何条目,例如播放列表或视频。

$dev_key = '****';
$entry = new ZendGData\YouTube\VideoEntry();
// do entry stuff here
$uploadUrl = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads'
$className = 'ZendGData\YouTube\VideoEntry';
$extraHeaders = array('X-GData-Key' => "key=$dev_key");
try {
    $yt->insertEntry($entry, $uploadUrl, $className, $extraHeaders);
} catch (Exception $e) {
    die($e->getMessage());
}