views:

192

answers:

2

Hi,

I want to use the youtube api to get the users new subscription videos with this api call:

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

Without logging in I get this response:

User authentication required.
Error 401

How can I login to youtube from php?

+3  A: 

You can use OAuth, AuthSub, or ClientLogin. ClientLogin is simplest (it just uses a username/password), but discouraged because it requires users to turn over their credentials to you. AuthSub and OAuth do not. Google's PHP library currently seems to only support AuthSub (PHP example) and ClientLogin.

Matthew Flaschen
+1  A: 

Here is simple function to do login. It's return error message or 0 on success. I used own curl lib but it't pretty clear that $this->curl->SetHeader can be replaced with 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;
    }
SET