在 Laravel 中为谷歌日历添加谷歌 php API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22863916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:18:15  来源:igfitidea点击:

Adding Google php API for google calendar in laravel

phplaravellaravel-4google-calendar-apigoogle-api-php-client

提问by SkeartReach

I want to use the Google php api for accessing the calendars. I'm working with laravel. I already added the package on composer and it's downloading fine, the thing is what I have to do with the providers and aliases or whatever to link the api with my app. I want to call the Calendar class. I do the auth properly to with another library, artdarek/oauth-4-laravel, i can show the calendar with this api but i can't insert/edit calendar, is this a simpler way ?

我想使用 Google php api 来访问日历。我正在使用 Laravel。我已经在 Composer 上添加了这个包并且它下载得很好,问题是我必须使用提供者和别名或任何将 api 与我的应用程序链接的东西。我想调用 Calendar 类。我使用另一个库 artdarek/oauth-4-laravel 正确进行身份验证,我可以用这个 api 显示日历,但我不能插入/编辑日历,这是一种更简单的方法吗?

Here the providers :

这里的提供者:

'providers' => array(

    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    'Illuminate\Cache\CacheServiceProvider',
    'Illuminate\Session\CommandsServiceProvider',
    'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider',
    'Illuminate\Routing\ControllerServiceProvider',
    'Illuminate\Cookie\CookieServiceProvider',
    'Illuminate\Database\DatabaseServiceProvider',
    'Illuminate\Encryption\EncryptionServiceProvider',
    'Illuminate\Filesystem\FilesystemServiceProvider',
    'Illuminate\Hashing\HashServiceProvider',
    'Illuminate\Html\HtmlServiceProvider',
    'Illuminate\Log\LogServiceProvider',
    'Illuminate\Mail\MailServiceProvider',
    'Illuminate\Database\MigrationServiceProvider',
    'Illuminate\Pagination\PaginationServiceProvider',
    'Illuminate\Queue\QueueServiceProvider',
    'Illuminate\Redis\RedisServiceProvider',
    'Illuminate\Remote\RemoteServiceProvider',
    'Illuminate\Auth\Reminders\ReminderServiceProvider',
    'Illuminate\Database\SeedServiceProvider',
    'Illuminate\Session\SessionServiceProvider',
    'Illuminate\Translation\TranslationServiceProvider',
    'Illuminate\Validation\ValidationServiceProvider',
    'Illuminate\View\ViewServiceProvider',
    'Illuminate\Workbench\WorkbenchServiceProvider',
    'Artdarek\OAuth\OAuthServiceProvider',
    'Google\Client',
),

Here's the aliases :

这是别名:

'aliases' => array(

    'App'             => 'Illuminate\Support\Facades\App',
    'Artisan'         => 'Illuminate\Support\Facades\Artisan',
    'Auth'            => 'Illuminate\Support\Facades\Auth',
    'Blade'           => 'Illuminate\Support\Facades\Blade',
    'Cache'           => 'Illuminate\Support\Facades\Cache',
    'ClassLoader'     => 'Illuminate\Support\ClassLoader',
    'Config'          => 'Illuminate\Support\Facades\Config',
    'Controller'      => 'Illuminate\Routing\Controller',
    'Cookie'          => 'Illuminate\Support\Facades\Cookie',
    'Crypt'           => 'Illuminate\Support\Facades\Crypt',
    'DB'              => 'Illuminate\Support\Facades\DB',
    'Eloquent'        => 'Illuminate\Database\Eloquent\Model',
    'Event'           => 'Illuminate\Support\Facades\Event',
    'File'            => 'Illuminate\Support\Facades\File',
    'Form'            => 'Illuminate\Support\Facades\Form',
    'Hash'            => 'Illuminate\Support\Facades\Hash',
    'HTML'            => 'Illuminate\Support\Facades\HTML',
    'Input'           => 'Illuminate\Support\Facades\Input',
    'Lang'            => 'Illuminate\Support\Facades\Lang',
    'Log'             => 'Illuminate\Support\Facades\Log',
    'Mail'            => 'Illuminate\Support\Facades\Mail',
    'Paginator'       => 'Illuminate\Support\Facades\Paginator',
    'Password'        => 'Illuminate\Support\Facades\Password',
    'Queue'           => 'Illuminate\Support\Facades\Queue',
    'Redirect'        => 'Illuminate\Support\Facades\Redirect',
    'Redis'           => 'Illuminate\Support\Facades\Redis',
    'Request'         => 'Illuminate\Support\Facades\Request',
    'Response'        => 'Illuminate\Support\Facades\Response',
    'Route'           => 'Illuminate\Support\Facades\Route',
    'Schema'          => 'Illuminate\Support\Facades\Schema',
    'Seeder'          => 'Illuminate\Database\Seeder',
    'Session'         => 'Illuminate\Support\Facades\Session',
    'SSH'             => 'Illuminate\Support\Facades\SSH',
    'Str'             => 'Illuminate\Support\Str',
    'URL'             => 'Illuminate\Support\Facades\URL',
    'Validator'       => 'Illuminate\Support\Facades\Validator',
    'View'            => 'Illuminate\Support\Facades\View',
    'OAuth'           => 'Artdarek\OAuth\Facade\OAuth',
    'Calendar'        => 'Google\Service\Calendar'
),

Composer.json

Composer.json

    "require": {
    "laravel/framework": "4.1.*",
    "artdarek/oauth-4-laravel": "dev-master",
    "google/apiclient": "dev-master"
},

The method for adding a calendar

添加日历的方法

public function addCalendar($calendarName){

    $calendar = new Calendar();
    $calendar->setSummary($calendarName);

    // get google service
    $googleService = OAuth::consumer( 'Google' );

    $createdCalendar = $googleService->calendars->insert($calendar);

    echo $createdCalendar->getId();
}

Hope you can help me ! Thanks !

希望你能帮我 !谢谢 !

采纳答案by Mihai Cr?i??

After you added the google library, in the require block of your composer.json file, this line:

添加 google 库后,在 composer.json 文件的 require 块中,这一行:

"google/apiclient": "dev-master"

run

composer update

作曲家更新

Now you can use the google client library in your controller

现在您可以在控制器中使用谷歌客户端库

$google_client = new Google_Client();
$google_client->setApplicationName('YOUR APPLICATION NAME');
$google_client->setClientId('YOUR CLIENT ID');
$google_client->setClientSecret('SECRET');

Of course you could store your client id and secret stored in a config file.

当然,您可以将客户端 ID 和机密存储在配置文件中。

You don't need to add the library path to the providers list.

您不需要将库路径添加到提供程序列表中。

回答by Chris Goosey

In its current form (as far as I am aware) this library is not designed for easy use in Laravel. It does not contain a Laravel Service Provider (that would go in your providers arrays) or a Facade class that would go in your alias array.

在目前的形式中(据我所知),这个库并不是为了在 Laravel 中方便使用而设计的。它不包含 Laravel 服务提供者(会出现在您的提供者数组中)或会出现在您的别名数组中的 Facade 类。

When I tried to use this API I went and got an unofficial package that contained namespaces, then whenever I wanted to use the code I would just call it via its namespace, e.g.

当我尝试使用这个 API 时,我得到了一个包含命名空间的非官方包,然后每当我想使用代码时,我都会通过它的命名空间调用它,例如

$client = new \Google\Client(); 

// Sets the class to use objects
$client->setUseObjects(true);
// Initiates Calendar Class injecting the $client class
$cal = new \Google\CalendarService($client);

if you were set on using the official library I believe you would need to include the files e.g.

如果您开始使用官方库,我相信您需要包含这些文件,例如

include('vendor/google/client...');

And following the github guide on using the package.

并遵循有关使用该软件包的 github 指南。

Hope this helps

希望这可以帮助