0

我正在使用 Symfony2.3 并且我想访问 Google Calendar API,在这里我做了什么 1-我安装了HIWO BundleFOSUser Bundle
2-集成了两个捆绑包,现在我已经通过访问令牌进行了用户身份验证并插入到数据库中 3-我有安装Google API 库并自动加载它 4-创建一个服务包装类来访问

问题 1: 现在似乎我在登录时使用 HIWO Bundle 中的 Oauth2,我将在发出请求时在 Google API 库中使用 Oauth2,这没有任何意义,并且不确定在这件事上应该做什么

试验: -我发现 HIW Oauth 提供的令牌与code重定向回 URL 中的参数中的令牌不同 -尝试手动设置令牌并尝试启动模拟 Google 客户端请求$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
4

1 回答 1

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();
于 2013-09-24T16:45:43.583 回答