Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Authorization fail when post in google moment with plusapi

I was trying to made a post for google plus moment in php, i using below code, but when trying to post i am getting authorization error, I am doing with 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);
    }
}

}

index function was for authentication and 'me' function for for post moment

and below code for library code of moment post , which is called from me function

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;
    }
like image 746
Kishor datta gupta Avatar asked Sep 04 '14 11:09

Kishor datta gupta


1 Answers

I think you have get authorization error because of following reason -

  1. Not setting email id & project name using developer console link APIs & Auth->Consent Screen.
  2. You are calling deprecated API $this->_CI->googleplus->item_scope->setId($post_data['gp_id']);. To Call this you have to use this class new Google_Service_Plus_ItemScope();
  3. $this->_CI->googleplus->moment_body->setTarget($this->_CI->googleplus->item_scope); .To call this method you have to use this class new Google_Service_Plus_Moment();
  4. I have setup demo code in git for you. Google-Plus-API-Codeigniter-Starter. You can use same code to start with. I have provided detailed instruction in git repository too.
  5. You have to Register your application using Google Console Settings as below:

    • Go to Google Developer Console
    • Create Project
    • To Get Google+ API Access go to: APIs & Auth->APIs -> enable Google+ API
    • To Get client_id & client_secret go to: APIs & Auth->Credentials->Create new Client ID
    • To Get api_key go to: APIs & Auth->Credentials->Create New Key->Browser key
    • To Get application_name go to: APIs & Auth->Consent Screen
      1. Set Email Address
      2. Product Name

Note: Ensure you have get all the required access from google as mentioned in point 5

Conclustion: You are using Latest google-api-php-client client. And trying to call method in an older way. Hence its not working.

like image 91
Rajesh Ujade Avatar answered Oct 23 '22 17:10

Rajesh Ujade