Reputation: 6435
I am trying to connect a PHP App located on an online server to Google Drive API, I have given my API key the right credentials as seen below:
Here is my code:
Class googleDriveAPIController Extends baseController {
/**
* Retrieve a list of File resources.
*
* @param Google_Service_Drive $service Drive API service instance.
* @return Array List of Google_Service_Drive_DriveFile resources.
*/
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
public function index() {
set_include_path('google-api-php-client/src');
require_once(get_include_path() . '/Google/autoload.php');
require_once(get_include_path() . '/Google/Client.php');
require_once(get_include_path() . '/Google/Service.php');
require_once(get_include_path() . '/Google/Http/MediaFileUpload.php');
require_once(get_include_path() . '/Google/Service/Drive.php');
$client = new Google_Client();
$client->setApplicationName("My Application");
$client->setDeveloperKey("AIzaSyAmWcZArf7NCk4d65HoKZzLENCJ6cx9fNg");
$service = new Google_Service_Drive($client);
$this->retrieveAllFiles($service);
}
}
I get the following error:
An error occurred: Error calling GET https://www.googleapis.com/drive/v3/files?key=AIzaSyAmWcZArf7NCk4d65HoKZzLENCJ6cx9fNg: (403) There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.
I have granted access to this application so not sure what is going on here, anyone know what I am doing wrong?
Many Thanks
Upvotes: 0
Views: 2362
Reputation: 117156
Your problem is that you need to fix the restriction. You must be running it from an incorrect location.
*shabazejaz.co.uk/*
Might be a better value. You will need to test it.
On a side note:
Browser API key is used for accessing public data.
Files: list Lists the user's files.
For you to be able to run file.list you are going to have to be authenticated using Oauth2 or a service account. Browser API key isn't going to work.
Upvotes: 0