简体   繁体   English

将GMAIL API与Symfony4结合使用

[英]Using GMAIL API with Symfony4

I am trying to use Gmail API with my symfony4 website. 我想在我的symfony4网站上使用Gmail API The goal is to execute a command regularly through a cron, that will fetch all unread email, get the content, and process some things, then mark them read. 目标是通过cron定期执行命令,该命令将提取所有未读的电子邮件,获取内容并处理某些内容,然后将其标记为已读。

I currently have (whet though the official documentation ): 我目前有(虽然官方文档 ):

  • Installed the library through composer 通过composer安装了库
  • One new gmail account created for this, enabled the gmail API, and downloaded my "credentials.json" 为此,创建了一个新的Gmail帐户,启用了gmail API,并下载了我的“ credentials.json”
  • The working command from symfony (app:upload-from-unread-emails), with this code: 来自symfony的工作命令(app:upload-from-unread-emails),具有以下代码:

     $client = new \\Google_Client(); $client->setApplicationName('Gmail API PHP Quickstart'); $client->addScope(\\Google_Service_Gmail::GMAIL_READONLY); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); $gmail = new \\Google_Service_Gmail($client); $list = $gmail->users_messages->listUsersMessages('me', ['maxResults' => 1000]); while ($list->getMessages() != null) { foreach ($list->getMessages() as $mlist) { $message_id = $mlist->id; var_dump($message_id); } if ($list->getNextPageToken() != null) { $pageToken = $list->getNextPageToken(); $list = $gmail->users_messages->listUsersMessages('me', ['pageToken' => $pageToken, 'maxResults' => 1000]); } else { break; } } 

Am I on the right track to authenticate ? 我是否在进行身份验证的正确道路上? Where am I supposed to drop my credential.json in symfony ? 我应该在哪里将credential.json放在symfony中?

Where am I supposed to drop my credential.json in symfony? 我应该在哪里将credential.json放在symfony中?

I think that the best place for this file will be the config folder. 我认为此文件的最佳位置将是config文件夹。 But you can set any path (except perhaps not in public =)). 但是您可以设置任何路径(除非可能不在public =中)。

Let's say the path for this file be config/gmail/credentials.json . 假设此文件的路径为config/gmail/credentials.json So next step is get file path in command. 因此,下一步是在命令中获取文件路径。 For this you need get kernel.project_dir parameter from service container and concatenate kernel.project_dir and /config/gmail/credentials.json 为此,您需要从服务容器获取kernel.project_dir参数,并将kernel.project_dir/config/gmail/credentials.json连接在一起

For use service container in command you need: 要在命令中使用服务容器,您需要:

  • implement ContainerAwareInterface 实现ContainerAwareInterface
  • use ContainerAwareTrait (for $container property and setContainer method) 使用ContainerAwareTrait (用于$container属性和setContainer方法)
  • type-hint ContainerInterface $container in command contructor, call parent::__construct and setContainer($container) 在命令构造ContainerInterface $container中键入提示ContainerInterface $container ,调用parent::__constructsetContainer($container)

- -

// ...
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

class UploadFromUnreadEmailsCommand extends Command implements ContainerAwareInterface
{
    use ContainerAwareTrait;

    public function __construct(ContainerInterface $container)
    {
        parent::__construct();
        $this->setContainer($container);
    }

    // ...

}

Also documentation that you links has example with getClient function. 此外,您链接的文档还包含带有getClient函数的示例。 Just edit it with some changes: 只需进行一些更改即可对其进行编辑:

private function getClient()
{
    $projectDir = $this->container->getParameter('kernel.project_dir');
    $credentialsFilePath = sprintf('%s/config/gmail/credentials.json', $projectDir);
    $tokenFilePath = sprintf('%s/config/gmail/token.json', $projectDir);

    // ...
}

and in execute method: execute方法中:

$client = $this->getClient();
$gmail = new \Google_Service_Gmail($client);

Execute command and follow instructions (generate token). 执行命令并遵循说明(生成令牌)。

Full command class: 完整的命令类:

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

class UploadFromUnreadEmailsCommand extends Command implements ContainerAwareInterface
{

    use ContainerAwareTrait;

    protected static $defaultName = 'app:upload-from-unread-emails';

    /**
    * UploadFromUnreadEmailsCommand constructor.
    * @param $container
    */
    public function __construct(ContainerInterface $container)
    {
        parent::__construct();
        $this->setContainer($container);
    }

    protected function configure()
    {
        $this
            ->setDescription('Add a short description for your command')
        ;
    }

    private function getClient()
    {
        $projectDir = $this->container->getParameter('kernel.project_dir');
        $credentialsFilePath = sprintf('%s/config/gmail/credentials.json', $projectDir);
        $tokenFilePath = sprintf('%s/config/gmail/token.json', $projectDir);

        $client = new \Google_Client();
        $client->setApplicationName('Gmail API PHP Quickstart');
        $client->setScopes(\Google_Service_Gmail::GMAIL_READONLY);
        $client->setAuthConfig($credentialsFilePath);
        $client->setAccessType('offline');
        $client->setPrompt('select_account consent');

        // Load previously authorized token from a file, if it exists.
        if (file_exists($tokenFilePath)) {
            $accessToken = json_decode(file_get_contents($tokenFilePath), true);
            $client->setAccessToken($accessToken);
        }

        // If there is no previous token or it's expired.
        if ($client->isAccessTokenExpired()) {
            // Refresh the token if possible, else fetch a new one.
            if ($client->getRefreshToken()) {
                $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
            } else {
                // Request authorization from the user.
                $authUrl = $client->createAuthUrl();
                printf("Open the following link in your browser:\n%s\n", $authUrl);
                print 'Enter verification code: ';
                $authCode = trim(fgets(STDIN));

                // Exchange authorization code for an access token.
                $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
                $client->setAccessToken($accessToken);

                // Check to see if there was an error.
                if (array_key_exists('error', $accessToken)) {
                    throw new Exception(join(', ', $accessToken));
                }
            }
            // Save the token to a file.
            if (!file_exists(dirname($tokenFilePath))) {
                mkdir(dirname($tokenFilePath), 0700, true);
            }
            file_put_contents($tokenFilePath, json_encode($client->getAccessToken()));
        }
        return $client;
    }


    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);

        $client = $this->getClient();

        $gmail = new \Google_Service_Gmail($client);
        $list = $gmail->users_messages->listUsersMessages('me', ['maxResults' => 10]);

        while ($list->getMessages() != null) {
            foreach ($list->getMessages() as $mlist) {

                $message_id = $mlist->id;
                $io->text($message_id);

            }

            if ($list->getNextPageToken() != null) {
                $pageToken = $list->getNextPageToken();
                $list = $gmail->users_messages->listUsersMessages('me', ['pageToken' => $pageToken, 'maxResults' => 1000]);
            } else {
                break;
            }
        }

        $io->success('Success');
    }
}

Also I think better solution is move getClient function to a service, where get container parameter, init \\Google_Service_Gmail and type-hint GmailService in command constructor: 另外,我认为更好的解决方案是将getClient函数移至服务,在命令构造函数中获取容器参数,init \\Google_Service_Gmail和类型提示GmailService:

# /src/Command/UploadFromUnreadEmailsCommand.php
private $gmailService;
public function __construct(GmailService $gmailService)
{
    $this->gmailService = $gmailService;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM