【问题标题】:Symfony2 Google API, How to use Google clientSymfony2 Google API,如何使用 Google 客户端
【发布时间】:2013-06-19 21:57:03
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: php api rest symfony google-calendar-api


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多