0
votes

i am trying to build a CMS which uses a Google Drive Account as storage and display its content, files/folders and the user shall be able to manage those files via my Angular FrontEnd. I am new to the google-api and thought i give it a try but i'm stuck.

I am using this library to get it working in PHP on my local xampp.

Git Google PHP API

I try to get a response in a .php test file of the users files like this:

/*************************************************
 * Ensure you've downloaded your oauth credentials
 ************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile())
{
    echo missingOAuth2CredentialsWarning();
    return;
}

/************************************************
 * The redirect URI is to the current page, e.g:
 * http://localhost:8080/simple-file-upload.php
 ************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

$client = new Google_Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope(Google_Service_Drive::DRIVE);
$service = new Google_Service_Drive($client);


$pageToken = null;
do {
    $response = $service->files->listFiles();

    foreach ($response->files as $file) {
        printf("Found file: %s (%s)\n", $file->name, $file->id);
    }
} while ($pageToken != null);

But this throws Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",

I got the examples of the git repository to work. But i didn't find a useful example to just query the users file structure.

1
I've got this error before and it happens when the request I'm sending is not authenticated. - Morfinismo
There are several authentications (OAuth,Service Accounts ) within that library, what is needed for such a request, or does it even matter? When i try the upload example, i get asked to log in and can upload a file to my drive successfully. If i copy the authentication part to this query file, it doesn't work. - AHahn

1 Answers

1
votes

I found what i was missing in my test application: Setting access tokens like this ->

   if (isset($_GET['code']))
{
    $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    $client->setAccessToken($token); //really needed? we also set it when the session is not empty 

    // store in the session also
    $_SESSION['upload_token'] = $token;

    // redirect back to the example
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

// set the access token as part of the client
if (!empty($_SESSION['upload_token']))
{
    $client->setAccessToken($_SESSION['upload_token']);
    if ($client->isAccessTokenExpired())
    {
        unset($_SESSION['upload_token']);
    }
}
else
{
    $authUrl = $client->createAuthUrl();
}

create a service with the client like ->

$service = new Google_Service_Drive($client);

And finally, the important part: Only access it, if we are logged in

/************************************************
 * If we're signed in then lets try to do something
 ************************************************/
if ( $client->getAccessToken())
{
    $response = $service->files->listFiles();

    foreach ($response->files as $file)
    {
        echo "<div>" . $file->name . " - " . $file->id . "</div><br>";
    }
}