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

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

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

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

YouTube

YouTubeとはユーザーがビデオをアップロード・共有・閲覧できるビデオ共有ウェブサイトです。

YouTube API

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

Q&A

0回答

2033閲覧

YouTubeAPIとPHPを利用して動画を一度アップロードした際に、二度目以降からエラーが表示される

mmm022

総合スコア5

PHP

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

YouTube

YouTubeとはユーザーがビデオをアップロード・共有・閲覧できるビデオ共有ウェブサイトです。

YouTube API

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

0グッド

0クリップ

投稿2021/06/07 05:55

編集2021/06/07 06:09

前提・実現したいこと

PHPとYouTubeAPI_v3を利用した動画投稿フォームを作成しようとしています。
機能自体はテストを行い、問題なく目的のYOUTUBEアカウントに投稿を行えましたが、その後同じ手順で再度動画投稿を試みた際には、以下のエラー文が表示されました。

エラー文の内容によると、おそらく割り当て上限に達したのだと思うのですが、一度きりのアップロードをテストで行っただけですので、本当に上限に達したとは考えづらいかと思います。。。
何か他に考えられる要因がないかどうかお力添えいただきたく思います。

発生している問題・エラーメッセージ

Failed to start the resumable upload (HTTP 403: youtube.quota, The request cannot be completed because you have exceeded your <a href="/youtube/v3/getting-started#quota">quota</a>.)

試したこと

キャッシュの削除やサーバ内の/VIDEOSファイルの中身の消去、DB内のレコードの消去も一通り行いましたが、以前エラーが治りません。

補足情報(FW/ツールのバージョンなど)

php5.6
YouTubeAPI_v3
利用ソース参考:https://www.codexworld.com/upload-video-to-youtube-using-php/

php

1```upload.php 2<?php 3// Include api config file 4require_once 'config.php'; 5 6// Include database class 7require_once 'DB.class.php'; 8 9// Create an object of database class 10$db = new DB; 11 12// If the form is submitted 13if(isset($_POST['videoSubmit'])){ 14 // Video info 15 $title = $_POST['title']; 16 $desc = $_POST['description']; 17 $tags = $_POST['tags']; 18 $privacy = !empty($_POST['privacy'])?$_POST['privacy']:'public'; 19 20 // Check whether file field is not empty 21 if($_FILES["file"]["name"] != ''){ 22 // File upload path 23 $fileName = str_shuffle('codexworld').'-'.basename($_FILES["file"]["name"]); 24 $filePath = "videos/".$fileName; 25 26 // Check the file type 27 $allowedTypeArr = array("video/mp4", "video/avi", "video/mpeg", "video/mpg", "video/mov", "video/wmv", "video/rm"); 28 if(in_array($_FILES['file']['type'], $allowedTypeArr)){ 29 // Upload file to local server 30 if(move_uploaded_file($_FILES['file']['tmp_name'], $filePath)){ 31 // Insert video data in the database 32 $vdata = array( 33 'title' => $title, 34 'description' => $desc, 35 'tags' => $tags, 36 'privacy' => $privacy, 37 'file_name' => $fileName 38 ); 39 $insert = $db->insert($vdata); 40 41 // Store db row id in the session 42 $_SESSION['uploadedFileId'] = $insert; 43 }else{ 44 header("Location:","https://powerb.co.jp/test/maika/fileup_v2/index.php?err=ue"); 45 exit; 46 } 47 }else{ 48 header("Location:","https://powerb.co.jp/test/maika/fileup_v2/index.php?err=fe"); 49 exit; 50 } 51 }else{ 52 header('Location:','https://powerb.co.jp/test/maika/fileup_v2/index.php?err=bf'); 53 exit; 54 } 55} 56 57// Get uploaded video data from database 58$videoData = $db->getRow($_SESSION['uploadedFileId']); 59 60// Check if an auth token exists for the required scopes 61$tokenSessionKey = 'token-' . $client->prepareScopes(); 62if (isset($_GET['code'])) { 63 if (strval($_SESSION['state']) !== strval($_GET['state'])) { 64 die('The session state did not match.'); 65 } 66 67 $client->authenticate($_GET['code']); 68 $_SESSION[$tokenSessionKey] = $client->getAccessToken(); 69 header('Location: ' . REDIRECT_URL); 70} 71 72if (isset($_SESSION[$tokenSessionKey])) { 73 $client->setAccessToken($_SESSION[$tokenSessionKey]); 74} 75 76// Check to ensure that the access token was successfully acquired. 77if ($client->getAccessToken()) { 78 $htmlBody = ''; 79 try{ 80 // REPLACE this value with the path to the file you are uploading. 81 $videoPath = 'videos/'.$videoData['file_name']; 82 83 if(!empty($videoData['youtube_video_id'])){ 84 // Uploaded video data 85 $videoTitle = $videoData['title']; 86 $videoDesc = $videoData['description']; 87 $videoTags = $videoData['tags']; 88 $videoId = $videoData['youtube_video_id']; 89 }else{ 90 // Create a snippet with title, description, tags and category ID 91 // Create an asset resource and set its snippet metadata and type. 92 // This example sets the video's title, description, keyword tags, and 93 // video category. 94 $snippet = new Google_Service_YouTube_VideoSnippet(); 95 $snippet->setTitle($videoData['title']); 96 $snippet->setDescription($videoData['description']); 97 $snippet->setTags(explode(",", $videoData['tags'])); 98 99 // Numeric video category. See 100 // https://developers.google.com/youtube/v3/docs/videoCategories/list 101 $snippet->setCategoryId("22"); 102 103 // Set the video's status to "public". Valid statuses are "public", 104 // "private" and "unlisted". 105 $status = new Google_Service_YouTube_VideoStatus(); 106 $status->privacyStatus = $videoData['privacy']; 107 108 // Associate the snippet and status objects with a new video resource. 109 $video = new Google_Service_YouTube_Video(); 110 $video->setSnippet($snippet); 111 $video->setStatus($status); 112 113 // Specify the size of each chunk of data, in bytes. Set a higher value for 114 // reliable connection as fewer chunks lead to faster uploads. Set a lower 115 // value for better recovery on less reliable connections. 116 $chunkSizeBytes = 1 * 1024 * 1024; 117 118 // Setting the defer flag to true tells the client to return a request which can be called 119 // with ->execute(); instead of making the API call immediately. 120 $client->setDefer(true); 121 122 // Create a request for the API's videos.insert method to create and upload the video. 123 $insertRequest = $youtube->videos->insert("status,snippet", $video); 124 125 // Create a MediaFileUpload object for resumable uploads. 126 $media = new Google_Http_MediaFileUpload( 127 $client, 128 $insertRequest, 129 'video/*', 130 null, 131 true, 132 $chunkSizeBytes 133 ); 134 $media->setFileSize(filesize($videoPath)); 135 136 137 // Read the media file and upload it chunk by chunk. 138 $status = false; 139 $handle = fopen($videoPath, "rb"); 140 while (!$status && !feof($handle)) { 141 $chunk = fread($handle, $chunkSizeBytes); 142 $status = $media->nextChunk($chunk); 143 } 144 fclose($handle); 145 146 // If you want to make other calls after the file upload, set setDefer back to false 147 $client->setDefer(false); 148 149 // Update youtube video id to database 150 $db->update($videoData['id'], $status['id']); 151 152 // Delete video file from local server 153 @unlink("videos/".$videoData['file_name']); 154 155 // uploaded video data 156 $videoTitle = $status['snippet']['title']; 157 $videoDesc = $status['snippet']['description']; 158 $videoTags = implode(",",$status['snippet']['tags']); 159 $videoId = $status['id']; 160 } 161 162 // uploaded video embed html 163 $youtubeURL = 'https://youtu.be/'.$videoId; 164 $htmlBody .= "<p class='succ-msg'>Video Uploaded to YouTube</p>"; 165 $htmlBody .= '<embed width="400" height="315" src="https://www.youtube.com/embed/'.$videoId.'"></embed>'; 166 $htmlBody .= '<ul><li><b>YouTube URL: </b><a href="'.$youtubeURL.'">'.$youtubeURL.'</a></li>'; 167 $htmlBody .= '<li><b>Title: </b>'.$videoTitle.'</li>'; 168 $htmlBody .= '<li><b>Description: </b>'.$videoDesc.'</li>'; 169 $htmlBody .= '<li><b>Tags: </b>'.$videoTags.'</li></ul>'; 170 $htmlBody .= '<a href="logout.php">Logout</a>'; 171 172 } catch (Google_Service_Exception $e) { 173 $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', 174 htmlspecialchars($e->getMessage())); 175 } catch (Google_Exception $e) { 176 $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', 177 htmlspecialchars($e->getMessage())); 178 $htmlBody .= 'Please reset session <a href="logout.php">Logout</a>'; 179 } 180 181 $_SESSION[$tokenSessionKey] = $client->getAccessToken(); 182} elseif (OAUTH_CLIENT_ID == '') { 183 $htmlBody = <<<END 184 <h3>Client Credentials Required</h3> 185 <p> 186 You need to set <code>$oauthClientID</code> and 187 <code>$oauthClientSecret</code> before proceeding. 188 <p> 189END; 190} else { 191 // If the user hasn't authorized the app, initiate the OAuth flow 192 $state = mt_rand(); 193 $client->setState($state); 194 $_SESSION['state'] = $state; 195 196 $authUrl = $client->createAuthUrl(); 197 $htmlBody = <<<END 198 <h3>Authorization Required</h3> 199 <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> 200END; 201} 202?> 203 204<!DOCTYPE html> 205<html> 206<head> 207<title>Upload Video to YouTube using PHP by CodexWorld</title> 208</head> 209<body> 210 <div class="uplink"><a href="<?php echo BASE_URL; ?>">New Upload</a></div> 211 <div class="content"> 212 <!-- Display uploaded video info --> 213 <?php echo $htmlBody; ?> 214 </div> 215</body> 216</html>``` 217コード

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

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

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

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

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

m.ts10806

2021/06/07 06:02

サービス提供者に問い合わせるべき案件と思います。 ご自身のコードも提示されないままですし、おそらく他者には難しいのでは。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問