totalcrm / microsoft-graph-bundle
Symfony bundle for API MicrosoftGraph
Installs: 120
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.4.0
- guzzlehttp/guzzle: ^6.3 || ^7.0
- league/oauth2-client: ^2.2
- microsoft/microsoft-graph: 1.44.*
- symfony/asset: ^4.4 || ^5.3 || ^6.0
- symfony/cache: ^4.4 || ^5.3 || ^6.0
- symfony/config: ^4.4 || ^5.3 || ^6.0
- symfony/console: ^4.4 || ^5.3 || ^6.0
- symfony/dependency-injection: ^4.4 || ^5.3 || ^6.0
- symfony/framework-bundle: ^4.4 || ^5.3 || ^6.0
- symfony/routing: ^4.4 || ^5.3 || ^6.0
Requires (Dev)
- roave/security-advisories: dev-latest
README
Installation
Add MicrosoftGraphBundle to your project
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], ];
Configuration
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 ...
Get token from Office 365 | API Graph
/** @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 */ }
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 */ } }
Example get contacts in folder
/** @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);
Example get contact by id
$id = '...'; $contact = $contactManager->getContact($id); dump($contacts);
Create an contact
/** @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@gmail.com') ; /** @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);
Example get events from outlook calendar
// 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 an event
// 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);
Update an event
$id = '...'; $updateEvent = new Microsoft\Graph\Model\Event(); $updateEvent->setId($id); $updateEvent->setSubject("I Forgot The Eggs!"); $event = $calendarManager->updateEvent($updateEvent);
Delete an event
$id='...'; $response = $calendar->deleteEvent($id); dump($response->getStatus()==204 ? "Event deleted" : $response);