2010-05-30 46 views
7

Tôi muốn sử dụng api youtube để có được những người sử dụng video đăng ký mới với cuộc gọi api này:Cách đăng nhập vào Youtube bằng PHP?

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

Nếu không đăng nhập tôi nhận được câu trả lời này:

User authentication required. 
Error 401 

Làm thế nào tôi có thể đăng nhập vào youtube từ php?

Trả lời

5

Bạn có thể sử dụng OAuth, AuthSub hoặc ClientLogin. ClientLogin là đơn giản nhất (nó chỉ sử dụng tên người dùng/mật khẩu), nhưng không khuyến khích vì nó yêu cầu người dùng chuyển thông tin đăng nhập của họ cho bạn. AuthSub và OAuth thì không. Google PHP library hiện tại dường như chỉ hỗ trợ AuthSub (PHP example) và ClientLogin.

+0

Xin giải thích downvote của bạn. –

0

Đây là chức năng đơn giản để đăng nhập. Đó là thông báo lỗi trả về hoặc 0 khi thành công. Tôi sử dụng lib curl riêng nhưng it't khá rõ ràng rằng $ this-> curl-> SetHeader có thể được thay thế bằng curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ tiêu đề)

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; 
    } 
0

Cái này đang làm việc cho tôi. Đăng nhập bằng tài khoản google và đăng nhập thông tin kênh người dùng.

Download Code

<?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> 
Các vấn đề liên quan