Dear Stackoverflow Helper ... I need a big ask. Google Help page are very unhelpful and doesn’t explain it at all.
I have been trying to work out the Google Drive API, something they claim is easy, and yet for the last few days nothing has me more stumped.
Basically I have “underpants …?... profit” I just need that something.
So all I want to do is write a PHP script, which will be called without user interaction on my server by a cron job It calls the list of files in my Google drive Gives me the download links And I’ll sort it all out and download the latest file (to a certain criteria PDF certain file name)
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;
First Issue The required once is different to the samples the google gives, thus through much trial an error I have had to guess at which classes should be initialised.
I keep getting numerous errors about classes that are not activated
Fatal error: Class 'Google_Service' not found in ***\google-api-php-client\src\Google\Service\Drive.php on line 32
(Which I solved by 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
(This one I don’t know where the class lives, I tried searching all the php docs in “src\Google”, but no joy, but I could have missed it.)
Second Issue I just need it OAuth to do its thing so I can call this line
$GoogleDriveList = file_get_contents("https://www.googleapis.com/drive/v2/files?fields=items(id%2CdownloadUrl%2CoriginalFilename%2CcreatedDate%2CfileExtension)&key=APIKEYHERE");
Would some please give me the code to get AuthO working
This part has to be wrong, it just doesn’t make sense to me, and it seems like user input. But after days of searching I can’t find a single automatic system. And yet the very page this code is from seem to suggest that this is the answer.
//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
To me this looks like what I want https://developers.google.com/drive/web/auth/web-server but for the life of me I can’t find the simple things like how to initialise the getCredentials, and what parameters I have to pass to it?
I have also seen code on this URL but I am just too fried to understand it. Though it does look really promising. https://developers.google.com/accounts/docs/OAuth2WebServer#formingtheurl
Does anyone have a sample code of how to get this blast thing to work? I just need a full code sample of how the php OAuth system works. Thank you very much Sorry if it doesn't make sense but I am just fried …