使用plusapi在google时刻发布时,PHP授权失败

时间:2014-09-04 11:41:27

标签: php codeigniter authorization google-api-php-client

我试图在php中发布google plus时刻的帖子,我使用下面的代码,但是当我试图发布我收到授权错误时,我正在使用codeignitter

<?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);
    }
}

}

索引功能用于身份验证 并且&#39;我&#39;用于发布时刻的功能

以及以下代码,用于从我的函数

调用的当前帖子的库代码
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;
    }

1 个答案:

答案 0 :(得分:2)

我认为您因为以下原因而获得授权错误 -

  1. 未设置电子邮件ID&amp;项目名称使用开发人员控制台链接APIs & Auth->Consent Screen
  2. 您正在调用已弃用的API $this->_CI->googleplus->item_scope->setId($post_data['gp_id']);。要调用此方法,您必须使用此课程new Google_Service_Plus_ItemScope();
  3. $this->_CI->googleplus->moment_body->setTarget($this->_CI->googleplus->item_scope);。要调用此方法,您必须使用此类new Google_Service_Plus_Moment();
  4. 我已经为你设置了git的演示代码。 Google-Plus-API-Codeigniter-Starter。您可以使用相同的代码开始。我也在git仓库中提供了详细的说明。
  5. 您必须使用Google控制台设置注册您的应用程序,如下所示:

    • 转到Google Developer Console
    • 创建项目
    • 要获取 Google+ API ,请访问:API&amp; Auth-&gt; API - &gt;启用Google+ API
    • 获取 client_id&amp; client_secret 转到:API&amp; Auth-&gt; Credentials-&gt;创建新的客户ID
    • 要获取 api_key ,请转到:API&amp; Auth-&gt; Credentials-&gt; Create New Key-&gt; Browser key
    • 要获取 application_name ,请转到:API&amp; Auth-&gt;同意屏幕
      1. 设置电子邮件地址
      2. 产品名称
  6. 注意:确保您已获得谷歌所需的所有访问权限,如 5

    中所述

    结论:您正在使用最新的google-api-php-client客户端。并尝试以较旧的方式调用方法。因此它不起作用。