-
Notifications
You must be signed in to change notification settings - Fork 442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem Using Gmail with XOAUTH2 - get_oauth_token.php does not result in refresh token... redirects to OJS homepage #7754
Comments
@mohsincheema, I see you've already posted this on our support forum: That's the best place to get help. |
@asmecher yes, I have posted the issue on forum first to get assistance. Now I tried a workaround as suggested by the PHPMailer support i.e.:
I was able to fetch the refresh token on localhost. But when I used these in config.inc.php on live website, I was still unable to send email. Error log recorded following errors:
and when PHPMailer support said:
I am trying to connect via google smtp for months now. the forum response is very slow which I understand as normal in volunteer support programs. So asked here. Anyhow thanks if anyone can help me where I am making a mistake. |
Describe the bug
We are trying to configure OJS for Gmail SMTP as we have acquired Google workspace services and want OJS to send email via google workspace instead of the local mail server. We configured the config.inc.php file as guided here: https://docs.pkp.sfu.ca/admin-guide/en/email#configuring-the-system-to-use-gmail-smtp . In config.inc.php our email settings are provided in additional information. However, email sending error happened, so we tried to configure Gmail with XOAUTH2 and followed the process as guided here: https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2 . However, when we run the get_oauth_token.php in browser and select the provider as google, then sign in (with same account from where we generated client ID), and then allow the permissions, it redirects us to the OJS homepage instead of showing the refresh token. We have tried to reach PHPMailer support and link to their ongoing response is: PHPMailer/PHPMailer#2646.
NOTE: In localhost simple configuration as recommended https://docs.pkp.sfu.ca/admin-guide/en/email#configuring-the-system-to-use-gmail-smtp worked and email sent via [email protected]. Similiarly, in localhost get_oauth_token.php resulted in displaying refresh token and not redirected to homepage as is the case in live website.
To Reproduce
Email settings in config.inc.php as recommended at https://docs.pkp.sfu.ca/admin-guide/en/email#configuring-the-system-to-use-gmail-smtp
For Using Gmail with XOAUTH2 steps followed as recommended at https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2 and at Fetch the token step:
What application are you using?
OJS, version 3.3.0.7.
Additional information
Email settings in config.inc.php:
`[email]
; Use SMTP for sending mail instead of mail()
smtp = On
; SMTP server settings
smtp_server = smtp.gmail.com
smtp_port = 465
; Enable SMTP authentication
; Supported smtp_auth: ssl, tls (see PHPMailer SMTPSecure)
smtp_auth = ssl
smtp_username = "my_email@my_domain_name.com"
smtp_password = "my_email_password"
;
; Supported smtp_authtype: RAM-MD5, LOGIN, PLAIN, XOAUTH2 (see PHPMailer AuthType)
; (Leave blank to try them in that order)
; smtp_authtype =
; The following are required for smtp_authtype = XOAUTH2 (e.g. GMail OAuth)
; (See https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2)
; smtp_oauth_provider = Google
; smtp_oauth_email =
; smtp_oauth_clientid =
; smtp_oauth_clientsecret =
; smtp_oauth_refreshtoken =
; Enable suppressing verification of SMTP certificate in PHPMailer
; Note: this is not recommended per PHPMailer documentation
; smtp_suppress_cert_check = On
`
The code in get_oauth_token.php:
`<?php
/**
*/
/**
*/
namespace PHPMailer\PHPMailer;
/**
composer install
*/
// @see https://github.com/thephpleague/oauth2-google
use League\OAuth2\Client\Provider\Google;
// @see https://packagist.org/packages/hayageek/oauth2-yahoo
use Hayageek\OAuth2\Client\Provider\Yahoo;
// @see https://github.com/stevenmaguire/oauth2-microsoft
use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
if (!isset($_GET['code']) && !isset($_GET['provider'])) {
Select Provider:?>
Google
Yahoo
Microsoft/Outlook/Hotmail/Live/Office365
//require 'vendor/autoload.php';
require '/home/username/public_html/subdomain/lib/pkp/lib/vendor/autoload.php';
session_start();
$providerName = '';
if (array_key_exists('provider', $_GET)) {
$providerName = $_GET['provider'];
$_SESSION['provider'] = $providerName;
} elseif (array_key_exists('provider', $_SESSION)) {
$providerName = $_SESSION['provider'];
}
if (!in_array($providerName, ['Google', 'Microsoft', 'Yahoo'])) {
exit('Only Google, Microsoft and Yahoo OAuth2 providers are currently supported in this script.');
}
//These details are obtained by setting up an app in the Google developer console,
//or whichever provider you're using.
$clientId = 'my_client_id.apps.googleusercontent.com';
$clientSecret = 'my_client_secret';
//If this automatic URL doesn't work, set it yourself manually to the URL of this script
//$redirectUri = (isset($_SERVER['HTTPS']) ? 'https://' : 'http:https://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$redirectUri = 'https://subdomain.my_domain_name.com/lib/pkp/lib/vendor/phpmailer/phpmailer/get_oauth_token.php';
$params = [
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $redirectUri,
'accessType' => 'offline'
];
$options = [];
$provider = null;
switch ($providerName) {
case 'Google':
$provider = new Google($params);
$options = [
'scope' => [
'https://mail.google.com/'
]
];
break;
case 'Yahoo':
$provider = new Yahoo($params);
break;
case 'Microsoft':
$provider = new Microsoft($params);
$options = [
'scope' => [
'wl.imap',
'wl.offline_access'
]
];
break;
}
if (null === $provider) {
exit('Provider missing');
}
if (!isset($_GET['code'])) {
// If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl($options);
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authUrl);
exit;
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
unset($_SESSION['provider']);
exit('Invalid state');
} else {
unset($_SESSION['provider']);
// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken(
'authorization_code',
[
'code' => $_GET['code']
]
);
// Use this to interact with an API on the users behalf
// Use this to get a new access token if the old one expires
echo 'Refresh Token: ', $token->getRefreshToken();
}`
The text was updated successfully, but these errors were encountered: