如何通过谷歌加api插入时刻?

时间:2013-06-10 09:49:37

标签: php codeigniter google-api google-plus google-apps

我正在使用google plus api和codeigniter,一切正常,除非我尝试插入片刻时,它显示身份验证错误,而我能够通过$this->plus->people->get('me')获取G +信息。

我在CodeIgniter Library Construct中有这个:

    $CI =& get_instance();
    $CI->config->load('googleplus');

    require APPPATH .'libraries/google-api-php-client/src/Google_Client.php';
    require APPPATH .'libraries/google-api-php-client/src/contrib/Google_PlusService.php';
    $cache_path = $CI->config->item('cache_path');
    $GLOBALS['apiConfig']['ioFileCache_directory'] = ($cache_path == '') ? APPPATH .'cache/' : $cache_path;

    $this->client_id = $CI->config->item('client_id', 'googleplus');
    $this->client_secret = $CI->config->item('client_secret', 'googleplus');
    $this->redirect_uri = $CI->config->item('redirect_uri', 'googleplus');

    $this->client = new Google_Client();
    $this->client->setApplicationName($CI->config->item('application_name', 'googleplus'));
    $this->client->setClientId($this->client_id);
    $this->client->setClientSecret($this->client_secret);
    $this->client->setRedirectUri($this->redirect_uri);
    $this->client->setDeveloperKey($CI->config->item('api_key', 'googleplus'));
    $this->client->setAccessType("offline");
    $this->client->setScopes(array('https://www.googleapis.com/auth/plus.me','https://www.googleapis.com/auth/plus.login'));
    $this->client-> setApprovalPrompt("force");
    $this->plus = new Google_PlusService($this->client); 

以下是我正在做的插入片刻的示例。

    $target = new Google_ItemScope();
    $target->url = 'https://developers.google.com/+/plugins/snippet/examples/thing';

    $moment = new Google_Moment();
    $moment->type = "http://schemas.google.com/AddActivity";
    $moment->target = $target;

    $this->plus->moments->insert('me', 'vault', $moment);

我在尝试插入片刻时遇到此错误。

致命错误:未捕获的异常'Google_ServiceException',并显示错误“错误调用POST https://www.googleapis.com/plus/v1/people/me/moments/vault?key= * ** * ** : (401)未经授权'在/application/libraries/google-api-php-client/src/io/Google_REST.php:66堆栈跟踪:#0 / application / libraries / google-api-php-client / src / io / Google_REST.php(36):Google_REST :: decodeHttpResponse(Object(Google_HttpRequest))#1 /application/libraries/google-api-php-client/src/service/Google_ServiceResource.php(186):Google_REST :: execute(Object( Google_HttpRequest))#2 /application/libraries/google-api-php-client/src/contrib/Google_PlusService.php(167):Google_ServiceResource-> __ call('insert',Array)#3 / application / libraries / googleplus。 php(290):Google_MomentsServiceResource-> insert(位于第66行的/application/libraries/google-api-php-client/src/io/Google_REST.php

我不知道我做错了什么,请用代码向我建议。谢谢:))

1 个答案:

答案 0 :(得分:1)

我通过https://gist.github.com/silvolu/5054214解决了这个问题。我在CI Library Construct中添加了这个。

// set $requestVisibleActions to write moments
$requestVisibleActions = array(
'http://schemas.google.com/AddActivity',
'http://schemas.google.com/ReviewActivity');
$this->client->setRequestVisibleActions($requestVisibleActions);

此外,我用它来写下片刻并为我工作。 :)

  $moment_body = new Google_Moment();
  $moment_body->setType("http://schemas.google.com/AddActivity");
  $item_scope = new Google_ItemScope();
  $item_scope->setUrl("https://developers.google.com/+/plugins/snippet/examples/thing");
  $moment_body->setTarget($item_scope);
  $momentResult = $this->plus->moments->insert('me', 'vault', $moment_body);