我正在使用Google API客户端从Gmail中读取电子邮件。现在我想添加一个Cronjob,以便每5分钟阅读一次邮件。Google API客户端和Cronjob
使用Google API客户端的问题在于,它需要允许用户先单击授权链接并允许用户使用Google API客户端。
我有一个具有函数初始化类的收件箱,用于初始化Google API客户端。但是cronjob不起作用,因为我必须得到一个access_token。
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));
}
你们是否知道如何使用刷新令牌或任何解决它?我无法做到这一点,并且我没有想法。
如果我访问的网址,然后点击“点击这里”,并允许其它的作品成功地但不是一个cronjob,因为我不能点击“点击这里”网址...
希望你们了解它并可以帮助我:)。
亲切的问候,
Yanick