簡體   English   中英

Symfony2 Google API,如何使用Google客戶端

[英]Symfony2 Google API, How to use Google client

我正在使用Symfony2.3,並且想訪問Google Calendar API,在這里我做了什么1-我安裝了HIWO BundleFOSUser Bundle
2-集成了兩個包,現在我已經通過訪問令牌對用戶進行了身份驗證並插入到數據庫中3-我已經安裝了Google API庫並自動加載了它4創建了服務包裝類以進行訪問

問題1:現在登錄時似乎正在使用HIWO Bundle中的Oauth2,並且在發出請求時我將在Google API庫中使用Oauth2,這很有意義並且不確定在此問題上應該做什么

試驗:-I發現由HIW的Oauth提供的令牌是不一樣的一個在code中的URL參數,同時重定向回-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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM