Foros del Web » Programando para Internet » PHP »

listar videos api youtube v3

Estas en el tema de listar videos api youtube v3 en el foro de PHP en Foros del Web. Buenas... necesito para mi pagina web listar los videos de una lista que tengo en Youtube... Ya descargue las librerías de Google que incluye el ...
  #1 (permalink)  
Antiguo 21/06/2015, 22:40
 
Fecha de Ingreso: mayo-2006
Ubicación: Bogotá
Mensajes: 2.061
Antigüedad: 17 años, 11 meses
Puntos: 50
listar videos api youtube v3

Buenas... necesito para mi pagina web listar los videos de una lista que tengo en Youtube...

Ya descargue las librerías de Google que incluye el script
Ya entre a google console y active la api y genere los datos para autenticarme con oauth
Configuro los datos en el script pero me sale esto y no logro hacerlo funcionar.. alguien que lo haya usado o lo pueda probar que me ayude a identificar que esta pasando, gracias

Authorization Required

You need to authorize access before proceeding.



"Este script esta en la API de Google pero no me funciona"
Código PHP:
<?php
// Call set_include_path() as needed to point to your client library.
require_once 'Google/Client.php';
require_once 
'Google/Service/YouTube.php';
session_start();

/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * Google Developers Console <https://console.developers.google.com/>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
$OAUTH2_CLIENT_ID 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET 'REPLACE_ME';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect filter_var('http://' $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
  
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

if (isset(
$_GET['code'])) {
  if (
strval($_SESSION['state']) !== strval($_GET['state'])) {
    die(
'The session state did not match.');
  }

  
$client->authenticate($_GET['code']);
  
$_SESSION['token'] = $client->getAccessToken();
  
header('Location: ' $redirect);
}

if (isset(
$_SESSION['token'])) {
  
$client->setAccessToken($_SESSION['token']);
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
  try {
    
// Call the channels.list method to retrieve information about the
    // currently authenticated user's channel.
    
$channelsResponse $youtube->channels->listChannels('contentDetails', array(
      
'mine' => 'true',
    ));

    
$htmlBody '';
    foreach (
$channelsResponse['items'] as $channel) {
      
// Extract the unique playlist ID that identifies the list of videos
      // uploaded to the channel, and then call the playlistItems.list method
      // to retrieve that list.
      
$uploadsListId $channel['contentDetails']['relatedPlaylists']['uploads'];

      
$playlistItemsResponse $youtube->playlistItems->listPlaylistItems('snippet', array(
        
'playlistId' => $uploadsListId,
        
'maxResults' => 50
      
));

      
$htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>";
      foreach (
$playlistItemsResponse['items'] as $playlistItem) {
        
$htmlBody .= sprintf('<li>%s (%s)</li>'$playlistItem['snippet']['title'],
          
$playlistItem['snippet']['resourceId']['videoId']);
      }
      
$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()));
  }

  
$_SESSION['token'] = $client->getAccessToken();
} else {
  
$state mt_rand();
  
$client->setState($state);
  
$_SESSION['state'] = $state;

  
$authUrl $client->createAuthUrl();
  
$htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
  <head>
    <title>My Uploads</title>
  </head>
  <body>
    <?=$htmlBody?>
  </body>
</html>
  #2 (permalink)  
Antiguo 22/06/2015, 07:48
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 10 meses
Puntos: 127
Respuesta: listar videos api youtube v3

lineas

$OAUTH2_CLIENT_ID = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
  #3 (permalink)  
Antiguo 22/06/2015, 07:50
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 10 meses
Puntos: 127
Respuesta: listar videos api youtube v3

TRADUCCION
Código PHP:
<?php
// Call set_include_path() as needed to point to your client library.
require_once 'Google/Client.php';
require_once 
'Google/Service/YouTube.php';
session_start();

/*
  * Puede adquirir una ID de cliente de OAuth 2.0 y secreto cliente desde el
  * Google Consola Desarrolladores <https://console.developers.google.com/>
  * Para obtener más información acerca del uso de OAuth 2.0 para acceder a las API de Google, por favor, consulte:
  * <Https://developers.google.com/youtube/v3/guides/authentication>
  * Por favor, asegúrese de que ha permitido a la API de datos de YouTube para su proyecto.
  */
$OAUTH2_CLIENT_ID 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET 'REPLACE_ME';
  #4 (permalink)  
Antiguo 22/06/2015, 10:50
 
Fecha de Ingreso: mayo-2006
Ubicación: Bogotá
Mensajes: 2.061
Antigüedad: 17 años, 11 meses
Puntos: 50
Respuesta: listar videos api youtube v3

jaja enlinea777 gracias pero yo se ingles y como coloque en mi post yo ya cree y configure los datos del cliente..... no los coloque acá por obvias razones.



Ya descargue las librerías de Google que incluye el script
Ya entre a google console y active la api y genere los datos para autenticarme con oauth
Configuro los datos en el script pero me sale esto y no logro hacerlo funcionar.. alguien que lo haya usado o lo pueda probar que me ayude a identificar que esta pasando, gracias



El problema esta en que aun configurandolos no me permite el acceso y me sale el error que comente... saben que puede estar pasando, gracais

Etiquetas: api, html, videos, youtube
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:40.