UnauthorizedException in reading emails with Oauth 2 using EWS - php

I am trying to load email list using oauth 2. Following is my code:
<?php
require_once "vendor/autoload.php";
use src\API\Type;
use garethp\ews\MailAPI as API;
$token='EwA......'; //access token
$api = API::withCallbackToken('outlook.office365.com', $token);
$mail = $api->getMailItems();
foreach ($mail as $email) {
$email = $api->getItem($email->getItemId());
$email_id=$email->getItemId();
$subject = $email->getSubject();
$sender = $email->getSender()->getMailbox()->getEmailAddress();
$body = (string) $email->getBody();
echo $body;
}
but getting response as follows:
Fatal error: Uncaught garethp\ews\API\Exception\UnauthorizedException
in
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php:438
Stack trace: #0
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php(356):
garethp\ews\API\ExchangeWebServices->handleNonSuccessfulResponses(NULL,
401) #1
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices\MiddlewareFactory.php(57):
garethp\ews\API\ExchangeWebServices->processResponse(NULL) #2
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php(497):
garethp\ews\API\ExchangeWebServices->garethp\ews\API\ExchangeWebServices{closure}(Object(garethp\ews\API\MiddlewareRequest),
Object(Closure)) #3
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices\MiddlewareFactory.php(66):
garethp\ews\API\ExchangeWebServices->garethp\ews\API{closure}(Object(garethp\ews\API\MiddlewareRequest)) #4 C:\inetpub\wwwroot\bephpforTt in C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php
on line 438

Related

Can't Send PHPMailer with Oauth Google

I have some problem.
My code
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$userEmail = "mail#mail.org";
$clientId = "xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
$clientSecret = "xxxxxxxx-xxxxxxxxx";
$token = "1/xxxxxxxx-xxxxxxxx-5EmWcSmuvnRbJs";
$mailer = new PHPMailerOAuth;
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->AuthType = 'XOAUTH2';
$mailer->oauthUserEmail = $userEmail;
$mailer->oauthClientId = $clientId;
$mailer->oauthClientSecret = $clientSecret;
$mailer->oauthRefreshToken = $token;
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;
$mailer->setFrom("xxxxx#xxxxxxx.org");
$mailer->addAddress("xxxxxx#gmail.com");
$mailer->Subject = "Subject";
$mailer->Body = "Message";
if (! $mailer->send())
throw new RuntimeException('Mail submission failed! ' . $mailer->ErrorInfo);
This eror
Fatal error: Uncaught Error: Class 'League\OAuth2\Client\Provider\Google' not found in /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php:54 Stack trace: #0 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php(67): PHPMailerOAuthGoogle->getProvider() #1 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php(74): PHPMailerOAuthGoogle->getToken() #2 /opt/lampp/htdocs/mailer/PHPMailer/class.smtp.php(470): PHPMailerOAuthGoogle->getOauth64() #3 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauth.php(174): SMTP->authenticate('', '', 'XOAUTH2', '', '', Object(PHPMailerOAuthGoogle)) #4 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1540): PHPMailerOAuth->smtpConnect(Array) #5 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1335): PHPMailer->smtpSend('Date: Thu, 3 Au...', 'Message\n') #6 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1213): PHPMailer->postSend() #7 /opt/lampp/htdocs/mailer/index.php(27): PHPMailer->send() #8 {main} thrown in /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php on line 54
i don't know what my mistake, i just want to create mail with PHPmailer using google smtp with Oauth2.0, I've searched on google but I have not found the solution yet
Look at PHPMailer's composer.json file - it includes the OAuth class as a suggestion, because it's not a requirement, and won't work on older PHP versions that PHPMailer supports, so it can't be enabled by default.
You need to add it to your own composer.json file (the same one you load PHPMailer with).
I've added comments to this effect to the Gmail OAuth2 tutorial on the PHPMailer wiki.
While I'm here, I suggest you don't implement this using PHPMailer 5.2, but go directly to PHPMailer 6.0 which has much better support for OAuth2.

Get users for all accounts with Google Analytics api

I am having problems getting a list of users for accounts
If I put the account number in the try me page (https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountUserLinks/list) it works
$client = new Google_Client();
$client->setAuthConfigFile($_SERVER['DOCUMENT_ROOT'] . '/client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_MANAGE_USERS);
$client=gaGetClient();
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$analytics = new Google_Service_Analytics($client);
//this doesn't work
$accountUserlinks=$analytics->management_accountUserLinks->listManagementAccountUserLinks('123456');
//this works
$man_accounts = $analytics->management_accounts->listManagementAccounts();
$accounts = [];
foreach ($man_accounts['items'] as $account) {
$accounts[] = [ 'id' => $account['id'], 'name' => $account['name'] ];
echo $account['name'] . " (" . $account['id'] . ")<br/>";
}
}
I get this error message
Fatal error: Uncaught exception 'Google_Service_Exception' with
message 'Error calling GET
https://www.googleapis.com/analytics/v3/management/accounts/123456/entityUserLinks:
(403) Insufficient Permission' in
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php:110
Stack trace: #0
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php(62):
Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request),
Object(Google_Client)) #1 [internal function]:
Google_Http_REST::doExecute(Object(Google_Client),
Object(Google_Http_Request)) #2
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Task/Runner.php(174):
call_user_func_array(Array, Array) #3
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php(46):
Google_Task_Runner->run() #4
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Goo
in
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php
on line 110
Can anyone think what I am doing wrong. I am thinking it is a scope issue, but I have stated my scopes
Thanks
Grant

Swift mailer not working in server but working in local machine

My code is:
require_once 'Swift-5.0.3/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp-mail.outlook.com', 587, "tls")
->setUsername('username#outlook.com')
->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Test')
->setFrom(array('demo#gmail.com' => 'Dpu'))
->setTo(array('demo2#gmail.com'))
->setBody('test body');
$result = $mailer->send($message);
if($result)
echo "success";
else
echo "Failed";
The code is working and mail is sent when I working in local machine. But in server I got the error and mail was not sent.
The error is:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "username#outlook.com" using 2 possible authenticators' in /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:184 Stack trace: #0 /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Transport/EsmtpTransport.php(312): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) #1 /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Transport/AbstractSmtpTransport.php(120): Swift_Transport_EsmtpTransport->_doHeloCommand() #2 /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /home/xxx/public_html/xxx/Mail/mail.php(19): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 184
Please help me...

Get Sharepoint data list in php

I tried to get SharePoint list in php. Below is code
require_once __DIR__ . '\Faker-master\src\autoload.php'; //Faker library (https://github.com/fzaninotto/Faker)
require_once 'lib/SPOClient.php';
$username = 'aaaa.aaa#microsoft.com';
$password = '*******';
$url = "https://microsoft.sharepoint.com";
generateContacts($url,$username,$password);
function generateContacts($url,$username,$password){
$client = new SPOClient($url);
$client->signIn($username,$password);
$list = $client->getList('ClientMaster');
$contactsCount = 120;
for($i = 0; $i < $contactsCount; $i++){
$contactEntry = createContactEntry();
$item = $list->addItem($contactEntry);
print "Contact '{$item->Title}' has been created succesfully.\r\n";
}
}
O/p:
Fatal error: Uncaught exception 'Exception' with message 'Invalid STS request. ' in E:\Apps\xampp\htdocs\ep\lib\SPOClient.php:260 Stack trace: #0 E:\Apps\xampp\htdocs\ep\lib\SPOClient.php(242): SPOClient->processToken('requestToken('ssss.kkkk...', '*****') #2 E:\Apps\xampp\htdocs\ep\listexamples.php(24): SPOClient->signIn('ssss.kkkk...', '******') #3 E:\Apps\xampp\htdocs\ep\listexamples.php(15): generateContacts('https://worldem...', 'sssss.kkk...', '******') #4 {main} thrown in E:\Apps\xampp\htdocs\ep\lib\SPOClient.php on line 260

Google Spreadsheets API with PHP – Fatal error: Uncaught exception

I'm trying to get started with the google SpreadSheet API. I know there are a bunch of other languages but PHP is the only one I'm vaguely competent in. I keep falling at the first hurdle and getting:
Fatal error: Uncaught exception 'Google\Spreadsheet\Exception' in
/Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/ServiceRequestFactory.php:48
Stack trace: #0 /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/SpreadsheetService.php(37):
Google\Spreadsheet\ServiceRequestFactory::getInstance()
#1 /Users/djave/Google Drive/Sites/practise/gdata/index.php(32): Google\Spreadsheet\SpreadsheetService->getSpreadsheets()
#2 {main} thrown in /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/ServiceRequestFactory.php on line 48
How this happens:
Step 1 Download and add the folder google-api-php-client
Next, fiddle with the code until it works
set_include_path('lib/');
require_once 'lib/Google/Client.php';
require_once 'lib/Google/Service/Books.php';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("-------------------------------------");
$service = new Google_Service_Books($client);
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
foreach ($results as $item) {
echo $item['volumeInfo']['title'], "<br /> \n";
}
Result: prints out a list of books
Step 2 Download and install php-google-spreadsheet-client
First off I copy the example exactly below what I have, then include all the right files until it can find everything:
set_include_path('lib/');
require_once 'lib/Google/Client.php';
require_once 'lib/Google/Service/Books.php';
require_once 'lib/Google/Spreadsheet/SpreadsheetService.php';
require_once 'lib/Google/Spreadsheet/ServiceRequestFactory.php';
require_once 'lib/Google/Spreadsheet/Exception.php';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("-------------------------------------");
$service = new Google_Service_Books($client);
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
foreach ($results as $item) {
echo $item['volumeInfo']['title'], "<br /> \n";
}
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
$worksheetFeed = $spreadsheet->getWorksheets();
But I just get
Fatal error: Uncaught exception 'Google\Spreadsheet\Exception' in /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/ServiceRequestFactory.php:48 Stack trace: #0 /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/SpreadsheetService.php(37): Google\Spreadsheet\ServiceRequestFactory::getInstance() #1 /Users/djave/Google Drive/Sites/practise/gdata/index.php(23): Google\Spreadsheet\SpreadsheetService->getSpreadsheets() #2 {main} thrown in /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/ServiceRequestFactory.php on line 48
I'm doing something really stupid, right? Thanks for any help
Looks like you've forgotten to bootstrap the lib first: https://github.com/asimlqt/php-google-spreadsheet-client#bootstrapping
require 'vendor/autoload.php';
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);
Give edit permission to "client_email" field value in client_secret.json file by opening the actual spreadsheet, refer image.

Categories