简体   繁体   中英

Symfony2 Google API, How to use Google client

I am using Symfony2.3 and I want to access Google Calendar API, here what i did 1-I installed HIWO Bundle and FOSUser Bundle
2-Integrated both bundles and now i have user authenticated and inserted into database with access token 3-I have installed Google API library and auto-loaded it 4-created a Service wrapper class to access

Problem 1 : Seems now im using Oauth2 found in HIWO Bundle while logging in and I will be using Oauth2 in Google API library while making request, which dosent make any sense and not sure what should be done in this matter

Trials: -I found out that token provided by HIW Oauth is not the same as the one in code parameter in URL while redirecting back -Tried to set token manually and try to intiat simulate Google client request $cal = new \\Google_Calendar($this->googleClient) as below but

    $this->googleClient->authenticate('4/PmsUDPCbxWgL1X_akVYAhvnVWqpn.ErqFdB3R6wMTOl05ti8ZT3Zpgre8fgI');
    return $cal->calendarList->listCalendarList();`

Error received:

Error fetching OAuth2 access token, message: 'redirect_uri_mismatch'

and i made sure i have redirect_uri matched

My Service code is as below :

<?php

namespace Clinic\MainBundle\Services;

use Clinic\MainBundle\Entity\Patient;
use Doctrine\Common\Persistence\ObjectManager;

/*
 * @author: Ahmed Samy
 */

class GoogleInterfaceService {
    /*
     * Entity manager
     */

    protected $em;
    /*
     * instance of Symfphony session
     */
    protected $session;
    /*
     * Service container
     */
    protected $container;

    /*
     * Google client instance
     */
    protected $googleClient;

    public function __construct(ObjectManager $em, $container) {
        $this->em = $em;
        $this->container = $container;
        $this->googleClient = new \Google_Client();

        $this->googleClient->setClientId('xxxxxxxx.apps.googleusercontent.com');
        $this->googleClient->setClientSecret('uNnaK1o-sGH_pa6Je2jfahpz');
        $this->googleClient->setRedirectUri('http://hacdc.com/app_dev.php/login/check-google');
        $this->googleClient->setDeveloperKey('xxxxxxxxxxxxxxxxxxxx');
        $this->googleClient->setApplicationName("Google Calendar PHP Starter Application");
    }

    public function getCalendar() {

        $cal = new \Google_Calendar($this->googleClient);

        //setting token manually 
        $this->googleClient->authenticate('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
        return $cal->calendarList->listCalendarList();
    }

}

and when i dump $this->googleClient i get

protected 'scopes' => 
    array (size=0)
      empty
  protected 'useObjects' => boolean false
  protected 'services' => 
    array (size=0)
      empty
  private 'authenticated' => boolean false

The HWIOAuthBundle's token is missing the created array segment, but you could force that in there, and then just feed that token to the client like so:

    $googleAccessToken = $this->get('security.context')->getToken()->getRawToken();
    $googleAccessToken['created'] = time(); // This is obviously wrong... but you get the poing

    $this->google_client->setAccessToken(json_encode($googleAccessToken));

    $activities = $this->google_plusservice->activities->listActivities('me', 'public');

    var_dump($activities);die();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM