0

This PHP code below has worked for years but since some days ago it has stopped working and I get an error which I show below
The purpose of this code is to update and delete members from the group list in Google Apps.

I searched a lot on Google but couldn't find an answer

Google API client used
https://github.com/googleapis/google-api-php-client

Google Workspace Admin SDK
https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups

PHP CODE

if (isset($_GET['mode']))
    $mode = $_GET["mode"];
else
    $mode="TASK";

// credentials (with domain delegation)
putenv('GOOGLE_APPLICATION_CREDENTIALS=gaiviweb2web-193e903a022f.json');

// create client
$client = new Google_Client();

// apply credentials
$client->useApplicationDefaultCredentials();

// add scope api
$client->addScope(Google_Service_Directory::ADMIN_DIRECTORY_GROUP);

// impersonate user
$client->setSubject('myemail@mydomain.com');

$client->setUseBatch(true);

$batch = new Google_Http_Batch($client);

// returns a Guzzle HTTP Client
$httpClient = $client->authorize();

// service class
$service = new Google_Service_Directory($client);

$serverName = "myserver"; 
$connectionInfo = array( "Database"=>"mydb", "UID"=>"myuser", "PWD"=>"mypw");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {

    $sql = "my_stored_procedure;";

    $stmt = sqlsrv_query( $conn, $sql);
    if( $stmt === false ) {
         die( print_r( sqlsrv_errors(), true));
    }
    
    // get results
    while($obj = sqlsrv_fetch_object( $stmt)) {
    
        $lista = $obj->IDGruppo;
        $action= $obj->Action;
        $email= $obj->Email;
        $ragione= $obj->Ragione;
        $id= $obj->ID;

        // email 
        $account = array('email' => $email,
                    'kind' => 'admin#directory#member',
                    'role' => 'MEMBER',
                    'type' => 'USER');
        

        // set member
        $member= new Google_Service_Directory_Member($account);                        

        switch($action)
        {
            case 'ADD':
                $request = $service->members->insert($lista, $member);
                break;

            case 'DEL':
                $request = $service->members->delete($lista, $email);
                break;
        }

        // add to batch
        $batch->add($request, $id);
        
        // update
        if ($mode == 'TASK')
        {

            $sqlupd = "Update myTable set Process=1 where ID=".$id;
            $cmd = sqlsrv_query( $conn, $sqlupd);
        }
        
    }

    // run
    $results = $batch->execute();
    
    echo json_encode($results);
     
}else{
     die( print_r( sqlsrv_errors(), true));
}

ERROR

Fatal error: Uncaught Google_Service_Exception: Error 502 (Server Error)!!1
in C:\mypath\www\src\Google\Http\REST.php on line 118

What can be the problem?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Luka Milani
  • 1,541
  • 14
  • 21
  • 502 is an internal error with google have you tried reporting it? https://issuetracker.google.com/issues?q=componentid:191635%2B – Linda Lawton - DaImTo Feb 10 '21 at 16:31
  • Thanks for your suggestion, i just reported this issue by following your link – Luka Milani Feb 10 '21 at 17:23
  • Can you clarify where in the code you're encountering this error? – Iamblichus Feb 11 '21 at 08:18
  • @Iamblichus The error is at `REST.php` (google-api-php-client) on line 118, no any evidence where the error is generated, the error happen during the line: `throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body));` in the `public static function decodeHttpResponse` (REST.php) There are no extra information also in `php_errors.log` that's why i dont have any clue – Luka Milani Feb 11 '21 at 13:04
  • You don't have information on where **in your code** this is failing? – Iamblichus Feb 12 '21 at 09:21

0 Answers0