亲爱的堆栈溢出帮助程序。。。我需要一个大要求。谷歌帮助页面是非常没有帮助的,并没有解释它。
我一直在努力开发Google Drive API,他们声称这很容易,但在过去的几天里,没有什么比这更让我困惑的了。
基本上我有"内裤…?…利润"我只是需要一些东西。
因此,我只想写一个PHP脚本,它将在没有用户交互的情况下由cron作业在我的服务器上调用它调用我的谷歌驱动器中的文件列表给我下载链接我会把它全部整理好并下载最新的文件(根据特定标准PDF特定文件名)
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service/Drive.php';
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId(IDHere);
$client->setClientSecret(SecretHere);
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_Service_Drive($client);
$authUrl = $client->createAuthUrl();
//Request authorization
print "Please visit:'n$authUrl'n'n";
print "Please enter the auth code:'n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
$GoogleDriveList = file_get_contents("https://www.googleapis.com/drive/v2/files?fields=items(id%2CdownloadUrl%2CoriginalFilename%2CcreatedDate%2CfileExtension)&key=APIKEYHERE");
echo $GoogleDriveList;
第一期所需的once与谷歌提供的样本不同,因此经过多次尝试,我不得不猜测哪些类应该初始化。
我不断收到许多关于未激活的类的错误
Fatal error: Class 'Google_Service' not found in ***'google-api-php-client'src'Google'Service'Drive.php on line 32
(我通过require_once‘google-api-php-client/src/google/Service.php’解决了这个问题;)
Fatal error: Class 'Google_Service_Resource' not found in ***'google-api-php-client'src'Google'Service'Drive.php on line 1283
(这个我不知道这个类住在哪里,我试着在"src''Google"中搜索所有的php文档,但没有什么乐趣,但我可能错过了。)
第二期我只需要它OAuth来做它的事情,这样我就可以把这条线称为
$GoogleDriveList = file_get_contents("https://www.googleapis.com/drive/v2/files?fields=items(id%2CdownloadUrl%2CoriginalFilename%2CcreatedDate%2CfileExtension)&key=APIKEYHERE");
一些人能给我代码让AuthO工作吗
这部分肯定是错的,它对我来说毫无意义,而且看起来像是用户输入。但经过几天的搜索,我找不到一个自动系统。然而,这段代码来自的页面似乎表明这就是答案。
//Request authorization
print "Please visit:'n$authUrl'n'n";
print "Please enter the auth code:'n";
$authCode = trim(fgets(STDIN));
对我来说,这就像我想要的https://developers.google.com/drive/web/auth/web-server但在我的一生中,我找不到简单的事情,比如如何初始化getCredentials,以及我必须传递给它什么参数?
我也看到了这个网址上的代码,但我太激动了,无法理解它。尽管它看起来确实很有前景。https://developers.google.com/accounts/docs/OAuth2WebServer#formingtheurl
有人有如何让这个爆炸事件发挥作用的示例代码吗我只需要一个php OAuth系统如何工作的完整代码示例。非常感谢抱歉,如果这没有意义,但我只是被炸了…
$CLIENT_ID = 'YOUR_CLIENT_ID..ENDS.WITH.apps.googleusercontent.com';
$CLIENT_SECRET = 'YOUR_CLIENT_SECRET';
$RefreshToken = 'YOUR_REFRESH_TOKEN';
$ReDirectURL = 'https://developers.google.com/oauthplayground'; #needs to be set, normally just set to the oauthplayground
$YOUR_API_KEY = 'YOUR_API_KEY';
$url = 'https://www.googleapis.com/oauth2/v3/token';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_POSTFIELDS,
'client_id=' . urlencode($CLIENT_ID) . '&' .
'client_secret=' . urlencode($CLIENT_SECRET) . '&' .
'refresh_token=' . urlencode($RefreshToken) . '&' .
'grant_type=refresh_token'
);
$AuthorizationReturn = curl_exec($ch);
curl_close($ch);
$lemmings = explode('"',$AuthorizationReturn);
$accessToken = $lemmings[3];
$GoogleDriveList = file_get_contents("https://www.googleapis.com/drive/v2/files?fields=items(id%2CdownloadUrl%2CoriginalFilename%2CcreatedDate%2CfileExtension)&key=$YOUR_API_KEY&access_token=$accessToken");
$GoogleDriveList = json_decode($GoogleDriveList, true);
#The rest is dependent on your goals ... good luck and I hope I saved you the stress
#Get List of Files on your Google Drive and find the id of the file ($i being a counter through the list)
$FILE_ID = $GoogleDriveList['items'][$i]['id'];
$DownloadedFilefromGoogleDrive = file_get_contents("https://www.googleapis.com/drive/v2/files/$FILE_ID?alt=media&key=$YOUR_API_KEY");
# and save ...
我收到错误:
Fatal error: Class 'Google_Service_Resource' ....
直到我加上这行:
require_once 'google-api-php-client/src/Google/autoload.php';
根据此问题和评论:https://stackoverflow.com/a/29620217/2911874