PHP Zend Youtube API 上传 - 此操作错误需要开发人员 key

标签 php api zend-framework upload youtube

我正在尝试基于浏览器上传到 YouTube。我正在为 ZF2 使用 PHP、Zend Framework 2 和 ZendGData 客户端库。我已按照 Google Developers 指南进行设置,并成功执行未经授权的请求 - 即搜索视频。我还能够执行授权请求 - 即从我的 YouTube 帐户中检索我的全名。

当我尝试上传时 - 检索上传 token 时出现错误:

预期响应代码 200,得到 403

此操作需要开发人员 key

错误 403

我的上传器 Controller 如下所示。我使用 https://code.google.com/apis/youtube/dashboard 设置了开发者 key 如教程中所述,并将其包含在我的代码中。还提供了相应的用户/电子邮件和密码。我正在使用 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 );
    }

}

`

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

最佳答案

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

关于PHP Zend Youtube API 上传 - 此操作错误需要开发人员 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14348780/

相关文章:

zend-framework - 如何在 SOA 中使用依赖注入(inject)?

php - 文件上传拒绝 GIF 动画(使用 PHP/Zend Framework)

php - 如何使用JQuery获取输入类型文件上传的文件内容

php 未运行 sqlldr

php - 销毁大部分 session

php - 对于以下场景,如何使用 SQL 查询显示按月结果?

javascript - 将JSON中的字符串js函数解析为js函数

ruby-on-rails - Rails JSON 响应问题

php - 当我尝试登录时出现错误 "Couldn' t reach API"

javascript - 有谁知道您是否可以在没有当前 URL 的情况下在 Google 获得 javascript 的 API key ?