Как сделать так, чтобы мое PHP-приложение никогда не запрашивало код подтверждения Gmail API?Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как сделать так, чтобы мое PHP-приложение никогда не запрашивало код подтверждения Gmail API?

Сообщение Anonymous »

Мне бы хотелось, чтобы приложение работало автоматически, но пока мы тестируем, если мы проведем выходные, не заморачиваясь с ним в понедельник, первое, что сделает приложение, это:

Откройте в браузере следующую ссылку:
https://accounts.google.com/o/oauth2/(...)
Введите код подтверждения:

Где-то в Интернете я однажды видел, что необходимо выделить жирным шрифтом параметры в приведенном ниже коде; но не работает должным образом.

Код: Выделить всё

use GuzzleHttp\Client;
require __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}

// @return Google_Client the authorized client object
function getClient()
{
$guzzleClient = new GuzzleHttp\Client([
'proxy' => ':
',
'verify' => false,
]);

$client = new Google_Client();
$client->setApplicationName('RS Gmail Check API PHP');
$client->setScopes(Google_Service_Gmail::GMAIL_READONLY);
$client->setAuthConfig('credentials.json');
**$client->setAccessType('offline');**
// Using "consent" ensures that your application always receives a refresh token.
// If you are not using offline access, you can omit this.
**$client->setApprovalPrompt("consent");
$client->setIncludeGrantedScopes(true);   // incremental auth**
//
$client->setPrompt('select_account consent');
$client->setHttpClient($guzzleClient);

$tokenPath = '/opt/gmail-check/token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
if (isset($_GET['code'])) {
$authCode = $_GET['code'];
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
header('Location: ' .  filter_var($this->redirectUri,
FILTER_SANITIZE_URL));

if(!file_exists(dirname($this->tokenFile))) {
mkdir(dirname($this->tokenFile), 0700, true);
}

file_put_contents($this->tokenFile, json_encode($accessToken));
}else{
exit('No code found');
}
}
$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 {
$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);

// Print the labels in the user's account.
$user = 'me';
$inboxMessage = [];

function decodeBody($body) {
$rawData = $body;
$sanitizedData = strtr($rawData,'-_', '+/');
$decodedMessage = base64_decode($sanitizedData);
if(!$decodedMessage){
$decodedMessage = FALSE;
}
return $decodedMessage;
}
$list = $service->users_messages->listUsersMessages($user, ['maxResults' => 10, 'q' => $search]);
try{
foreach ($list->getMessages() as $mlist) {

$message_id = $mlist->id;
$optParamsGet2['format'] = 'full';
$single_message = $service->users_messages->get('me', $message_id, $optParamsGet2);
$payload = $single_message->getPayload();
$headers = $single_message->getPayload()->getHeaders();
$snippet = $single_message->getSnippet();

foreach($headers as $single) {

if ($single->getName() == 'Subject') {

$message_subject = $single->getValue();

}
else if ($single->getName() == 'Date') {

$message_date = $single->getValue();
$message_date = date('M jS Y h:i A', strtotime($message_date));
}

else if ($single->getName() == 'From') {

$message_sender = $single->getValue();
$message_sender = str_replace('"', '', $message_sender);
}
}

$inboxMessage = [
'messageId' => $message_id,
'messageSnippet' => $snippet,
'messageSubject' => $message_subject,
'messageDate' => $message_date,
'messageSender' => $message_sender
];

// With no attachment, the payload might be directly in the body, encoded.
$body = $payload->getBody();
$FOUND_BODY = decodeBody($body['data']);

// If we didn't find a body, let's look for the parts
if(!$FOUND_BODY) {
$parts = $payload->getParts();
foreach ($parts  as $part) {
if($part['body']) {
$FOUND_BODY = decodeBody($part['body']->data);
}
// Last try: if we didn't find the body in the first parts,
// let's loop into the parts of the parts (as @Tholle suggested).
if($part['parts'] && !$FOUND_BODY) {

foreach ($part['parts'] as $p) {
// replace 'text/html' by 'text/plain' if you prefer
if($p['mimeType'] === 'text/html' &&  $p['body']) {
$FOUND_BODY = decodeBody($p['body']->data);
}
}
}
}
}
Обновление. Эти выходные я провел, тестируя другой проект, использующий API Gmail, но на основе того же кода, который я использую в сервисе. , и хотя я провел без запуска приложения более 5 часов, токен не запрашивался ни разу более одного раза. . Самое существенное отличие состоит в том, что в сервисе GuzzleClient настроена работа с прокси (которым будет наш брандмауэр pfsense). Возникла ли у меня проблема с истекающим сроком действия токена на работе из-за прокси-сервера?


Подробнее здесь: https://stackoverflow.com/questions/595 ... ation-code
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»