7

我想使用 youtube api 通过此 api 调用获取用户新的订阅视频:

http://gdata.youtube.com/feeds/api/users/default/newsubscriptionvideos

如果没有登录,我会收到以下回复:

User authentication required.
Error 401

如何从 php 登录 youtube?

4

3 回答 3

5

您可以使用OAuthAuthSubClientLogin。ClientLogin 是最简单的(它只使用用户名/密码),但不鼓励使用,因为它要求用户将他们的凭据交给您。AuthSub 和 OAuth 没有。Google 的PHP 库目前似乎只支持 AuthSub(PHP 示例)和 ClientLogin。

于 2010-05-30T10:35:07.660 回答
0

这是登录的简单功能。成功时返回错误消息或 0。我使用了自己的 curl 库,但不清楚 $this->curl->SetHeader 可以替换为 curl_setopt($ch, CURLOPT_HTTPHEADER, $header)

public function login(Model_ServiceAccount $account){
        $this->curl->SetHeader('Content-type', 'application/x-www-form-urlencoded');
        $this->curl->post('https://www.google.com/youtube/accounts/ClientLogin',
                          'Email=' . $account->mail->login . '&Passwd=' . $account->mail->password . '&service=youtube&source=whatever');
        if (preg_match('~Error=(.+)~', $this->curl->getResponse(), $match))
            return $match[1];
        if (!preg_match('~Auth=(.*)~', $this->curl->getResponse(), $match)) Toolkit::error('Unhandled error in Authentication request');
        $authToken = $match[1];
        $this->curl->SetHeader('Authorization: GoogleLogin auth', $this->developerKey);
        $this->curl->SetHeader('X-GData-Key: key=', $authToken);
        $this->curl->SetHeader('Content-Type', 'application/atom+xml');
        return 0;
    }
于 2010-08-23T02:06:16.420 回答
0

这个对我有用。使用谷歌帐户登录并获取登录用户频道信息。

下载代码

<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_YoutubeService.php';
session_start();

$OAUTH2_CLIENT_ID = 'XXXXXXXXXXXXXXXX';
$OAUTH2_CLIENT_SECRET = 'XXXXXXXXXXXXXXXX';

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

$youtube = new Google_YoutubeService($client);

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

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

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

$json_output = json_decode($client->getAccessToken());
$token = $json_output->access_token;   

if ($client->getAccessToken()) 
{        
    $userid = getYoutubeData($token);
    $htmlBody = "SubscriberCount: ".youtubefollowers($userid);
    $_SESSION['token'] = $client->getAccessToken();   
}
else 
{
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Connect to youtube</h3>
  <p>You need to <a href="$authUrl">Connect</a> before proceeding.<p>
  END;
}

function getYoutubeData($token)
{   
    $json_url ='https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&access_token='.$token;
    $json = file_get_contents($json_url);
    $json_output = json_decode($json);

    if($json_output->items[0]->id){
            $id = $json_output->items[0]->id;
    }else{
            $id = "";
    }
    return $id;
}

function youtubefollowers($channelID)
{   
    $json_url ='https://www.googleapis.com/youtube/v3/channels?part=statistics&id='.$channelID.'&key=AIzaSyAlgOkFu2KlYJAQwt5j3jO1rUARpPzAIww';
    $json = file_get_contents($json_url);
    $json_output = json_decode($json);

    if($json_output->items[0]->statistics->subscriberCount){
            $subscriberCount = $json_output->items[0]->statistics->subscriberCount;
    }else{
            $subscriberCount = 0;
    }
    return $subscriberCount;
}
?>

<!doctype html>
<html>
  <head>
    <title>YouTube</title>
  </head>
  <body>
   <?=$htmlBody?>
  </body>
</html>
于 2015-08-11T06:17:48.077 回答