错误:403,在Google plus中创建cirlcle时出现禁止错误

时间:2016-11-15 16:05:56

标签: php google-api google-api-php-client

在这里,我想使用api在google plus中创建圈子。我获得了创建圈子的https://developers.google.com/+/domains/api/circles/insert链接。 我完美地完成了我的代码。

$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);

在这里,我$socialuserId$accessToken我正确。 但是我得到了如下的禁止错误。

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

出现此错误的原因是什么? 谢谢你提前。

1 个答案:

答案 0 :(得分:1)

如果在管理控制台内关闭服务,或者您尝试为其创建圈子的用户尚未创建Google Plus个人资料,则可以返回错误“403 forbidden”。以下是使用Google PHP Client Library版本2.0.3的实现示例,但您的代码也应该可以使用。

<?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));

    }

}


?>

请务必查看以下参考资料:https://developers.google.com/+/domains/authentication/scopes https://developers.google.com/+/domains/authentication/ https://support.google.com/a/answer/1631746?hl=en

我希望这有帮助!