totalcrm / microsoft-graph-bundle
Symfony API MicrosoftGraph 的捆绑包
1.0.5
2022-04-20 07:44 UTC
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
安装
将 MicrosoftGraphBundle 添加到您的项目中
安装该捆绑包的推荐方式是通过 Composer。
$ composer require 'totalcrm/microsoft-graph-bundle'
Symfony 3:在 AppKernel.php 的 registerBundles() 中添加 MicrosoftGraphBundle
$bundles = [ ..., New TotalCRM\MicrosoftGraph\MicrosoftGraphBundle(), ];
Symfony 4 及以上版本:在 bundles.php 中添加 MicrosoftGraphBundle
return [ ..., TotalCRM\MicrosoftGraph\MicrosoftGraphBundle::class => ['all' => true], ];
配置
您必须配置您的 API。
Symfony 3:在 config.yml 中添加
Symfony 4 及以上版本:创建 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 ...
从 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 */ }
设置 Office 365 认证和缓存令牌
/** @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 获取联系人示例
$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@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);
获取 Outlook 日历中的事件示例
// 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);