我是谷歌客户端身份验证的新手。我正试图使用从这里下载的php谷歌客户端将文件上传到我的谷歌驱动器https://github.com/google/google-api-php-client
public function oauth2callback(){
set_include_path(get_include_path() . PATH_SEPARATOR . ROOT .DS. "vendors");
require_once 'Google/Client.php';
$client = new Google_Client();
$client->setClientId('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$client->setClientSecret('xxxxxxxxxxxxxxxxx');
$client->setRedirectUri('http://example.com/auth/oauth2callback');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setState('offline');
$authUrl = $client->createAuthUrl();
if (isset($_GET['code'])) {
$authCode = trim($_GET['code']);
$accessToken = $client->authenticate($authCode);
print_r($accessToken);
$client->setAccessToken($accessToken);
}
if ($client->getAccessToken()) {
// To do
}else{
$authUrl = $client->createAuthUrl();
echo "<a href='$authUrl'>Login Now</a>";
}
}
在回复中,我只收到了这个
{"access_token":"ya29.eQAeXvi4c5CAGRwAAAAKr55Tljr6z_GpdfjyY0xbrD15XGikNRL-D724Hx1L_g","token_type":"Bearer","expires_in":3600,"created":1410085058}
而没有任何刷新令牌。
我只想从响应中获取刷新令牌,以便以后使用。
我也关注这个问题
将状态设置为脱机,吊销了以前属于此应用的所有访问权限,甚至尝试使用新的帐户/应用程序。。但从未收到刷新令牌。。
请大家帮帮我。。。
我缺少access_type=offline。。
因此,添加
$client->setAccessType('offline');
解决了问题。