php - Login required error when attempting to use the YouTube API -
i trying setup script can create live events via youtube api , have taken example off developer console , attempted modify use api key instead going our own account , not users using own account.
also, fyi using google api php client v1.
i have below code:
require_once 'includes/google/autoload.php'; require_once 'includes/google/client.php'; require_once 'includes/google/service/youtube.php'; $client = new google_client(); $client->setapplicationname("my title"); $client->setdeveloperkey("xxxxxxxxxxxxxx"); $client->setscopes('https://www.googleapis.com/auth/youtube'); // define object used make api requests. $youtube = new google_service_youtube($client); try { // create object livebroadcast resource's snippet. specify values // snippet's title, scheduled start time, , scheduled end time. $broadcastsnippet = new google_service_youtube_livebroadcastsnippet(); $broadcastsnippet->settitle('test broadcast'); $broadcastsnippet->setscheduledstarttime('2017-01-30t00:00:00.000z'); $broadcastsnippet->setscheduledendtime('2017-01-31t00:00:00.000z'); // create object livebroadcast resource's status, , set // broadcast's status "private". $status = new google_service_youtube_livebroadcaststatus(); $status->setprivacystatus('unlisted'); // create api request inserts livebroadcast resource. $broadcastinsert = new google_service_youtube_livebroadcast(); $broadcastinsert->setsnippet($broadcastsnippet); $broadcastinsert->setstatus($status); $broadcastinsert->setkind('youtube#livebroadcast'); // execute request , return object contains information // new broadcast. $broadcastsresponse = $youtube->livebroadcasts->insert('snippet,status', $broadcastinsert, array()); // create object livestream resource's snippet. specify value // snippet's title. $streamsnippet = new google_service_youtube_livestreamsnippet(); $streamsnippet->settitle('test stream'); // create object content distribution network details live // stream , specify stream's format , ingestion type. $cdn = new google_service_youtube_cdnsettings(); $cdn->setformat("1080p"); $cdn->setingestiontype('rtmp'); // create api request inserts livestream resource. $streaminsert = new google_service_youtube_livestream(); $streaminsert->setsnippet($streamsnippet); $streaminsert->setcdn($cdn); $streaminsert->setkind('youtube#livestream'); // execute request , return object contains information // new stream. $streamsresponse = $youtube->livestreams->insert('snippet,cdn', $streaminsert, array()); // bind broadcast live stream. $bindbroadcastresponse = $youtube->livebroadcasts->bind( $broadcastsresponse['id'], 'id,contentdetails', array( 'streamid' => $streamsresponse['id'], )); $htmlbody .= "<h3>added broadcast</h3><ul>"; $htmlbody .= sprintf('<li>%s published @ %s (%s)</li>', $broadcastsresponse['snippet']['title'], $broadcastsresponse['snippet']['publishedat'], $broadcastsresponse['id']); $htmlbody .= '</ul>'; $htmlbody .= "<h3>added stream</h3><ul>"; $htmlbody .= sprintf('<li>%s (%s)</li>', $streamsresponse['snippet']['title'], $streamsresponse['id']); $htmlbody .= '</ul>'; $htmlbody .= "<h3>bound broadcast</h3><ul>"; $htmlbody .= sprintf('<li>broadcast (%s) bound stream (%s).</li>', $bindbroadcastresponse['id'], $bindbroadcastresponse['contentdetails']['boundstreamid']); $htmlbody .= '</ul>'; } catch (google_service_exception $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())); } ?> <!doctype html> <html> <head> <title>bound live broadcast</title> </head> <body> <?= $htmlbody ?> </body> </html>
but when run error:
a service error occurred: error calling post https://www.googleapis.com/youtube/v3/livebroadcasts?part=snippet%2cstatus&key=xxxxxxxxxxxxxxxx: (401) login required
what doing wrong here?
this operation may executed user authenticated via oauth2, can't create broadcast using developer key. in case want use own account, you'll need obtain access token valid account, preferably "offline" one, can refresh automatically once expires, way you'll need manually go through consent screen once. here's more on oauth 2 flow:
https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps
edit: more precise, flow should this:
- in separate script, you go through process of obtaining offline access token - click through consent screen , grant application access api on account's behalf.
- store obtained token later use, example save in flat file or database. need first 2 steps once, later token can refreshed automatically.
- user enters site , script uses obtained token perform api operations. happens server-side, without user's input.
Comments
Post a Comment