Pages

Monday, August 5, 2013

PHP CODE FOR CREATING A NEW FOLDER IN GOOGLE DRIVE API

         PHP CODE FOR CREATING A NEW FOLDER IN GOOGLE DRIVE API




require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
$client = new Google_Client();
// Get your credentials from the APIs Console
//$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('your client id');
$client->setClientSecret('your client secret id');
$client->setRedirectUri('your redirect uri');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));


$client->setAccessToken(file_get_contents('conf.json'));
if ($client->getAccessToken()) {
//It means that I have the right to manipulate whatever service
// Choose the service you want to use
    $service = new Google_DriveService($client);

    $file = new Google_DriveFile();
    $file->setTitle('test'); //name of the folder
    $file->setDescription('A test folder');
    $file->setMimeType('application/vnd.google-apps.folder');
    $createdFile = $service->files->insert($file, array(
        'mimeType' => 'application/vnd.google-apps.folder',
    ));

   // print_r($createdFile);
    print "folder created sucessfully";
   
    echo "folder id is" . $createdFile->id;
   
}

CODE TO UPLOAD ANY TYPE OF FILE TO GOOGLE DRIVE API AFTER CREATING AN ACCESS TOKEN USING PHP

CODE TO UPLOAD ANY TYPE OF FILE TO GOOGLE DRIVE API   AFTER CREATING AN ACCESS TOKEN USING PHP

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';


$client = new Google_Client();

// Get your credentials from the APIs Console
//$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('your client id');
$client->setClientSecret('your client secret id'); 
$client->setRedirectUri('your resirect uri');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));


$client->setAccessToken(file_get_contents('conf.json'));
if ($client->getAccessToken()) {
//It means that I have the right to manipulate whatever service
// Choose the service you want to use
    $service = new Google_DriveService($client);

    $file = new Google_DriveFile();
    $file->setTitle('hai');
    $file->setDescription('A test document');
    $file->setMimeType('text/plain');
    $data = file_get_contents('hello.txt'); //example file
    
    $createdFile = $service->files->insert($file, array(
        'data' => $data,
        'mimeType' => 'text/plain',
    ));
   
   // print_r($createdFile);
    print "file uploaded sucessfully";
   
    

?>

Sunday, August 4, 2013

CODE TO CREATE A CONNECTION WITH GOOGLE DRIVE API USING PHP AND SAVE AUTHENTICATION TOKEN


CODE TO CREATE A CONNECTION WITH GOOGLE DRIVE API USING PHP AND SAVE AUTHENTICATION TOKEN 

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

session_start();
$client = new Google_Client();
// Get your credentials from the APIs Console
//$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('your client side');
$client->setClientSecret('your client secret id');
$client->setRedirectUri('your client redirect uri');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

////I will address building permit
$authUrl = $client->createAuthUrl();
print "Connect Me!";
echo $authUrl;
//Return of the address with the code that I can autenticarme
if (isset($_GET['code'])) {
  $accessToken = $client->authenticate($_GET['code']);
  
  file_put_contents('conf.json', $accessToken);
  $client->setAccessToken($accessToken);
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}