谷歌API即时错误代码400 PHP

时间:2015-04-17 08:53:20

标签: php google-api-php-client

我试图将某些活动发布到他们的谷歌+中的用户个人资料。

我一直在搜索关于时刻问题的所有帖子,但我仍然无法解决我的问题。以下是我的代码

    $requestVisibleActions = array(
    'http://schemas.google.com/AddActivity');
    $client = new Google_Client();
    $client->setApplicationName("PHP Google OAuth Login Example");
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->setDeveloperKey($simple_api_key);
    $client->addScope("https://www.googleapis.com/auth/plus.login");
    $client->addScope("https://www.googleapis.com/auth/plus.me");
    $client->addScope("https://www.googleapis.com/auth/userinfo.email");    
    $client->setRequestVisibleActions($requestVisibleActions);

    $plus = new Google_Service_Plus($client);

    // Add Access Token to Session
    if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }

    // Set Access Token to make Request
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    $client->setAccessToken($_SESSION['access_token']);
    }

    // Post moment from mysite
    if ($client->getAccessToken()) {
    $moment = new Google_Service_Plus_Moment();
    $moment->setType('http://schemas.google.com/AddActivity');
    $itemScope = new Google_Service_Plus_ItemScope();
    $itemScope->setUrl('http://developers.google.com/+/web/snippet/examples/thing');
    $moment->setTarget($itemScope);
    $momentResult = $plus->moments->insert('me', 'vault',$moment);
    $_SESSION['access_token'] = $client->getAccessToken();
    } else {    
    $authUrl = $client->createAuthUrl();
    redirect($authUrl);
    }

但是我收到了Google异常错误

输入:Google_Service_Exception

Message: Error calling POST 
https://www.googleapis.com/plus/v1/people/me/moments/vault?key=xxxxxxx: 
(400) Unable to fetch metadata.
Filename: /home2/mysite/public_html/application/libraries/google-api-php-client-master/src/Google/Http/REST.php

当我尝试访问帖子网址时,我得到了这个。

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

我真的不知道我错在哪里。请帮帮我

1 个答案:

答案 0 :(得分:1)

Moments: insert记录代表用户操作的时刻,例如在博客上进行购买或评论。写作时刻涉及指定类型,即时刻类型,并发布该类型的时刻所需的字段。

Moments描述了用户在您的应用中加入的活动。

Momemt类型与App Activity Types相同:

  

的addAction   ,BuyAction,CheckInAction,CommentAction,CreateAction,DiscoverAction,ListenAction,ReserveAction,ReviewAction,WantAction

片刻发布到用户的Google+信息流。 可以将某些活动发布到Google +中的用户个人资料中。