• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Happyr/GoogleApiBundle: A Symfony Wrapper for the Google APIs Client Library for ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Happyr/GoogleApiBundle

开源软件地址(OpenSource Url):

https://github.com/Happyr/GoogleApiBundle

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

Google API Bundle

A symfony2 bundle to communicate to Google API. This bundle is a Symfony2 wrapper for the google apiclient. There are some services not yet implemented. Please submit a PR and I'm happy to merge.

Installation

Step 1: Using Composer

Install it with Composer!

// composer.json
{
    // ...
    require: {
        // ...
        "happyr/google-api-bundle": "~2.1",
    }
}

Then, you can install the new dependencies by running Composer's update command from the directory where your composer.json file is located:

$ php composer.phar update

Step 2: Register the bundle

To register the bundles with your kernel:

<?php

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new HappyR\Google\ApiBundle\HappyRGoogleApiBundle(),
    // ...
);

Step 3: Configure the bundle

# app/config/config.yml
# you will get these parameters form https://code.google.com/apis/console/"
happy_r_google_api:
  application_name: MySite
  oauth2_client_id:
  oauth2_client_secret:
  oauth2_redirect_uri:
  developer_key:
  site_name: mysite.com

Basic Usage

Step 1: Create a controller

Create a controller with authenticate and redirect methods.

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;

class GoogleOAuthController extends Controller
{
  /**
   * @Route("/oauth/google/auth")
   */
  public function getAuthenticationCodeAction()
  {
  }

  /**
   * @Route("/oauth/google/redirect")
   */
  public function getAccessCodeRedirectAction(Request $request)
  {
  }
}

Step 2: Get the access code

Setup the required scope of your app and redirect the user to complete their part of the OAuth request.

// ...

  private $accessScope = [
    \Google_Service_Calendar::CALENDAR
  ];

  /**
   * @Route("/oauth/google/auth")
   */
  public function getAuthenticationCodeAction()
  {
    $client = $this->container->get('happyr.google.api.client');

    // Determine the level of access your application needs
    $client->getGoogleClient()->setScopes($this->accessScope);

    // Send the user to complete their part of the OAuth
    return $this->redirect($client->createAuthUrl());
  }

 // ...

Step 3: Handle the redirect

Determine if an access code has been returned. If there is an access code then exchange this for an access token by using the client authenticate method.

// ...

  private $accessScope = [
    \Google_Service_Calendar::CALENDAR
  ];

// ...

  /**
   * @Route("/oauth/google/redirect")
   */
  public function getAccessCodeRedirectAction(Request $request)
  {
    if($request->query->get('code'))
    {
      $code = $request->query->get('code');

      $client = $this->container->get('happyr.google.api.client');
      $client->getGoogleClient()->setScopes($this->accessScope);
      $client->authenticate($code);

      $accessToken = $client->getGoogleClient()->getAccessToken();

      // TODO - Store the token, etc...
    } else {
      $error = $request->query->get('error');
      // TODO - Handle the error
    }
  }

// ...

If successful the response should include access_token, expires_in, token_type, and created.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap