Google API 客户端和 Cronjob

Google API Client and Cronjob

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

使用GoogleAPI客户端的问题是需要先让用户点击授权link,然后让用户使用Google API客户。

我有一个 class 收件箱,其中包含初始化 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));
}

你们知道如何使用刷新令牌或其他方式修复它吗?我无法让它工作,我没有想法。

如果我正在访问 URL 并单击 "Click Here" 并允许它成功运行但无法使用 Cronjob 因为我无法单击 "Click Here" URL...

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

亲切的问候,
亚尼克

这个答案更像是关于如何使用 O-Auth2 Flow 的一般方法,因为我刚才遇到过类似的问题。希望对您有所帮助。

一个可能的问题(理解 OAuth 的正确使用)是您使用 force 作为批准提示。你为什么要强制用户在他已经授予权限的情况下授予权限?

当用户针对您的后端进行身份验证时,系统会询问他是否要授予您的应用程序执行 scope 中定义的操作的权限。您的应用程序第一次获得此权限(通过用户单击 "Agree"-按钮)您的脚本将从 google.

获得 access_tokenrefresh_token

access_token 用于通过已验证用户的帐户访问 Google-API。如果您想访问 google APIs 而无需用户在场(所谓的 offline-access),您应该将其存储在服务器上的某个位置。使用此令牌,您可以以用户的名义执行任何操作(仅限于定义的范围)。 1小时左右后失效。在整个时间(1 小时)内,您可以在用户不在场的情况下使用此令牌!

access_token在这段时间后失效时,需要refresh_token。只有到那时。您只会得到 refresh_token ONCE 并且永远不会改变。这是非常重要的数据,应妥善保管!

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

下次新 access_token 失效时,您必须使用与以前相同的 refresh_token,这就是 refresh_token 如此重要的原因。

希望能对您有所帮助。如果没有,请对此发表评论。

编码愉快

资源