返回文件夹ID但不创建文件夹

Using the code below

define('DRIVE_SCOPE', 'https://www.googleapis.com/auth/drive');
define('SERVICE_ACCOUNT_EMAIL', 'xxx.iam.gserviceaccount.com');
define('SERVICE_ACCOUNT_PKCS12_FILE_PATH', 'xxx');

function buildService($userEmail) {
    $key = file_get_contents(SERVICE_ACCOUNT_PKCS12_FILE_PATH);
    $auth = new Google_Auth_AssertionCredentials(
    SERVICE_ACCOUNT_EMAIL,
    array(DRIVE_SCOPE),
    $key);
    //$auth->sub = $userEmail;
    $client = new Google_Client();
    $client->setAssertionCredentials($auth);
    return new Google_Service_Drive($client);
}

$service = buildService('xxx.apps.googleusercontent.com');

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'Invoices',
    'mimeType' => 'application/vnd.google-apps.folder'
));

$file = $service->files->create($fileMetadata, array(
    'fields' => 'id'
));

printf("Folder ID: %s
", $file->id);

I have been trying to create a folder on my drive account through PHP. I believe everything is configured correctly and I am getting a result

Folder ID: 0B2nllBpB_k0NQWFNUjlSc0NUdE0

But the folder is not being created on my drive account. The same goes with creating files as well. It keeps returning an ID but nothing is actually created. What exactly am I doing wrong?

Your current code is authenticating as the service account and creating a file. The resultant file would then only be accessible to the service account, not your Google user. You can confirm this by doing a list of files instead of a create in your code, you'll get back the files you've previously created.

Your code has sub= commented out. Uncommented that line and make sure you've followed the domain-wide delegation instructions so that your code can authenticate and act as your Google user.