doutong2132 2016-11-15 16:05
浏览 60
已采纳

错误:403,在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? Thanks in advanced.

  • 写回答

1条回答 默认 最新

  • drgawfsf1069 2016-11-16 04:25
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP