doutong2132 2016-11-15 08: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-15 20: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!

    展开全部

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

报告相同问题?

悬赏问题

  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 某东JD算法逆向算法
  • ¥15 求GCMS辅导数据分析
  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部