2013-06-19 104 views
0

我使用Symfony2.3,我想訪問谷歌日曆API,在這裏我做了什麼 1-I安裝HIWO BundleFOSUser Bundle
2,綜合這兩個包,現在我有用戶認證,並且與接入令牌 3-I已經安裝Google API library和自動加載它 4-創建的服務包裝器類來訪問插入到數據庫Symfony2的谷歌API,如何使用谷歌的客戶

問題1: 現在似乎即時通訊使用的OAuth2在HIWO捆綁發現登錄時,我將在Google API庫中使用Oauth2同時使請求,這dosent任何意義,不知道什麼應該在這個問題上做

試驗:在URL -I發現由HIW的Oauth提供的令牌不一樣的一個在code參數同時重定向回 -Tried手動設置令牌,並嘗試intiat模仿谷歌的客戶端請求$cal = new \Google_Calendar($this->googleClient)如下卻收到

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

錯誤:

錯誤獲取的OAuth2訪問令牌,消息: 'redirect_uri_mismatch'

,我確信我有redirect_uri匹配

我的服務代碼如下:

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

} 

當我傾倒$this->googleClient我得到

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

回答

0

HWIOAuthBundle的令牌是缺少created陣列段,但您可以在那裏強制執行該操作,然後僅將該令牌提供給客戶端,如下所示:

$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(); 
相關問題