如何通过google plus api插入时刻?
Posted
技术标签:
【中文标题】如何通过google plus api插入时刻?【英文标题】:How to insert moment through google plus api? 【发布时间】:2013-06-05 22:36:54 【问题描述】:我正在使用带有 codeigniter 的 google plus api,一切正常,除了当我尝试插入时刻时,它显示身份验证错误,而我能够通过 $this->plus->people->get('me')
获取 G+ 信息。
我在 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 ) 在 /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】:我通过 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);
【讨论】:
是的,您需要确保请求的活动是 OAuth 流程的一部分。以上是关于如何通过google plus api插入时刻?的主要内容,如果未能解决你的问题,请参考以下文章
用于在 Facebook 等墙上发布的 Google plus API
如何通过 API 向 Google Drive 添加/创建/插入文件?
社交应用程序如何将帖子缓冲到 google plus 个人资料