将 Gmail API 与 PHP 一起使用:如何使 CLI 应用程序在浏览器中运行?

Using Gmail API with PHP: How to make a CLI app work in the browser?

我正在测试 Gmail API。

到目前为止,我正在使用 PHP client example,并将我的环境选择为“桌面”,因为“其他”不可用,网站环境将无法工作。

它不会工作,因为当我使用浏览器访问 php 文件时,我得到:

在浏览器中打开以下link:https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id=35587452231052-k48bjsgefmnsbd654shdbf026q1un.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&state&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly&prompt=select_account%20consent输入验证码:

而且验证后没有地方实际输入验证码

当我从终端访问它时,我只需粘贴验证码就可以了。

这可能是个愚蠢的问题,但如何让它在页面中发挥作用?

这是我的文件:

<?php

require_once 'vendor/autoload.php';

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient()
{
    $client = new Google_Client();
    $client->setApplicationName('Gmail Access');
    $client->setScopes(Google_Service_Gmail::GMAIL_READONLY);
    $client->setAuthConfig(__DIR__ . 'credentials.json');
    $client->setAccessType('offline');
    $client->setPrompt('select_account consent');

    // Load previously authorized token from a file, if it exists.
    // The file token.json stores the user's access and refresh tokens, and is
    // created automatically when the authorization flow completes for the first
    // time.
    $tokenPath = 'token.json';
    if (file_exists($tokenPath)) {
        $accessToken = json_decode(file_get_contents($tokenPath), true);
        $client->setAccessToken($accessToken);
    }

    // If there is no previous token or it's expired.
    if ($client->isAccessTokenExpired()) {
        // Refresh the token if possible, else fetch a new one.
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code:';
            $authCode = trim(fgets(STDIN));

            // Exchange authorization code for an access token.
            $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
            $client->setAccessToken($accessToken);

            // Check to see if there was an error.
            if (array_key_exists('error', $accessToken)) {
                throw new Exception(join(', ', $accessToken));
            }
        }
        // Save the token to a file.
        if (!file_exists(dirname($tokenPath))) {
            mkdir(dirname($tokenPath), 0700, true);
        }
        file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    }
    return $client;
}


// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Gmail($client);
.........

我添加到文件中的所有内容都出现在 CLI 中,但我无法从那里继续并实际使其在浏览器中运行...

您应该遵循 OAuth Flow for Web applications。凭据对象有所不同,令牌将存储在会话中,这意味着您不必将其复制并粘贴到终端中。

示例代码:


参考文献: