PHP Google YouTube API Authentication


PHP Google YouTube API Authentication

我使用的是Google Data PHP API。我跟随https://developers.google.com/youtube/2.0/developers_guide_php#AuthSub_for_Web_Applications,期望我保留一个刷新令牌,并发送一个获取访问令牌的请求。刷新令牌是为拥有视频的用户提供的(我使用Java完成了这项工作)。但最后我仍然无法通过身份验证。这里怎么了?代码如下。

$refreshToken = '1/wY9.......';
$postString = 'client_id=' . $_SESSION['clientId'] .
        '&client_secret=' . $_SESSION['clientSecret'] . 
        '&refresh_token=' . $refreshToken .
        '&grant_type=refresh_token';
$ch = curl_init($_SESSION['oauth2Url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result);
$accessToken = $json->access_token;
$httpClient = Zend_Gdata_AuthSub::getHttpClient($accessToken);
$yt = new Zend_Gdata_YouTube($httpClient, null, $_SESSION['clientId'], $_SESSION['developerKey']);
$yt->setMajorProtocolVersion(2);
$youtubeId = 'xxxxxxxxxxx';
$videoEntry = $yt->getVideoEntry($youtubeId);
if ($videoEntry->getEditLink() !== null) {
    echo 'can edit!';
} else {
    echo 'cannot edit!';
}

*更新*

正如其他人建议的那样,我尝试了var_dump($httpClient),得到了以下结果:

object(Zend_Gdata_HttpClient)#1 (23) { 
  ["_authSubPrivateKeyId":"Zend_Gdata_HttpClient":private]=> NULL 
  ["_authSubToken":"Zend_Gdata_HttpClient":private]=> string(61) "yaXXXXX"
  ["_clientLoginToken":"Zend_Gdata_HttpClient":private]=> NULL
  ["_clientLoginKey":"Zend_Gdata_HttpClient":private]=> NULL 
  ["_streamingRequest":protected]=> NULL 
  ["config":protected]=> array(12) { 
    ["maxredirects"]=> int(5) 
    ["strictredirects"]=> bool(true) 
    ["useragent"]=> string(28) "Zend_Framework_Gdata/1.11.12" 
    ["timeout"]=> int(10) 
    ["adapter"]=> string(31) "Zend_Http_Client_Adapter_Socket" 
    ["httpversion"]=> string(3) "1.1" 
    ["keepalive"]=> bool(false) 
    ["storeresponse"]=> bool(true) 
    ["strict"]=> bool(true) 
    ["output_stream"]=> bool(false) 
    ["encodecookies"]=> bool(true) 
    ["rfc3986_strict"]=> bool(false) 
  } 
  ["adapter":protected]=> NULL 
  ["uri":protected]=> NULL 
  ["headers":protected]=> array(0) { } 
  ["method":protected]=> string(3) "GET" 
  ["paramsGet":protected]=> array(0) { } 
  ["paramsPost":protected]=> array(0) { } 
  ["enctype":protected]=> NULL 
  ["raw_post_data":protected]=> NULL 
  ["auth":protected]=> NULL 
  ["files":protected]=> array(0) { } 
  ["body_field_order":protected]=> array(0) { } 
  ["cookiejar":protected]=> NULL 
  ["last_request":protected]=> NULL 
  ["last_response":protected]=> NULL 
  ["redirectCounter":protected]=> int(0) 
  ["_unmaskStatus":protected]=> bool(false) 
  ["_queryBracketsEscaped":protected]=> bool(true)
} 

终于知道发生了什么。

身份验证实际上已经完成。但是不要使用$videoEntry->getEditLink()来测试服务是否经过身份验证。Zend PHP库已经很旧了。也许新的数据结构没有编辑链接。