Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

joenyambura's avatar

How to create Google Project Service Account using PHP Client Library

I am trying to create a Google project service account using the PHP client library as demonstrated https://cloud.google.com/iam/docs/reference/rest/v1/projects.serviceAccounts/create but I am getting this error

   Undefined property: Google\Service\Iam::$projects_serviceAccounts.

I am not sure but it seems V2 is being loaded but I need to use V1.

Here is my code

 namespace App\Services;

 use App\Models\GoogleProject;
  use App\Models\UserProject;
  use Google_Client;
  use Google_Service_Iam;
  use Google_Service_Iam_CreateServiceAccountRequest;
 use Google_Service_Iam_ServiceAccount;
 use Illuminate\Support\Facades\Log;

method

   public function createServiceAccount()
{
    $requestBody = new Google_Service_Iam_CreateServiceAccountRequest();

    // Required: Set the account ID for the service account
    $requestBody->setAccountId($this->googleProject->project_id);

   // Optional: Set additional properties for the service account
    $serviceAccount = new Google_Service_Iam_ServiceAccount();
    $serviceAccount->setDisplayName($this->googleProject->project_display_name);
    $serviceAccount->setDescription("Service account for {$this->googleProject->project_display_name}");
    // Assign the service account details to the request body
    $requestBody->setServiceAccount($serviceAccount);


    $name = 'projects/' . $this->googleProject->project_id;
    return $this->service->projects_serviceAccounts->create($name, $requestBody);

    }

I am using PHPStorm IDE that is showing me this warning

   Property 'projects_serviceAccounts' not found in \Google_Service_Iam

Kindly help me figure out where I am going wrong.

I am creating a Laravel APP that needs to automate Google project creation as well as service accounts for these projects.

0 likes
10 replies
LaryAI's avatar
Level 58

It looks like you're trying to access a property projects_serviceAccounts that doesn't exist on the Google_Service_Iam class. The correct way to create a service account using the Google PHP client library is to use the projects.serviceAccounts collection.

Here's how you can modify your code to create a service account:

namespace App\Services;

use Google_Client;
use Google_Service_Iam;
use Google_Service_Iam_CreateServiceAccountRequest;
use Google_Service_Iam_ServiceAccount;

class GoogleServiceAccountManager
{
    private $service;
    private $googleProject;

    public function __construct(GoogleProject $googleProject)
    {
        $client = new Google_Client();
        // Set up your Google_Client
        $client->useApplicationDefaultCredentials();
        $client->addScope(Google_Service_Iam::CLOUD_PLATFORM);

        $this->service = new Google_Service_Iam($client);
        $this->googleProject = $googleProject;
    }

    public function createServiceAccount()
    {
        $serviceAccount = new Google_Service_Iam_ServiceAccount();
        $serviceAccount->setDisplayName($this->googleProject->project_display_name);
        $serviceAccount->setDescription("Service account for {$this->googleProject->project_display_name}");

        $requestBody = new Google_Service_Iam_CreateServiceAccountRequest();
        $requestBody->setAccountId($this->googleProject->project_id);
        $requestBody->setServiceAccount($serviceAccount);

        $name = 'projects/' . $this->googleProject->project_id;

        // Use the 'projects.serviceAccounts' collection to create the service account
        $serviceAccounts = $this->service->projects_serviceAccounts;
        return $serviceAccounts->create($name, $requestBody);
    }
}

Make sure to replace the placeholder code where the Google_Client is set up with your actual configuration, including setting the correct credentials and scopes as needed for your application.

The key change here is that we're using $this->service->projects_serviceAccounts to access the projects.serviceAccounts collection, which is the correct way to interact with the service accounts in the Google IAM API.

Also, ensure that you have the latest version of the Google API PHP Client installed, as older versions may not have the correct service definitions. You can update the client library using Composer:

composer require google/apiclient:^2.0

This should resolve the Undefined property error you're encountering.

joenyambura's avatar

It has been two months and I have not found solutions.

Laravel community help me?

gych's avatar

When I check the example from the link you shared it looks quite different compared to what you have right now. Are you sure that you followed this example or did you follow another documentation?

Also are you using a constructor in the class file where createServiceAccount method is used?

joenyambura's avatar

@gych I have literally created a testing class and copy-pasted the code and I am getting that error

  <?php

   namespace App\Services;

   use Google_Client;
   use Google_Service_Iam;
   use Google_Service_Iam_CreateServiceAccountRequest;

   class TestingServiceAccountsService 
  {

public function createServiceAccount()
{
    // Initialize Google Client
    $client = new Google_Client();
    $client->setApplicationName('Google-iamSample/0.1');
    $client->useApplicationDefaultCredentials();
    $client->addScope('https://www.googleapis.com/auth/cloud-platform');

    // Initialize IAM service
    $service = new Google_Service_Iam($client);

    // Set project name
    $name = 'projects/my-project'; // Update with your project name

    // Create service account request body
    $requestBody = new Google_Service_Iam_CreateServiceAccountRequest();

    // Create service account
    $response = $service->projects_serviceAccounts->create($name, $requestBody);

    // Process response
    echo '<pre>', var_export($response, true), '</pre>', "\n";
}

}
gych's avatar

Did you follow this step?

This sample uses Application Default Credentials for authentication. If not already done, install the gcloud CLI from https://cloud.google.com/sdk and run gcloud beta auth application-default login. For more information, see https://developers.google.com/identity/protocols/application-default-credentials

Also add the correct resource name of your project

// Required. The resource name of the project associated with the service
// accounts, such as `projects/my-project-123`.
$name = 'projects/my-project';  // TODO: Update placeholder value.
joenyambura's avatar

@gych I have done all that. My problem is here

  $response = $service->projects_serviceAccounts->listProjectsServiceAccounts($name, $optParams);

Where I am getting Property 'projects_serviceAccounts' not found in \Google_Service_Iam

gych's avatar

@joenyambura Can you test which results you get with the code below:

  <?php

   namespace App\Services;

  use Google\Client;
  use Google\Service\Iam;

   class TestingServiceAccountsService 
  {

public function createServiceAccount()
{
    // Initialize Google Client
    $client = new Client();
    $client->setApplicationName('Google-iamSample/0.1');
    $client->useApplicationDefaultCredentials();
    $client->addScope('https://www.googleapis.com/auth/cloud-platform');

    // Initialize IAM service
    $service = new Iam($client);

    // Set project name
    $name = 'projects/my-project'; // Update with your project name

    // Create service account request body
    $requestBody = new Iam\CreateServiceAccountRequest();

    // Create service account
    $response = $service->projects_serviceAccounts->create($name, $requestBody);

    // Process response
    echo '<pre>', var_export($response, true), '</pre>', "\n";
}

}

I made it based on this example in their code base

/**
 * The "serviceAccounts" collection of methods.
 * Typical usage is:
 *  <code>
 *   $iamService = new Google\Service\Iam(...);
 *   $serviceAccounts = $iamService->projects_serviceAccounts;
 *  </code>
 */
joenyambura's avatar

@gych That is not still working at all. I am now getting this error

  Property 'projects_service_ccounts' not found in \Google\Service\Iam 
joenyambura's avatar

I have not yet found a solution. I have tried everything above but nothing is wokring.

Please or to participate in this conversation.