0

我正在使用带有codeigniter的google plus api,一切正常,除了当我尝试插入时刻时,它显示身份验证错误,而我能够通过$this->plus->people->get('me').

我在 CodeIgniter 库结构中有这个:

    $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) Unauthorized' in /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 中

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

4

1 回答 1

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);
于 2013-06-10T10:44:00.423 回答