0
votes

I am using google plus api with codeigniter, everything works good except when I try to insert moment, it shows an authentication error while I am able to get G+ information through $this->plus->people->get('me').

I have this in 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); 

And here is sample what I am doing to insert moment.

    $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);

I am getting this error when trying to insert moment.

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling 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 Stack trace: #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( in /application/libraries/google-api-php-client/src/io/Google_REST.php on line 66

I don't know what I am doing wrong, please suggest me with code. Thanks :)

1

1 Answers

1
votes

I solved it by going https://gist.github.com/silvolu/5054214. I added this in CI Library Construct.

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

Further I used this to write moment and worked for me. :)

  $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);