将Gmail API与PHP结合使用:如何使CLI应用程序在浏览器中工作



我正在测试Gmail API。

到目前为止,我使用的是PHP客户端示例,并将我的环境选择为";"桌面";,因为";其他";不可用,s的网站环境将不起作用。

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

在浏览器中打开以下链接:https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id=35587452231052-k48bjsgefmnsbd654shdbf026q1un.apps.googleusercontent.com&redirect_uri=urn%3ietf%3wg%3oauth%3A2.0%3oob&状态&scope=https%3A%2F%2Fww.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%sn", $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中,但我无法从那里继续前进,并使其在浏览器中工作。。。

您应该遵循Web应用程序的OAuth流。凭据对象中存在差异,令牌将存储在会话中,这意味着您不必将其复制并粘贴到终端中。

示例代码:

  • https://github.com/googleapis/google-api-php-client/blob/master/examples/simple-file-upload.php

参考文献:

  • https://developers.google.com/identity/protocols/oauth2/web-server
  • https://github.com/googleapis/google-api-php-client/blob/master/README.md#authentication-带oauth

您正在用"命令行流"扭曲"Web流"。虽然存在一些重叠,但最终,它们使用不同的功能来接收授权代码。

显然,在web路径中使用fgets(STDIN)是行不通的。您需要遵循前面所述的示例:https://github.com/googleapis/google-api-php-client/blob/master/examples/simple-file-upload.php使用重定向并从$_GET全局获取代码。

相关内容

最新更新