我正在使用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