Pages

Monday, August 5, 2013

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";
   
    

?>

No comments:

Post a Comment