3

我们的项目允许用户在登录我们的会话时将视频上传到我们的公共 youtube 频道。我们不想要求额外的 OAuth2 验证,而是遵循 Google API v2。

我的代码(php)和错误如下,但我的一般问题是:我的服务器可以使用我的 API 密钥/秘密进行插入视频 POST,而无需用户进行身份验证。

这个问题 - Google Calendar API v3 hardcoded credentials - 非常相似。但是,如果 CURLing Google 获取 access_token 是唯一的答案,我会感到失望。无论如何谢谢。

谷歌 API V3 设置:

Client ID:  xxxx.apps.googleusercontent.com
Email address:  xxxx@developer.gserviceaccount.com
Client secret:  xxxx
Redirect URIs:  http://fbtabs.imagendigital.co/unilever/sedal/surprise/galeria
JavaScript origins: https://fbtabs.imagendigital.co
API key:    
AIzaSyBgS-jMJQUPIiAyX20xC-encFWTPsR7qxQ
Referers:   
Any referer allowed
Activated on:   Jul 4, 2013 1:21 PM
Activated by:    xxxx@gmail.com – you

代码:

<?php
require_once BASE_CD . 'app/src/Idframework/Tools.php';
require_once BASE_CD . 'app/vendor/google-api-php-client/src/Google_Client.php';
require_once BASE_CD . 'app/vendor/google-api-php-client/src/contrib/Google_YouTubeService.php';
$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxxx');
$client->setRedirectUri('http://fbtabs.imagendigital.co/unilever/sedal/surprise/galeria');
$client->setDeveloperKey('xxxx');
$youtube = new Google_YoutubeService($client);
$snippet = new Google_VideoSnippet();
$status = new Google_VideoStatus();
$video = new Google_Video();

if (isset($_POST['tip_desc'])) $snippet->setDescription($_POST['tip_desc']);
    $snippet->setTitle($_POST['tip_name']);
    $snippet->setTags(array("sedal","hair","pello","cabello"));
    $status->privacyStatus = "public";

    $video->setSnippet($snippet);
    $video->setStatus($status);

    $filename = $_FILES['videoInp']['tmp_name'];
    $mime = \Idframework\Tools::get_mime($filename);

    try {
        $part = "status";
        $obj = $youtube->videos->insert($part, $video,
                                         array("data"=>file_get_contents($filename), 
                                        "mimeType" => $mime));
    } catch(Google_ServiceException $e) {
        print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(). " <br>";
        print "Stack trace is ".$e->getTraceAsString();
    }

错误:

Notice: Undefined index: content-type in C:\DATA\IDInteractive\SedalSurprise\app\vendor\google-api-php-client\src\service\Google_MediaFileUpload.php on line 99
Caught Google service Exception 401 message is Error calling POST https://www.googleapis.com/upload/youtube/v3/videos?part=player&uploadType=multipart&key=AIzaSyBgS-jMJQUPIiAyX20xC-encFWTPsR7qxQ: (401) Login Required 
Stack trace is #0 C:\DATA\IDInteractive\SedalSurprise\app\vendor\google-api-php-client\src\io\Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 C:\DATA\IDInteractive\SedalSurprise\app\vendor\google-api-php-client\src\service\Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 C:\DATA\IDInteractive\SedalSurprise\app\vendor\google-api-php-client\src\contrib\Google_YouTubeService.php(789): Google_ServiceResource->__call('insert', Array) #3 C:\DATA\IDInteractive\SedalSurprise\youtuber.php(56): Google_VideosServiceResource->insert('player', Object(Google_Video), Array) #4 {main}
4

1 回答 1

3

首先,不建议让任意用户将视频上传到“主”YouTube 频道,原因在这篇博文中列出。

话虽如此,如果您决心这样做,那么您需要在每个上传请求中包含与您的频道关联的访问令牌。获取新访问令牌(它们在一小时后过期)的适当方法是获取您之前生成并存储在某处的刷新令牌,并向适当的 URL 发出 HTTP 请求以取回访问令牌,如中所述OAuth 2文档。没有办法解决这个问题——我不确定 Google APIs PHP 客户端库中是否有一个本地方法可以为您自动执行该过程,但这就是需要在幕后发生的事情。

Google Calendar API v3 硬编码凭据中的信息似乎确实相关。

于 2013-07-09T16:24:40.397 回答