我试图使用谷歌php api在谷歌+发布时刻。我正在使用Codeigniter框架。我正在关注https://developers.google.com/+/api/latest/moments/insert文档,并使用了它的一些修改版本。我逐个测试了以下代码的每一行。似乎只有这行代码无效。
$momentResult = $this->googleplus->plus->moments->insert('me', 'vault', $this->googleplus->moment_body);
它会导致 401未经授权的错误,如下所示。
Google_Service_Exception Object
(
[errors:protected] => Array
(
[0] => Array
(
[domain] => global
[reason] => unauthorized
[message] => Unauthorized
)
)
[message:protected] => 'Error calling POST https://www.googleapis.com/plus/v1/people/me/moments/vault?key=my_key_from_console: (401) Unauthorized'
...
...
)
我使用了正确的api_key,secret_key,client_id,app_name,redirect_url,但仍然无法解决此错误。
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
include_once 'base_controller.php';
class Gp_auth extends Base_controller {
private $_gp_client;
public function __construct() {
parent::__construct();
$this->load->library('googleplus');
$this->_gp_client = $this->googleplus->client;
$this->_gp_client->setApplicationName("my_app_name");
$this->_gp_client->setDeveloperKey("my_developerkey_incase_it_needed");
$this->_gp_client->setClientId('my_client_id_from_console');
$this->_gp_client->setClientSecret('my_client_secret');
$this->_gp_client->setRedirectUri(base_url().'gp_auth');
}
public function index() {
if ($this->input->get_post('code')) {
try {
$this->_gp_client->authenticate($this->input->get_post('code'));
$access_token = $this->_gp_client->getAccessToken();
$this->session->set_userdata('access_token', $access_token);
redirect('/gp_auth/me');
} catch (Google_Auth_Exception $e) {
echo "<pre>";
print_r($e);
echo "</pre>";
}
} else {
$this->_gp_client->addScope(array(
'https://www.googleapis.com/auth/plus.login',
'https://www.googleapis.com/auth/plus.moments.write'
));
$this->_gp_client->setAccessType('online');
$this->_gp_client->setApprovalPrompt("force");
$this->_gp_client->setRequestVisibleActions('http://schemas.google.com/CreateActivity');
try {
echo anchor($this->_gp_client->createAuthUrl(), 'Conect Me');
} catch (Google_Auth_Exception $e) {
echo "<pre>";
print_r($e);
echo "</pre>";
}
}
}
public function me() {
try {
$this->_gp_client->setAccessToken($this->session->userdata('access_token'));
$response = $this->googleplus->plus->people->get('me');
$post_data = array(
'gp_id' => $response->id,
'gp_access_token' => $this->session->userdata('access_token'),
'post_body' => 'Silicon orchard limited logo.',
'post_attachment' => 'http://www.siliconorchard.com/img/logo.png'
);
$this->gp_post($post_data);
} catch (Google_Auth_Exception $e) {
echo "<pre>";
print_r($e);
echo "</pre>";
}
}
public function gp_post($post_data) {
$this->_gp_client->setAccessToken($post_data['gp_access_token']);
$this->_gp_client->setRequestVisibleActions(array('http://schemas.org/AddActivity'));
$this->googleplus->item_scope->setId("target-id-1");
$this->googleplus->item_scope->setType("http://schema.org/AddAction");
$this->googleplus->item_scope->setName("The Google+ Platform");
$this->googleplus->item_scope->setDescription("A page that describes just how awesome Google+ is!");
$this->googleplus->item_scope->setImage("https://developers.google.com/+/plugins/snippet/examples/thing.png");
$this->googleplus->moment_body->setObject($this->googleplus->item_scope);
try {
$momentResult = $this->googleplus->plus->moments->insert('me', 'vault', $this->googleplus->moment_body);
echo "<pre>";
print_r($momentResult);
echo "</pre>";
} catch (Google_Auth_Exception $e) {
echo "<pre>";
print_r($e);
echo "</pre>";
} catch (Google_Service_Exception $servic_exception) {
echo "<pre>";
print_r($servic_exception);
echo "</pre>";
}
}
}
这是googleplus库文件。
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Googleplus {
public function __construct() {
$CI = & get_instance();
$CI->config->load('googleplus');
(set_include_path(APPPATH."third_party/" . PATH_SEPARATOR . get_include_path()));
require_once 'Google/Client.php';
require_once 'Google/Service/Plus.php';
$cache_path = $CI->config->item('cache_path');
$GLOBALS['apiConfig']['ioFileCache_directory'] = ($cache_path == '') ? APPPATH . 'cache/' : $cache_path;
$this->client = new Google_Client();
$this->client->setApplicationName($CI->config->item('application_name', 'googleplus'));
$this->client->setClientId($CI->config->item('client_id', 'googleplus'));
$this->client->setClientSecret($CI->config->item('client_secret', 'googleplus'));
$this->client->setRedirectUri($CI->config->item('redirect_uri', 'googleplus'));
$this->client->setDeveloperKey($CI->config->item('api_key', 'googleplus'));
$this->plus = new Google_Service_Plus($this->client);
$this->moment_body = new Google_Service_Plus_Moment();
$this->moment_body->setType("http://schema.org/AddAction");
$this->item_scope = new Google_Service_Plus_ItemScope();
}
public function __get($name) {
if (isset($this->plus->$name)) {
return $this->plus->$name;
}
return false;
}
public function __call($name, $arguments) {
if (method_exists($this->plus, $name)) {
return call_user_func(array($this->plus, $name), $arguments);
}
return false;
}
}
发现了很多类似的问题,但没有解决方案。我正在使用谷歌php api的最新版本。
答案 0 :(得分:1)
有时同时使用OAuth和API /开发人员密钥会混淆API(即使它不应该)。
从代码中删除->setDeveloperKey
行应该可以解决此问题。
这里有一个未解决的问题:https://code.google.com/p/google-plus-platform/issues/detail?id=458
另一个潜在问题:
您在创建身份验证URL时使用此选项:
->setRequestVisibleActions('http://schemas.google.com/CreateActivity');
但是你试着写下这样的时刻:
->setType("http://schema.org/AddAction");
您必须在原始身份验证中包含此时刻类型,而不是/除CreateActivity之外。在身份验证发生后的稍后时间设置它不会更改当前访问令牌具有的权限。