[英]PHP Google oauth2
I am using Xampp and I wanted to integrate sending Gmail over PHP. 我正在使用Xampp,并且希望集成通过PHP发送Gmail。 I see that everybody recommend PHP Mailer so I download Composer and install phpmailer composer require phpmailer/phpmailer
. 我看到每个人都推荐PHP Mailer,所以我下载了Composer并安装phpmailer composer require phpmailer/phpmailer
。 After that i install google oauth2: composer require league/oauth2-google
. 之后,我安装google oauth2: composer require league/oauth2-google
。
I look at this link to use : Authorization Code Flow. 我看这个链接使用:授权码流。
Google API is all set up (i got APP-id and Secret) Google API已全部设置(我有APP-id和Secret)
when i run get_oauth_token.php
i get error: Uncaught Error: Class 'League\\OAuth2\\Client\\Provider\\Google' not found in C:\\xampp\\htdocs\\telesales\\gentelella-master\\production\\PHPMailer\\get_oauth_token.php
当我运行get_oauth_token.php
时,出现错误: Uncaught Error: Class 'League\\OAuth2\\Client\\Provider\\Google' not found in C:\\xampp\\htdocs\\telesales\\gentelella-master\\production\\PHPMailer\\get_oauth_token.php
my composer.json is this: 我的composer.json是这样的:
{
"require": {
"phpmailer/phpmailer": "^6.0",
"league/oauth2-client": "^2.3",
"stevenmaguire/oauth2-microsoft": "^2.2"
}
}
Sorry, but I am new at this and cannot find why .php page wont find Class. 对不起,但是我对此并不陌生,无法找到为什么.php页面找不到Class。
Here is get_oauth_token.php 这是get_oauth_token.php
<?php
$provider = new League\OAuth2\Client\Provider\Google([
'clientId' => '{google-app-id}',
'clientSecret' => '{google-app-secret}',
'redirectUri' => 'https://example.com/callback-url',
'hostedDomain' => 'example.com', // optional; used to restrict access to users on your G Suite/Google Apps for Business accounts
]);
if (!empty($_GET['error'])) {
// Got an error, probably user denied access
exit('Got error: ' . htmlspecialchars($_GET['error'], ENT_QUOTES, 'UTF-8'));
} elseif (empty($_GET['code'])) {
// If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authUrl);
exit;
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
// State is invalid, possible CSRF attack in progress
unset($_SESSION['oauth2state']);
exit('Invalid state');
} else {
// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// Optional: Now you have a token you can look up a users profile data
try {
// We got an access token, let's now get the owner details
$ownerDetails = $provider->getResourceOwner($token);
// Use these details to create a new profile
printf('Hello %s!', $ownerDetails->getFirstName());
} catch (Exception $e) {
// Failed to get user details
exit('Something went wrong: ' . $e->getMessage());
}
// Use this to interact with an API on the users behalf
echo $token->getToken();
// Use this to get a new access token if the old one expires
echo $token->getRefreshToken();
// Number of seconds until the access token will expire, and need refreshing
echo $token->getExpires();
}
?>
I have just making the same but without composer. 我只是做同样的,但没有作曲家。 First it's not working with some errors. 首先,它没有某些错误。 While I am searching the solution for troubleshooting, and try to edit some lines, finally it's working now. 在搜索解决方案以进行故障排除并尝试编辑某些行时,终于可以使用了。 with this result : 结果如下:
2018-05-27 02:41:44 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:44 CLIENT -> SERVER: EHLO mybasic.local
2018-05-27 02:41:44 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.213.130.29]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-05-27 02:41:44 CLIENT -> SERVER: STARTTLS
2018-05-27 02:41:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2018-05-27 02:41:45 CLIENT -> SERVER: EHLO myweb.local
2018-05-27 02:41:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.213.130.29]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-05-27 02:41:45 CLIENT -> SERVER: AUTH LOGIN
2018-05-27 02:41:45 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2018-05-27 02:41:45 CLIENT -> SERVER: <credentials hidden>
2018-05-27 02:41:45 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2018-05-27 02:41:45 CLIENT -> SERVER: <credentials hidden>
2018-05-27 02:41:46 SERVER -> CLIENT: 235 2.7.0 Accepted
2018-05-27 02:41:46 CLIENT -> SERVER: MAIL FROM:<your_email@gmail.com>
2018-05-27 02:41:46 SERVER -> CLIENT: 250 2.1.0 OK q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:46 CLIENT -> SERVER: RCPT TO:<your_email_or_other@gmail.com>
2018-05-27 02:41:46 SERVER -> CLIENT: 250 2.1.5 OK q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:46 CLIENT -> SERVER: DATA
2018-05-27 02:41:46 SERVER -> CLIENT: 354 Go ahead q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:46 CLIENT -> SERVER: Date: Sun, 27 May 2018 04:41:44 +0200
2018-05-27 02:41:46 CLIENT -> SERVER: To: Test System <your_email@gmail.com>
2018-05-27 02:41:46 CLIENT -> SERVER: From: Test System <your_email@gmail.com>
2018-05-27 02:41:46 CLIENT -> SERVER: Subject: Here is the subject
2018-05-27 02:41:46 CLIENT -> SERVER: Message-ID: <ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk@mybasic.local>
2018-05-27 02:41:46 CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
2018-05-27 02:41:46 CLIENT -> SERVER: MIME-Version: 1.0
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Type: multipart/alternative;
2018-05-27 02:41:46 CLIENT -> SERVER: boundary="b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk"
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: This is a multi-part message in MIME format.
2018-05-27 02:41:46 CLIENT -> SERVER: --b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: This is the body in plain text for non-HTML mail clients
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: --b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: This is the HTML message body <b>in bold!</b>
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: --b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk--
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: .
2018-05-27 02:41:48 SERVER -> CLIENT: 250 2.0.0 OK 1527388939 q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:48 CLIENT -> SERVER: QUIT
2018-05-27 02:41:48 SERVER -> CLIENT: 221 2.0.0 closing connection q126-v6sm37491953pga.79 - gsmtp
Message has been sent
My code : 我的代码:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php'; // make sure the folder is right
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
//Load Composer's autoloader
//require 'vendor/autoload.php'; // if using composer
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your_email@gmail.com'; // SMTP username
$mail->Password = 'your_email_password';
$mail->SMTPSecure = 'tsl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('your_email@gmail.com', 'Test System');
$mail->addAddress('your_email_or_other@gmail.com', 'Test System');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
I also disabled/comment all lines related to email on php.ini in the folder xampp/php and reload the xampp. 我还禁用/注释了与xampp / php文件夹中php.ini上的电子邮件相关的所有行,并重新加载了xampp。
Get the latest code from : https://github.com/PHPMailer/PHPMailer 从以下位置获取最新代码: https : //github.com/PHPMailer/PHPMailer
Hope it will help. 希望它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.