The recommended way to install the bundle is through Composer.
$ composer require 'totalcrm/microsoft-graph-bundle'
Symfony 3: add MicrosoftGraphBundle in AppKernel.php registerBundles()
$bundles = [
...,
New TotalCRM\MicrosoftGraph\MicrosoftGraphBundle(),
];
Symfony 4 and up: add MicrosoftGraphBundle in bundles.php
return [
...,
TotalCRM\MicrosoftGraph\MicrosoftGraphBundle::class => ['all' => true],
];
You have to configure your api.
Symfony 3: add to config.yml
Symfony 4 and up: create config/packages/microsoft_graph.yaml
microsoft_graph:
client_id: "%env(MS_GRAPH_CLIENT_ID)%"
client_secret: "%env(MS_GRAPH_CLIENT_SECRET)%"
tenant_id: "%env(MS_GRAPH_TENANT_ID)%"
contact_folder: "%env(MS_GRAPH_CONTACT_FOLDER)%"
redirect_uri: "app_dashboard"
homepage_route: "app_dashboard"
prefer_time_zone: "%env(MS_GRAPH_TIMEZONE_UTC)%"
version: "1.0"
scopes: # see more details https://developer.microsoft.com/en-us/graph/docs/authorization/permission_scopes
- openid
- offline_access
- Contacts.Read
- Contacts.ReadWrite
- Contacts.ReadWrite.Shared
- Calendars.Read
- Calendars.Read.Shared
- Calendars.ReadWrite
- Tasks.ReadWrite
...
/** @var MicrosoftGraphClient $graphClient */
$graphClient = $container->get('microsoft_graph.client');
try {
/* if you have a refresh token then the token will refresh */
$graphClient->getNewToken();
} catch(\Exception $ex) {
/* return url by Authorization */
$url = $graphClient->redirect();
/*
Follow the link $url and login in to Microsoft Office 365 Service
After successful authorization, you should be redirected to the redirect_uri page with the code parameter, which you need to save
See set token Office 365 auth and cached
*/
}
/** @var MicrosoftGraphClient $graphClient */
$graphClient = $container->get('microsoft_graph.client');
$authorizationCode = "0.AQUAIIWUa9rYQEKaSsxrxOyxTP-AiocQKThAr3_TKz.......";
try {
$this->graphClient->setAuthorizationCode($authorizationCode);
} catch (\Exception $exception) {
if ($exception->getMessage() === 'invalid_grant') {
/*
OAuth2 Authorization code was already redeemed
Please retry with a new valid code or use an existing refresh token
*/
} else {
/*
Authorization code save error
Please retry with a new valid code or use an existing refresh token
*/
}
}
/** @var MicrosoftGraphContactManager $contactManager */
$contactManager = $container->get('microsoft_graph.contact_manager');
//Get Contacts by Folder
/** @var Microsoft\Graph\Model\Contact[] $folders */
$folders = $contactManager->getContactFolders();
dump($contacts);
foreach ($folders as $folder) {
/** @var Microsoft\Graph\Model\Contact[] $contacts */
$contacts = $contactManager->getContacts($folder->getId());
dump($contacts);
}
//Get All Contacts
/** @var Microsoft\Graph\Model\Contact[] $contacts */
$contacts = $contactManager->getContacts();
dump($contacts);
$id = '...';
$contact = $contactManager->getContact($id);
dump($contacts);
/** @var Microsoft\Graph\Model\PhysicalAddress $businessAddress */
$businessAddress = new Model\PhysicalAddress();
$businessAddress
->setPostalCode('PostalCode')
->setCity('City')
->setState('State')
->setStreet('Street')
->setCountryOrRegion('Country')
;
/** @var Microsoft\Graph\Model\EmailAddress $emailAddress */
$emailAddress = new EmailAddress();
$emailAddress
->setName('DisplayName')
->setAddress('[email protected]')
;
/** @var Microsoft\Graph\Model\Contact $newContact */
$newContact = new Model\Contact();
$newContact
->setNickName('NickName')
->setDisplayName('DisplayName')
->setMiddleName('MiddleName')
->setGivenName('GivenName')
->setBusinessAddress($businessAddress)
->setEmailAddresses($emailAddress)
...
;
$contact = $contactManager->addContact($newContact);
dump($contact);
// Get calendar service
$calendarManager = $this->get('microsoft_graph.calendar');
//Get a collection of Microsoft\Graph\Model\Event
$startTime = new DateTime("first day of this month");
$endTime = new DateTime("first day of next month");
$events = $calendarManager->getEvents($startTime,$endTime);
//Get a Microsoft\Graph\Model\Event
$id='...';
$event = $calendarManager->getEvent($id);
// create Microsoft\Graph\Model\Event and set properties
$newEvent = new Microsoft\Graph\Model\Event();
$start = $calendar->getDateTimeTimeZone(new \DateTime('Now next minute'));
$end = $calendar->getDateTimeTimeZone(new \DateTime('Now next hour'));
$newEvent->setSubject('Controller Test Token');
$newEvent->setStart($start);
$newEvent->setEnd( $end);
$event= $calendarManager->addEvent( $newEvent);
dump($event);
$id = '...';
$updateEvent = new Microsoft\Graph\Model\Event();
$updateEvent->setId($id);
$updateEvent->setSubject("I Forgot The Eggs!");
$event = $calendarManager->updateEvent($updateEvent);
$id='...';
$response = $calendar->deleteEvent($id);
dump($response->getStatus()==204 ? "Event deleted" : $response);