質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

YouTube API

YouTube APIはYouTubeのビデオコンテンツと機能性をウェブサイト、アプリケーション、デバイスに統合することを可能にします。

Q&A

解決済

1回答

5924閲覧

Youtube apiv3 に詳しい方

seeeekik

総合スコア11

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

YouTube API

YouTube APIはYouTubeのビデオコンテンツと機能性をウェブサイト、アプリケーション、デバイスに統合することを可能にします。

0グッド

1クリップ

投稿2015/01/30 04:48

UserがWeb上から動画をアップロードさせる仕組みを作りたいです。

https://developers.google.com/youtube/v3/code_samples/php?hl=ja#resumable_uploads

公式のサンプルコードだとOAuth認証しないと、アップロードできません。Userがgoogleアカウントを持っていなくてもアップロードできるように認証を挟まずにアップロードさせたいのです。

可能なのでしょうか?

GoogleDevelopperConsole->OAuth->サービスアカウント からサービスアカウント取って、試してみましたが、
An client error occurred: Failed to start the resumable upload (HTTP 401: youtube.header, Unauthorized)

と返ってきます。考えられる原因は何でしょうか?
(一応、コード載せますm(_ _)m)Cakephpでの記述です。

public function resumable_upload(){
App::import('Vendor', 'google-api-php-client-master/src/Google/Client');
App::import('Vendor', 'google-api-php-client-master/src/Google/Service/YouTube');
$htmlBody ='';

// サービスアカウント名(メールアドレス) $service_account_name ='service_accont_name'; // P12キーファイルのパス $key_file_location = 'path/to/key'; session_start(); if ( !strlen($service_account_name) || !strlen($key_file_location)) { echo missingServiceAccountDetailsWarning(); } $client = new Google_Client(); $client->setScopes('https://www.googleapis.com/auth/youtube'); //tokenをセット if (isset($_SESSION['service_token'])) { $client->setAccessToken($_SESSION['service_token']); } //Credentials(証明書)用の設定 $key = file_get_contents($key_file_location); $cred = new Google_Auth_AssertionCredentials( $service_account_name, array('https://www.googleapis.com/auth/youtube.upload'),//$scope $key ); $client->setAssertionCredentials($cred); //トークンのリフレッシュ if($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $_SESSION['service_token'] = $client->getAccessToken(); //apiを操作用?インスタンスを生成 $youtube = new Google_Service_YouTube($client); if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); }

);

// Check to ensure that the access token was successfully acquired. if ($client->getAccessToken()) { echo 'getAccessToken true <br>'; try{ // REPLACE this value with the path to the file you are uploading. $videoPath = 'videopath'; // Create a snippet with title, description, tags and category ID // Create an asset resource and set its snippet metadata and type. // This example sets the video's title, description, keyword tags, and // video category. $snippet = new Google_Service_YouTube_VideoSnippet(); $snippet->setTitle("Test title"); $snippet->setDescription("Test description"); $snippet->setTags(array("tag1", "tag2")); // Numeric video category. See // https://developers.google.com/youtube/v3/docs/videoCategories/list $snippet->setCategoryId("22"); // Set the video's status to "public". Valid statuses are "public", // "private" and "unlisted". $status = new Google_Service_YouTube_VideoStatus(); $status->privacyStatus = "public"; // Associate the snippet and status objects with a new video resource. $video = new Google_Service_YouTube_Video(); $video->setSnippet($snippet); $video->setStatus($status); // Specify the size of each chunk of data, in bytes. Set a higher value for // reliable connection as fewer chunks lead to faster uploads. Set a lower // value for better recovery on less reliable connections. $chunkSizeBytes = 1 * 1024 * 1024; // Setting the defer flag to true tells the client to return a request which can be called // with ->execute(); instead of making the API call immediately. $client->setDefer(true); // Create a request for the API's videos.insert method to create and upload the video. $insertRequest = $youtube->videos->insert("status,snippet", $video); // Create a MediaFileUpload object for resumable uploads. $media = new Google_Http_MediaFileUpload( $client, $insertRequest, 'video/*', null, true, $chunkSizeBytes ); $media->setFileSize(filesize($videoPath)); // Read the media file and upload it chunk by chunk. $status = false; $handle = fopen($videoPath, "rb"); debug($handle); while (!$status && !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } fclose($handle); // If you want to make other calls after the file upload, set setDefer back to false $client->setDefer(false); //text 成形 $htmlBody .= "<h3>Video Uploaded</h3><ul>"; $htmlBody .= sprintf('<li>%s (%s)</li>', $status['snippet']['title'], $status['id']); } catch (Google_ServiceException $e) { $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); } catch (Google_Exception $e) { $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); echo $htmlBody; ・・・☆ここで401エラーが吐かれます↓

An client error occurred: Failed to start the resumable upload (HTTP 401: youtube.header, Unauthorized)
}

$_SESSION['token'] = $client->getAccessToken(); } else { // If the user hasn't authorized the app, initiate the OAuth flow $state = mt_rand(); $client->setState($state); $_SESSION['state'] = $state; $authUrl = $client->createAuthUrl(); } $this->autoRender = false; }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

私もまったく同じものを開発すべく、調査中でした

その結果、公式のリファレンス

YouTube では Service Account はサポートされていないため、Service Account を使って認証しようとすると、このエラーが表示されます

との記載を見つけました

こちらのページの「YouTube API で Service Accounts が機能しない」の項にも同様の事が書いてあるのでサービスアカウントでのAPIアップロードはできないのではないかなと思います。

投稿2015/02/03 06:02

Takacy

総合スコア7

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

seeeekik

2015/02/03 07:10

回答有難うございます。ご指摘の通りのようですね。 DataAPIでは全て認証が必要との記述もありますが、他のAPIを試した所、確か動画リストの取得は可能でしたので、不思議に思っていました。おかげさまでスッキリしました。 https://developers.google.com/youtube/v3/getting-started?hl=ja 「次の表は、各種リソースでサポートされている操作を示したものです。リソースの挿入、更新、または削除を実行する操作の場合は、常にユーザー認証が必要になります。また、list メソッドについては認証されたリクエストと認証されていないリクエストの両方がサポートされています」 「サービスアカウントでのAPIアップロードはできない」 本当に回答ありがとうございました
Takacy

2015/02/03 07:22

余談ですが、代わりにVimeoという動画サービスもAPIを提供しています。 [https://developer.vimeo.com/api/upload](https://developer.vimeo.com/api/upload) こちらはおそらく目的の事はできるとは思うのですが、APIでのアップロードには申請が必要で、さらに認証なしの第三者アップロードは有料プランが必要みたいなのであきらめました。 年間利用料が払えるのであれば検討してみてはいかがでしょう。 私も同様のサービスを作ろうと思っているので、何か良い情報がありましたらお知らせください
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問