此处有人知道Google开始使用OAuth2后如何访问Google相册API吗?他们的开发者网站上的PHP客户端库现在已经过时,不起作用!
我使用OAuth使用Google云端硬盘但照片不起作用! :(
首先,我使用Google_Client
成功验证用户身份。然后在重定向页面中,我正在尝试:
require_once("Google/Client.php");
//set up path for Zend GData, because Google Documentation uses that lib
$clientLibraryPath = '/path/to/ZendGData/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath);
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_Photos');
try
{
$authCode = $_GET['code']; //authorization code returned from google
//next create google OAuth Client object and validate...
$webAuth= new Google_Client();
$webAuth->setClientId($clientId);
$webAuth->setClientSecret($clientSecret);
$webAuth->authenticate($authCode); //this authenticate() works fine...
//now my problem is HOW do I tie this to GData API for Picasa :(
//I tried following but it throws error
//*Token invalid - Invalid token: Request token used when not allowed.*
$client = Zend_Gdata_AuthSub::getHttpClient($authCode);
$gp = new Zend_Gdata_Photos($client, "GData:2.0");
$userFeed = $gp->getUserFeed("default");
我也尝试了一堆第三方库,尝试将我的$webAuth
连接到Zend_GData_Photos
以我可以尝试的每种方式...我甚至尝试过原始卷曲调用,但没有任何工作!
有人能帮帮我吗?我在我的智慧结束......我无法相信Google在将他们的身份验证更新为OAuth时会留下一个功能齐全的库(PicasaWeb PHP API Ver 1.0)。
答案 0 :(得分:2)
我有同样的问题,但最后我又恢复了工作。 最棒的是,您不需要任何客户端库就可以访问私人照片。
我花了两天的时间尝试使用“服务帐户”#39;但没有运气。
然后我找到了这个页面: https://holtstrom.com/michael/blog/post/522/Google-OAuth2-with-PicasaWeb.html
帮助我实现了我想要的目标。 这是一篇相当长的文章,但它不应该花很长时间来解决它并让它工作。基本上,您需要使用“OAuth 2.0客户端ID'而不是服务帐户'在https://console.developers.google.com的项目中 在您的OAuth 2.0客户端ID'您将获得以下信息:
您将在验证过程中使用此数据。 在开始之前,您需要完成OAuth同意屏幕。
在该教程中,有一个注释要在DB中存储这些令牌,但在这种情况下,我建议直接在网页中显示它们。这更容易。 建议使用https而不是http,但它应该适用于两者。 我已经将https用于我的应用程序。
这是上述链接中文章的较短版本。
创建 oauth2.ph p文件并将其放在https://www.yoursite.com/oauth2.php
上<?php
if (isset($_GET['code']))
{
$clientId = 'your-client-id.apps.googleusercontent.com';
$clientSecret = 'your-client-secret';
$referer = 'https://www.yoursite.com/oauth2.php';
$postBody = 'code='.urlencode($_GET['code'])
.'&grant_type=authorization_code'
.'&redirect_uri='.urlencode($referer)
.'&client_id='.urlencode($clientId)
.'&client_secret='.urlencode($clientSecret);
$curl = curl_init();
curl_setopt_array( $curl,
array( CURLOPT_CUSTOMREQUEST => 'POST'
, CURLOPT_URL => 'https://accounts.google.com/o/oauth2/token'
, CURLOPT_HTTPHEADER => array( 'Content-Type: application/x-www-form-urlencoded'
, 'Content-Length: '.strlen($postBody)
, 'User-Agent: www.yoursite.com/0.1 +https://www.yoursite.com/'
)
, CURLOPT_POSTFIELDS => $postBody
, CURLOPT_REFERER => $referer
, CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed
, CURLOPT_TIMEOUT => 15 // max seconds to wait
, CURLOPT_FOLLOWLOCATION => 0 // don't follow any Location headers, use only the CURLOPT_URL, this is for security
, CURLOPT_FAILONERROR => 0 // do not fail verbosely fi the http_code is an error, this is for security
, CURLOPT_SSL_VERIFYPEER => 1 // do verify the SSL of CURLOPT_URL, this is for security
, CURLOPT_VERBOSE => 0 // don't output verbosely to stderr, this is for security
) );
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo($response);
echo($http_code);
}
else { echo 'Code was not provided.'; }
?>
要调整的字段:redirect_uri和client_id
https://www.yoursite.com/oauth2.php?code=some-random-code
将返回(打印)包含以下内容的json格式数据:access_token,token_type,expires_in和refresh_token。
Http响应代码应为200。 Access_token将用于获取女贞专辑数据。
使用内容
创建 index.php<?php
$curl = curl_init();
$url = 'https://picasaweb.google.com/data/entry/api/user/default';
curl_setopt_array( $curl,
array( CURLOPT_CUSTOMREQUEST => 'GET'
, CURLOPT_URL => $url
, CURLOPT_HTTPHEADER => array( 'GData-Version: 2'
, 'Authorization: Bearer '.'your-access-token' )
, CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed
) );
$response = curl_exec($curl);
$http_code = curl_getinfo($curl,CURLINFO_HTTP_CODE);
curl_close($curl);
echo($response . '<br/>');
echo($http_code);
?>
从第5步运行脚本后,您应该会从picasaweb API收到默认Feed。当我说'默认&#39;它,当您使用私人相册进行记录时,eans默认。从现在开始,您应该可以使用该方法访问您的picasa照片库。
访问令牌将在3600秒(1小时)后过期,因此您必须获得新的令牌。这可以通过以下脚本来实现:
$clientId = 'your-client-id.apps.googleusercontent.com';
$clientSecret = 'your-client-secret';
$referer = 'https://www.yoursite.com/oauth2.php';
$refreshToken = 'your-refresh-token';
$postBody = 'client_id='.urlencode($clientId)
.'&client_secret='.urlencode($clientSecret)
.'&refresh_token='.urlencode($refreshToken)
.'&grant_type=refresh_token';
$curl = curl_init();
curl_setopt_array( $curl,
array( CURLOPT_CUSTOMREQUEST => 'POST'
, CURLOPT_URL => 'https://www.googleapis.com/oauth2/v3/token'
, CURLOPT_HTTPHEADER => array( 'Content-Type: application/x-www-form-urlencoded'
, 'Content-Length: '.strlen($postBody)
, 'User-Agent: www.yoursite.com/0.1 +https://www.yoursite.com/'
)
, CURLOPT_POSTFIELDS => $postBody
, CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed
, CURLOPT_TIMEOUT => 15 // max seconds to wait
, CURLOPT_FOLLOWLOCATION => 0 // don't follow any Location headers, use only the CURLOPT_URL, this is for security
, CURLOPT_FAILONERROR => 0 // do not fail verbosely fi the http_code is an error, this is for security
, CURLOPT_SSL_VERIFYPEER => 1 // do verify the SSL of CURLOPT_URL, this is for security
, CURLOPT_VERBOSE => 0 // don't output verbosely to stderr, this is for security
) );
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if (strlen($response) < 1)
{ echo('fail 01'); }
$NOW = time();
$responseDecoded = json_decode($response, true); // convert returned objects into associative arrays
$expires = $NOW - 60 + intval($responseDecoded['expires_in']);
if ( empty($responseDecoded['access_token'])
|| $expires <= $NOW )
{ echo('fail 02'); }
echo($http_code . '<br/>');
echo($response . '<br/>');
echo($expires . '<br/>');
?>
您可以手动在单独的脚本中运行步骤7中的代码,只是为了获得另外3600秒的新访问令牌,但通常您希望将其自动化,以便在access_token到期时,您自动要求新的一个使用来自步骤4的refresh_token的调用。
UFFF。就是这样。我希望你能够把它运行起来。