Search code examples
phpgoogle-apigoogle-api-php-client

Error: 403, Forbidden error in creating circle in Google plus


Here, I want to create circle in google plus using API. I got https://developers.google.com/+/domains/api/circles/insert link for creating circle.

I did my code with perfect.

$headers = array
      (
        'Content-Type: application/json'
      );
      
      $ch = curl_init();
      # Setup request to send json via POST.
      $jsonData = json_encode( array( "displayName"=> "abc" ) );
      //echo "https://www.googleapis.com/plusDomains/v1/people/".$socialuserId."/circles?access_token=".$accessToken;exit;
      curl_setopt( $ch, CURLOPT_URL, "https://www.googleapis.com/plusDomains/v1/people/".$socialuserId."/circles?access_token=".$accessToken);
      curl_setopt( $ch, CURLOPT_POSTFIELDS, $jsonData );
      curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt( $ch, CURLOPT_POST, true );
      # Return response instead of printing.
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
      # Send request.
      $result = curl_exec($ch);
      curl_close($ch);

Here, $socialuserId and $accessToken I am getting right. But I am getting Forbidden Error like below.

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Forbidden"
   }
  ],
  "code": 403,
  "message": "Forbidden"
 }
}

What can be the reason for this error?


Solution

  • The error "403 forbidden" can be returned if the service is turned off inside the admin console or if the user which you are trying to create a circle for, has not created a Google Plus profile. Here is a sample of an implementation with the Google PHP Client Library version 2.0.3 but your code should also work.

    <?php
    
    session_start();
    
    //INCLUDE PHP CLIENT LIBRARY
    require_once "google-api-php-client-2.0.3/vendor/autoload.php";
    
    $client = new Google_Client();
    $client->setAuthConfig("client_credentials.json");
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/createCircle.php');
    $client->addScope(array(
      "https://www.googleapis.com/auth/plus.circles.write",
      "https://www.googleapis.com/auth/plus.me")
    );
    
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    
        $client->setAccessToken($_SESSION['access_token']);
    
        $service = new Google_Service_PlusDomains($client);
    
        $circle = new Google_Service_PlusDomains_Circle(array(
          'displayName' => 'VIP Circle',
          'description' => 'Best of the best'
          )
        );
    
        $userId = 'me';
    
        $newcircle = $service->circles->insert($userId, $circle);
    
        echo "Circle created: ".$newcircle->id." - ".$newcircle->selfLink;
    
    } else {
    
        if (!isset($_GET['code'])) {    
    
              $auth_url = $client->createAuthUrl();
              header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    
        } else {  
    
          $client->authenticate($_GET['code']);
          $_SESSION['access_token'] = $client->getAccessToken();
    
          $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/createCircle.php';
          header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    
        }
    
    }
    
    
    ?>
    

    Make sure to review the following references: https://developers.google.com/+/domains/authentication/scopes https://developers.google.com/+/domains/authentication/ https://support.google.com/a/answer/1631746?hl=en

    I hope this helps!