Php googleapi客户端和Cronjob

Php googleapi客户端和Cronjob,php,cron,crontab,google-api-php-client,Php,Cron,Crontab,Google Api Php Client,我正在使用谷歌API客户端阅读来自Gmail的电子邮件。现在我想添加一个Cronjob,让它每5分钟读取一次邮件 使用GoogleAPI客户端的问题是,它需要允许用户首先单击授权链接,并允许用户使用GoogleAPI客户端 我有一个类收件箱,其中有一个初始化GoogleAPI客户端的函数。但是cronjob不起作用,因为我必须获得一个访问令牌 public function initialize() { $configuration = Configuration::getConfigu

我正在使用谷歌API客户端阅读来自Gmail的电子邮件。现在我想添加一个Cronjob,让它每5分钟读取一次邮件

使用GoogleAPI客户端的问题是,它需要允许用户首先单击授权链接,并允许用户使用GoogleAPI客户端

我有一个类收件箱,其中有一个初始化GoogleAPI客户端的函数。但是cronjob不起作用,因为我必须获得一个访问令牌

public function initialize() {
    $configuration = Configuration::getConfiguration('class_Inbox');

    // Creates the Google Client
    $this->client = new Google_Client();
    $this->client->setApplicationName('Tiptsernetwork');
    $this->client->setClientId($configuration['clientId']);
    $this->client->setClientSecret($configuration['clientSecret']);
    $this->client->setRedirectUri('http://www.tipsternetwork.nl/cronjob/authenticate');
    $this->client->addScope('https://mail.google.com/');
    $this->client->setApprovalPrompt('force');
    $this->client->setAccessType('offline');

    // Creates the Google Gmail Service
    $this->service = new Google_Service_Gmail($this->client);

    // Authenticates the user.
    if (isset($_GET['code'])) {
        $this->authenticate($_GET['code']);
    }

    // Check if we have an access token in the session
    if (isset($_SESSION['access_token'])) {
        $this->client->setAccessToken($_SESSION['access_token']);
    } else {
        $loginUrl = $this->client->createAuthUrl();
        echo '<a href="'.$loginUrl.'">Click Here</a>';
    }

    // If the token is expired it used the refresh token to generate a new Access Token
    if($this->client->isAccessTokenExpired()) {
        $this->client->refreshToken($configuration['refreshToken']);
    }
}

public function authenticate($code) {
    // Creates and sets the Google Authorization Code
    $this->client->authenticate($code);
    $_SESSION['access_token'] = $this->client->getAccessToken();

    $this->client->refreshToken($configuration['refreshToken']);

    // Redirect the user back after authorizaton
    $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($url, FILTER_VALIDATE_URL));
}
公共函数初始化(){
$configuration=configuration::getConfiguration('class_收件箱');
//创建Google客户端
$this->client=new Google_client();
$this->client->setApplicationName('Tiptsernetwork');
$this->client->setClientId($configuration['clientId']);
$this->client->setClientSecret($configuration['clientSecret']);
$this->client->setRedirectUri('http://www.tipsternetwork.nl/cronjob/authenticate');
$this->client->addScope('https://mail.google.com/');
$this->client->setApprovalPrompt('force');
$this->client->setAccessType('offline');
//创建谷歌Gmail服务
$this->service=newgoogle\u service\u Gmail($this->client);
//验证用户的身份。
如果(isset($_GET['code'])){
$this->authenticate($_GET['code']);
}
//检查会话中是否有访问令牌
如果(isset($_会话['access_token'])){
$this->client->setAccessToken($_会话['access_token']);
}否则{
$loginUrl=$this->client->createAuthUrl();
回声';
}
//如果令牌过期,它将使用刷新令牌生成新的访问令牌
如果($this->client->isAccessTokenExpired()){
$this->client->refreshttoken($configuration['refreshttoken']);
}
}
公共函数身份验证($code){
//创建并设置Google授权码
$this->client->authenticate($code);
$\u会话['access\u token']=$this->client->getAccessToken();
$this->client->refreshttoken($configuration['refreshttoken']);
//授权后将用户重定向回
$url='http://'.$\u服务器['http\u主机].$\u服务器['PHP\u SELF'];
标题('Location:'.filter_var($url,filter_VALIDATE_url));
}
你们知道如何使用刷新令牌或其他什么来修复它吗?我不能让它工作,我也没有主意

如果我正在访问URL并单击“单击此处”并允许它成功运行,但不能与Cronjob一起运行,因为我无法单击“单击此处”URL

我希望你们能理解并帮助我:)

亲切的问候,

Yanick

这个答案是关于如何使用O-Auth2流的一个更一般的方法,因为我不久前遇到过类似的问题。我希望这会有所帮助

一个可能的问题(在理解OAuth的正确用法时)是使用
force
作为批准提示。当用户已经授予权限时,为什么还要强制用户授予权限

当用户针对您的后端进行身份验证时,会询问他是否希望为您的应用程序授予
范围中定义的操作的权限。当您的应用程序第一次获得此权限时(通过用户单击“同意”-按钮),您的脚本将从google获得
access\u token
refresh\u token

access\u令牌
用于使用该认证用户的帐户访问Google API。如果您想在不让用户在场的情况下访问google API(即所谓的脱机访问),那么应该将其存储在服务器上的某个位置。使用此令牌,您可以以用户的名义执行任何操作(仅限于定义的范围)。大约1小时后它就会失效。在整个时间(1小时)内,您可以使用此令牌,而无需用户在场

access\u令牌
在此时间段后无效时,需要
refresh\u令牌
。只有到那时。您只会获得
刷新\u令牌一次
,并且它永远不会更改。这是非常重要的数据,应安全存储

因此,如果您想在用户不在场的情况下访问google API,您必须使用存储的
access\u令牌进行API调用。如果响应类似于
token expired
(我认为这是一个错误代码-必须进行研究),则使用存储在安全位置的刷新令牌调用
$client->refreshToken($refreshToken)
。您将从中获得一个新的
access\u令牌。使用此
访问\u令牌
您可以进行进一步的工作,而无需用户(再次)单击某个位置

下次新的
access\u令牌
无效时,您必须使用与以前相同的
refresh\u令牌
,这就是该
refresh\u令牌
如此重要的原因

我希望我能帮你一点忙。如果没有,请对此发表评论

快乐编码

资源

  • (谷歌)
  • (堆栈溢出)