EasyWayB Discussion

A Easy Way to Start Your Business Online

Create Google Shared Drive with Google API in php

To run this quickstart, you need the following prerequisites
  • PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed
  • The Composer dependency management tool
  • A Google Cloud Platform project with the API enabled. To create a project and enable an API, refer to Create a project and enable the API
  • A Google account with Google Drive enabled.

Enquiry For Your Project

  • Install the Google Client Library :
    composer require google/apiclient:^2.0
  • Set up API : Create a file named quickstart.php in your working directory and copy in the following code:


    require __DIR__ . '/vendor/autoload.php';

    if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');
    }

    /**
    * Returns an authorized API client.
    * @return Google_Client the authorized client object
    */
    function getClient()
    {
    $client = new Google_Client();
    $client->setApplicationName('Google Drive API PHP Quickstart');
    $client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY);
    $client->setAuthConfig('credentials.json');
    $client->setAccessType('offline');
    $client->setPrompt('select_account consent');

    // Load previously authorized token from a file, if it exists.
    // The file token.json stores the user's access and refresh tokens, and is
    // created automatically when the authorization flow completes for the first
    // time.
    $tokenPath = 'token.json';
    if (file_exists($tokenPath)) {
    $accessToken = json_decode(file_get_contents($tokenPath), true);
    $client->setAccessToken($accessToken);
    }

    // If there is no previous token or it's expired.
    if ($client->isAccessTokenExpired()) {
    // Refresh the token if possible, else fetch a new one.
    if ($client->getRefreshToken()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    } else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for an access token.
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
    $client->setAccessToken($accessToken);

    // Check to see if there was an error.
    if (array_key_exists('error', $accessToken)) {
    throw new Exception(join(', ', $accessToken));
    }
    }
    // Save the token to a file.
    if (!file_exists(dirname($tokenPath))) {
    mkdir(dirname($tokenPath), 0700, true);
    }
    file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    }
    return $client;
    }

    // Get the API client and construct the service object.
    $client = getClient();
    $service = new Google_Service_Drive($client);

  • Run the API
    Run the sample using the following command:

    php quickstart.php
    The first time you run the sample, it prompts you to authorize access:

    1. Browse to the provided URL in your web browser.
      If you are not already signed in to your Google account, you are be prompted to sign in. If you are signed in to multiple Google accounts, you are asked to select one account to use for the authorization.
    2. Click the Accept button.
    3. Copy the code you’re given, paste it into the command-line prompt, and press Enter.
  • Create Google Shared Drives:

    $folder_name = 'EasyWayB Drive';
    $metadata = new Google_Service_Drive_TeamDrive();
    $metadata->setName($folder_name);
    $resp = $service->teamdrives->create(uniqid(), $metadata);
    $teamdrive_id = $resp->id;

    shared-drives
  • Permissions Access Google Shared Drives:

    $email = 'easywayb.customer.service@gmail.com';
    $metadata = new Google_Service_Drive_Permission();
    $metadata->setKind('drive#permission');
    $metadata->setEmailAddress($email);
    $metadata->setType('user');
    $metadata->setRole('organizer');
    $optParams = [
    'supportsTeamDrives' => true
    ];
    $permision = $service->permissions->create($teamdrive_id, $metadata, $optParams);
    $permision_id = $permision->id;

Or visit https://stackoverflow.com/questions/71698428/create-google-shared-drive-with-google-api-in-php.