Google APIクライアントを使用してGmailからメールを読んでいます。今は5分ごとにメールを読むようにクロンジョブを追加したいと思っています。Google APIクライアントとCronjob
Google APIクライアントを使用する際の問題点は、ユーザーが認可リンクを最初にクリックし、Google APIクライアントを使用できるようにする必要があることです。
Google APIクライアントのinitliazesを初期化する関数を持つInboxクラスがあります。しかし、私はaccess_tokenを取得する必要があるので、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));
}
リフレッシュトークンなどを使用して修正する方法を知っていますか?私はそれを働かせることはできませんし、私はアイデアがありません。
私はURLにアクセスし、「ここをクリック」と私は「ここをクリック」URLをクリックすることができないので、それはcronジョブで正常ではなく、働くことを許可する...
をクリックしていた場合、私は願っていますあなたは人々がそれを理解し、私を助けることができます:)。
敬具、
Yanick