PHP / GMail API



我一直在尝试使用PHP编码来访问我的Gmail,遵循以下文档:https://developers.google.com/gmail/api/quickstart/php 这很有效,直到今天晚上我开始重新开始。我现在收到以下错误:

致命错误:未捕获的逻辑异常:必须传入刷新令牌 或设置为 setAccessToken 的一部分 C:\Users\mcgranj\Dropbox\eBay_web\google\vendor\google\apiclient\src\Google\Client.php:258 堆栈跟踪:#0 C:\Users\mcgranj\Dropbox\eBay_web\google\quickstart.php(71(: Google_Client->fetchAccessTokenWithRefreshToken(NULL( #1 C:\Users\mcgranj\Dropbox\eBay_web\google\quickstart.php(118(: getClient(( #2 {main} throw in C:\Users\mcgranj\Dropbox\eBay_web\google\vendor\google\apiclient\src\Google\Client.php 在线 258

我整晚都在进行故障排除,遵循我能找到的每一个建议:

  • Google API 客户端"刷新令牌必须传入或设置为setAccessToken的一部分">
  • Google API PHP Refresh Token 返回 NULL
  • 谷歌 api refresh_token空以及如何刷新访问令牌

但是我仍然遇到这个问题,它使用的是快速入门PHP代码。我对此感到非常沮丧。欢迎任何指导和/或建议。

这是我的PHP代码:

<?php
require_once __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('America/Chicago');
ini_set('max_execution_time', 0); //indefinite
ini_set('memory_limit','256M'); //increase PHP memory
ini_set('display_errors', 10);
define('APPLICATION_NAME', 'Gmail API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/gmail-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
Google_Service_Gmail::GMAIL_READONLY)
));
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:n%sn", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %sn", $credentialsPath);
}
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$newAccessToken = $client->getAccessToken();
$accessToken = array_merge($accessToken, $newAccessToken);
file_put_contents($credentialsPath, json_encode($accessToken));
}
return $client;
}
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}
$client = getClient();
$service = new Google_Service_Gmail($client);
$user = 'me';
$results = $service->users_labels->listUsersLabels($user);
function listMessages($service, $user) {
$pageToken = NULL;
$messages = array();
$opt_param = array();
do {
try {
if ($pageToken) {
$opt_param['pageToken'] = $pageToken;
}
$opt_param['maxResults'] = 5; //Return only 5 messages
$opt_param['labelIds'] = 'INBOX';
$opt_param['q'] = "after:2017/07/08 FROM:shipment-tracking@amazon.com";
$messagesResponse = $service->users_messages->listUsersMessages($user, $opt_param);
if ($messagesResponse->getMessages()) {
$messages = array_merge($messages, $messagesResponse->getMessages());
$pageToken = $messagesResponse->getNextPageToken();
}
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
} while ($pageToken);
foreach ($messages as $message) {
print 'Message with ID: ' . $message->getId() . '<br/>';
$id = $message->getId();
echo "<pre>"; print_r($message); echo "</pre>";
$gmailurl = "https://www.googleapis.com/gmail/v1/users/".$user."/messages/".$id;
echo "<a href='$gmailurl' target='_blank'>".$gmailurl."</a><p>";
$messagePayload = $message->getPayload();
}
return $messages;
}
listMessages($service, $user);
?>

基于此线程,请确保在将身份验证结果写入 token.json 文件之前已调用json_encode。您可以通过添加如下json_encode来修复它:file_put_contents($credentialsPath, json_encode($accessToken));.此外,此页面建议添加$client->setAccessType('offline');并包含强制提示以返回刷新令牌:$client->setApprovalPrompt('force');

相关内容

  • 没有找到相关文章

最新更新