MDL-47297 repository_googledocs: Use the newer Google API

This commit is contained in:
Frederic Massart 2014-09-30 19:59:02 +08:00 committed by Marina Glancy
parent 27bd4e4b86
commit 28cdc0439f
2 changed files with 66 additions and 11 deletions

55
lib/google/lib.php Normal file
View File

@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle's lib to use for the Google API.
*
* @package core
* @copyright 2014 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/weblib.php');
// Update the include_path so that the library can use require_once in its own files.
set_include_path(get_include_path() . PATH_SEPARATOR . $CFG->libdir . '/google');
require_once($CFG->libdir . '/google/Google/Client.php');
require_once($CFG->libdir . '/google/curlio.php');
/**
* Wrapper to get a Google Client object.
*
* This automatically sets the config to Moodle's defaults.
*
* @return Google_Client
*/
function get_google_client() {
global $CFG, $SITE;
make_temp_directory('googleapi');
$tempdir = $CFG->tempdir . '/googleapi';
$config = new Google_Config();
$config->setApplicationName('Moodle ' . $CFG->release);
$config->setIoClass('moodle_google_curlio');
$config->setClassConfig('Google_Cache_File', 'directory', $tempdir);
$config->setClassConfig('Google_Auth_OAuth2', 'access_type', 'online');
$config->setClassConfig('Google_Auth_OAuth2', 'approval_prompt', 'auto');
return new Google_Client($config);
}

View File

@ -26,8 +26,8 @@
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');
require_once($CFG->libdir . '/google/Google_Client.php');
require_once($CFG->libdir . '/google/contrib/Google_DriveService.php');
require_once($CFG->libdir . '/google/lib.php');
require_once($CFG->libdir . '/google/Google/Service/Drive.php');
/**
* Google Docs Plugin
@ -47,7 +47,7 @@ class repository_googledocs extends repository {
/**
* Google Drive Service.
* @var Google_DriveService
* @var Google_Drive_Service
*/
private $service = null;
@ -77,12 +77,12 @@ class repository_googledocs extends repository {
$callbackurl = new moodle_url(self::CALLBACKURL);
$this->client = new Google_Client();
$this->client = get_google_client();
$this->client->setClientId(get_config('googledocs', 'clientid'));
$this->client->setClientSecret(get_config('googledocs', 'secret'));
$this->client->setScopes(array('https://www.googleapis.com/auth/drive.readonly'));
$this->client->setScopes(array(Google_Service_Drive::DRIVE_READONLY));
$this->client->setRedirectUri($callbackurl->out(false));
$this->service = new Google_DriveService($this->client);
$this->service = new Google_Service_Drive($this->client);
$this->check_login();
}
@ -311,7 +311,7 @@ class repository_googledocs extends repository {
try {
// Retrieving files and folders.
$response = $this->service->files->listFiles($params);
} catch (Google_ServiceException $e) {
} catch (Google_Service_Exception $e) {
if ($e->getCode() == 403 && strpos($e->getMessage(), 'Access Not Configured') !== false) {
// This is raised when the service Drive API has not been enabled on Google APIs control panel.
throw new repository_exception('servicenotenabled', 'repository_googledocs');
@ -416,11 +416,11 @@ class repository_googledocs extends repository {
public function get_file($reference, $filename = '') {
global $CFG;
$request = new Google_HttpRequest($reference);
$httpRequest = Google_Client::$io->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
$auth = $this->client->getAuth();
$request = $auth->authenticatedRequest(new Google_Http_Request($reference));
if ($request->getResponseHttpCode() == 200) {
$path = $this->prepare_file($filename);
$content = $httpRequest->getResponseBody();
$content = $request->getResponseBody();
if (file_put_contents($path, $content) !== false) {
@chmod($path, $CFG->filepermissions);
return array(