【问题标题】:Google API Client and CronjobGoogle API 客户端和 Cronjob
【发布时间】:2016-02-26 15:08:43
【问题描述】:

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

使用Google API Client的问题是需要允许用户先点击授权链接,并允许用户使用Google API Client。

我有一个类收件箱,其中包含一个初始化 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 并单击“单击此处”并允许它成功运行,但不能使用 Cronjob,因为我无法单击“单击此处” URL...

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

亲切的问候,
亚尼克

【问题讨论】:

    标签: php cron crontab google-api-php-client


    【解决方案1】:

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

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

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

    access_token 用于使用经过身份验证的用户的帐户访问 Google-API。如果您想在没有用户在场的情况下访问 google API(所谓的 offline-access),则应该将其存储在服务器上的某个位置。使用此令牌,您可以以用户的名义做任何事情(仅限于定义的范围)。它会在 1 小时左右后失效。在整个时间(1 小时)内,您可以在没有用户在场的情况下使用此令牌!

    access_token 在这段时间之后失效时,需要refresh_token。只有这样。你只会得到refresh_token ONCE,它永远不会改变。这是非常重要的数据,应该安全存储!

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

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

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

    愉快的编码

    资源

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多