繁体   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