From d8c660c6d524ad1226abeb00e8d4de4039eae1e1 Mon Sep 17 00:00:00 2001
From: Michael Gapczynski <mtgap@owncloud.com>
Date: Thu, 16 May 2013 20:09:32 -0400
Subject: Switch to using Google Drive SDK, closes #2047

---
 apps/files_external/ajax/google.php | 83 ++++++++++++++-----------------------
 1 file changed, 30 insertions(+), 53 deletions(-)

(limited to 'apps/files_external/ajax')

diff --git a/apps/files_external/ajax/google.php b/apps/files_external/ajax/google.php
index 70adcb2c2ad..bd5a6099bb3 100644
--- a/apps/files_external/ajax/google.php
+++ b/apps/files_external/ajax/google.php
@@ -1,64 +1,41 @@
 <?php
 
-require_once 'Google/common.inc.php';
+require_once 'google-api-php-client/src/Google_Client.php';
 
 OCP\JSON::checkAppEnabled('files_external');
 OCP\JSON::checkLoggedIn();
 OCP\JSON::callCheck();
 
-$consumer = new OAuthConsumer('anonymous', 'anonymous');
-$sigMethod = new OAuthSignatureMethod_HMAC_SHA1();
-if (isset($_POST['step'])) {
-	switch ($_POST['step']) {
-		case 1:
-			if (isset($_POST['callback'])) {
-				$callback = $_POST['callback'];
-			} else {
-				$callback = null;
-			}
-			$scope = 'https://docs.google.com/feeds/'
-					.' https://docs.googleusercontent.com/'
-					.' https://spreadsheets.google.com/feeds/';
-			$url = 'https://www.google.com/accounts/OAuthGetRequestToken?scope='.urlencode($scope);
-			$params = array('scope' => $scope, 'oauth_callback' => $callback);
-			$request = OAuthRequest::from_consumer_and_token($consumer, null, 'GET', $url, $params);
-			$request->sign_request($sigMethod, $consumer, null);
-			$response = send_signed_request('GET', $url, array($request->to_header()), null, false);
-			$token = array();
-			parse_str($response, $token);
-			if (isset($token['oauth_token']) && isset($token['oauth_token_secret'])) {
-				$authUrl = 'https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token='.$token['oauth_token'];
-				OCP\JSON::success(array('data' => array('url' => $authUrl,
-														'request_token' => $token['oauth_token'],
-														'request_token_secret' => $token['oauth_token_secret'])));
-			} else {
+if (isset($_POST['client_id']) && isset($_POST['client_secret']) && isset($_POST['redirect'])) {
+	$client = new Google_Client();
+	$client->setClientId($_POST['client_id']);
+	$client->setClientSecret($_POST['client_secret']);
+	$client->setRedirectUri($_POST['redirect']);
+	$client->setScopes(array('https://www.googleapis.com/auth/drive'));
+	if (isset($_POST['step'])) {
+		$step = $_POST['step'];
+		if ($step == 1) {
+			try {
+				$authUrl = $client->createAuthUrl();
+				OCP\JSON::success(array('data' => array(
+					'url' => $authUrl
+				)));
+			} catch (Exception $exception) {
 				OCP\JSON::error(array('data' => array(
-					'message' => 'Fetching request tokens failed. Error: '.$response
-					)));
+					'message' => 'Step 1 failed. Exception: '.$exception->getMessage()
+				)));
 			}
-			break;
-		case 2:
-			if (isset($_POST['oauth_verifier'])
-				&& isset($_POST['request_token'])
-				&& isset($_POST['request_token_secret'])
-			) {
-				$token = new OAuthToken($_POST['request_token'], $_POST['request_token_secret']);
-				$url = 'https://www.google.com/accounts/OAuthGetAccessToken';
-				$request = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $url,
-																 array('oauth_verifier' => $_POST['oauth_verifier']));
-				$request->sign_request($sigMethod, $consumer, $token);
-				$response = send_signed_request('GET', $url, array($request->to_header()), null, false);
-				$token = array();
-				parse_str($response, $token);
-				if (isset($token['oauth_token']) && isset($token['oauth_token_secret'])) {
-					OCP\JSON::success(array('access_token' => $token['oauth_token'],
-											'access_token_secret' => $token['oauth_token_secret']));
-				} else {
-					OCP\JSON::error(array('data' => array(
-						'message' => 'Fetching access tokens failed. Error: '.$response
-						)));
-				}
+		} else if ($step == 2 && isset($_POST['code'])) {
+			try {
+				$token = $client->authenticate($_POST['code']);
+				OCP\JSON::success(array('data' => array(
+					'token' => $token
+				)));
+			} catch (Exception $exception) {
+				OCP\JSON::error(array('data' => array(
+					'message' => 'Step 2 failed. Exception: '.$exception->getMessage()
+				)));
 			}
-			break;
+		}
 	}
-}
+}
\ No newline at end of file
-- 
cgit v1.2.3