How to insert moment through google plus api?-Collection of common programming errors
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 🙂