使用plusapi在google moment中发布时PHP授权失败
我试图用php为google plus moment写一篇文章,我使用了下面的代码,但当我试图发表文章时,我发现授权错误,我使用的是CodeIgniter使用plusapi在google moment中发布时PHP授权失败,php,codeigniter,authorization,google-api-php-client,Php,Codeigniter,Authorization,Google Api Php Client,我试图用php为google plus moment写一篇文章,我使用了下面的代码,但当我试图发表文章时,我发现授权错误,我使用的是CodeIgniter <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); include_once 'base_controller.php'; class Gp_auth extends Base_controller { private $
<?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;
}
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) {
_print($e);
}
} else {
$this->_gp_client->addScope(array(
'https://www.googleapis.com/auth/plus.login',
'https://www.googleapis.com/auth/plus.moments.write'
));
$this->_gp_client->setRequestVisibleActions('http://schemas.google.com/AddActivity');
try {
echo anchor($this->_gp_client->createAuthUrl(), 'Conect Me');
} catch (Google_Auth_Exception $e) {
_print($e);
}
}
}
public function me() {
try {
$this->_gp_client->setAccessToken($this->session->userdata('access_token'));
$response = $this->googleplus->plus->people->get('me');
_print($response->id);
$post_data = array(
'gp_id' => $response->id,
'gp_access_token' => $this->session->userdata('access_token'),
'post_body' => 'Hello Google moment',
'post_attachment' => ''
);
$this->load->library('sns_post');
echo $this->sns_post->gp_post($post_data);
} catch (Google_Auth_Exception $e) {
_print($e);
}
}
我认为您得到授权错误是因为以下原因-
api&Auth->同意屏幕
$this->\u CI->googleplus->item\u scope->setId($post\u data['gp\u id')代码>。要调用它,您必须使用这个类新的Google_服务\u Plus_ItemScope()代码>
$this->\u CI->googleplus->moment\u body->setTarget($this->\u CI->googleplus->item\u scope)
。要调用此方法,必须使用此类新的Google_服务\u Plus_Moment()
李>
- 去
- 创建项目
- 要获得Google+API访问权限,请访问:API&Auth->API->enable Google+API
- 要获取客户端id和客户端机密请转到:API&Auth->Credentials->创建新的客户端id
- 要获取api密钥,请转到:api&Auth->Credentials->Create New key->Browser key
- 要获取应用程序名称,请转到:API&Auth->同意屏幕
- 设置电子邮件地址
- 产品名称
结论:您正在使用最新的google api php客户端。并尝试以旧的方式调用方法。因此它不起作用。我也面临同样的问题。您得到的具体错误是什么?您使用的库是什么?你能把链接发布到它吗?我正在使用库,稍微修改一下代码。你能发布错误吗?这将为理解问题提供更多的可视性。googleplusapi CodeIgniter的github存储库使用了不推荐的方法,但现在不起作用,我已经测试了这些方法。我也遵循你之前给出的第二个链接,它不起作用,我使用的是你在我的评论中看到的同一个库before@KishorDgupta我已经改变了答案。还可以为您设置git repo。检查此链接是否可以发布错误堆栈跟踪?您是否可以提供google配置?尝试配置/交叉验证第5点中提到的google配置。您可以从git自述文件获取配置参考。我确信您在某个地方缺少google配置。
public function gp_post($post_data) {
$this->_CI->load->library('googleplus');
_print($post_data['gp_access_token']);
$this->_CI->googleplus->client->setAccessToken($post_data['gp_access_token']);
$this->_CI->googleplus->client->setRequestVisibleActions('http://schemas.google.com/AddActivity');
$this->_CI->googleplus->item_scope->setId($post_data['gp_id']);
$this->_CI->googleplus->item_scope->setType("http://schema.google.com/AddAction");
$this->_CI->googleplus->item_scope->setName("The Google+ Platform");
$this->_CI->googleplus->item_scope->setDescription($post_data['post_body']);
if (!empty($post_data['post_attachment'])) {
$this->_CI->googleplus->item_scope->setImage($post_data['post_attachment']);
}
$this->_CI->googleplus->moment_body->setTarget($this->_CI->googleplus->item_scope);
// Execute the request
try {
$momentResult = $this->_CI->googleplus->plus->moments->insert('me', 'vault', $this->_CI->googleplus->moment_body);
_print($momentResult);
} catch (Google_Auth_Exception $e) {
_print($e);
} catch (Google_Service_Exception $servic_exception) {
_print($servic_exception);
}
if ($response->meta->status == 201) {
return $response->response->id;
} else {
return false;
}