baffo/googleapi

支持多个Google Client ID的Laravel 4.x版本的Google API v3包装器

1.0 2014-06-30 00:00 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:29:00 UTC


README

此包已被分叉,以便与illuminate 4.2.*一起使用,并添加对多个Google Client ID的支持,以便我们可以无缝地在多个工作环境之间切换

需要在您的/app/config/app.php文件中指定一个额外的配置变量

// app/config/app.php

'...',
'environment' => 'local', // local, prod, ...

Laravel 4的Google API v3包装器

此包允许通过API接口(v3)以Laravel风格管理Google服务

安装

将所需的包添加到您的composer.json文件中

{
    "require": {
    	...
    		"google/apiclient": "dev-master",
		"pongocms/googleapi": "dev-master"
	}
}

然后只需运行composer update

Laravel实现

此包包括一个ServiceProvider,它将提供对有用的GoogleAPI外观的访问。在您的/app/config/app.php中将GoogleapiServiceProvider引用设置如下

// app/config/app.php

'providers' => array(
    '...',
    'Pongo\GoogleAPI\GoogleapiServiceProvider'
);

导出并编辑配置文件

在开始使用此包之前,您需要从Google开发者激活一个配置文件并从Google开发者控制台获取您的个人代码,以便通过API调用访问和使用他们的服务。

一旦从Google开发者控制台获取了Web应用程序的Client IDClient Secret字符串并设置了有效的重定向URI回调,则导出包配置文件

php artisan config:publish pongocms/googleapi

...并将它们放入配置文件中的oauth2参数

// app/config/packages/pongocms/googleapi/config.php

return array(

    // OAuth2 Setting, you can get these keys in Google Developers Console
    'oauth2_client_id'      => '< YOUR CLIENT ID >',
    'oauth2_client_secret'  => '< YOUR CLIENT SECRET >',
    'oauth2_redirect_uri'   => 'https://:8000/',   // Change it according to your needs

    ...
  );

还要为您的应用程序中将使用的服务设置正确的作用域(记得在Google开发者控制台内激活相关API => APIS & AUTH => APIs)。有关任何帮助,请参阅Google API wiki。

使用GoogleAPI外观

一旦一切设置正确,您将获得对GoogleAPI外观的访问,以纯Laravel风格。

需要使用Google日历服务吗?

// routes.php

Route::get('/', function()
{
   if ( Input::has('code') )
   {
   	$code = Input::get('code');
   	
   	// authenticate with Google API
   	if ( GoogleAPI::authenticate($code) )
   	{
   		return Redirect::to('/protected');
   	}
   }
   
   // get auth url
   $url = GoogleAPI::authUrl();
   
   return link_to($url, 'Login with Google!');
});

Route::get('/logout', function()
{
   // perform a logout with redirect
   return GoogleAPI::logout('/');
});

Route::get('/protected', function()
{
   // Get the google service (related scope must be set)
   $service = GoogleAPI::getService('Calendar');
   
   // invoke API call
   $calendarList = $service->calendarList->listCalendarList();

   foreach ( $calendarList as $calendar )
   {
   	echo "{$calendar->summary} <br>";
   }

   return link_to('/logout', 'Logout');
});