|
| 1 | + |
1 | 2 | # Microsoft Graph API
|
2 | 3 |
|
3 | 4 | A Laravel package for working with Microsoft Graph API.
|
@@ -87,24 +88,24 @@ return [
|
87 | 88 |
|
88 | 89 | 'msgraphLandingUri' => env('MSGRAPH_LANDING_URL'),
|
89 | 90 |
|
90 |
| - /* |
| 91 | + /* |
91 | 92 | set the authorize url
|
92 | 93 | */
|
93 | 94 |
|
94 | 95 | 'urlAuthorize' => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
|
95 | 96 |
|
96 |
| - /* |
| 97 | + /* |
97 | 98 | set the token url
|
98 | 99 | */
|
99 | 100 | 'urlAccessToken' => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
|
100 | 101 |
|
101 |
| - /* |
| 102 | + /* |
102 | 103 | set the scopes to be used, Microsoft Graph API will accept up to 20 scopes
|
103 | 104 | */
|
104 | 105 |
|
105 | 106 | 'scopes' => 'offline_access openid calendars.readwrite contacts.readwrite files.readwrite mail.readwrite mail.send tasks.readwrite mailboxsettings.readwrite user.readwrite',
|
106 | 107 |
|
107 |
| - /* |
| 108 | + /* |
108 | 109 | The default timezone is set to Europe/London this option allows you to set your prefered timetime
|
109 | 110 | */
|
110 | 111 | 'preferTimezone' => env('MSGRAPH_PREFER_TIMEZONE', 'outlook.timezone="Europe/London"'),
|
@@ -154,9 +155,9 @@ Route::group(['middleware' => ['web', 'auth']], function(){
|
154 | 155 | Or using a middleware route, if user does not have a graph token then automatically redirect to get authenticated
|
155 | 156 |
|
156 | 157 | ```
|
157 |
| -Route::group(['middleware' => ['web', 'MsGraphAuthenticated']], function(){ |
| 158 | +Route::group(['middleware' => ['web', 'MsGraphAuthenticated']], function(){ |
158 | 159 | Route::get('msgraph', function(){
|
159 |
| - return MsGraph::get('me'); |
| 160 | + return MsGraph::get('me'); |
160 | 161 | });
|
161 | 162 | });
|
162 | 163 |
|
@@ -196,7 +197,7 @@ Contacts
|
196 | 197 | * contacts($limit = 25, $offset = 50, $skip = 0)
|
197 | 198 |
|
198 | 199 | Emails
|
199 |
| -* emails($limit = 25, $skip = 0, $folderId = null) |
| 200 | +* emails($limit = 25, $skip = 0, $folderId = null) |
200 | 201 | * emailAttachments($email_id)
|
201 | 202 | * emailInlineAttachments($email)
|
202 | 203 | * emailSend($subject, $message, $to, $cc, $bcc, $attachments = null)
|
@@ -238,6 +239,289 @@ use DaveismynameLaravel\MsGraph\Models\MsGraphToken;
|
238 | 239 | Please see the [changelog](changelog.md) for more information on what has changed recently.
|
239 | 240 |
|
240 | 241 |
|
| 242 | +## Contributing |
| 243 | + |
| 244 | +Contributions are welcome and will be fully credited. |
| 245 | + |
| 246 | +Contributions are accepted via Pull Requests on [Github](https://github.com/daveismynamelaravel/msgrapth). |
| 247 | + |
| 248 | +## Pull Requests |
| 249 | + |
| 250 | +- **Document any change in behaviour** - Make sure the `readme.md` and any other relevant documentation are kept up-to-date. |
| 251 | + |
| 252 | +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. |
| 253 | + |
| 254 | +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. |
| 255 | + |
| 256 | +## Security |
| 257 | + |
| 258 | +If you discover any security related issues, please email [email protected] email instead of using the issue tracker. |
| 259 | + |
| 260 | +## License |
| 261 | + |
| 262 | +license. Please see the [license file](license.md) for more information. |
| 263 | +# Microsoft Graph API |
| 264 | + |
| 265 | +A Laravel package for working with Microsoft Graph API. |
| 266 | + |
| 267 | +## Installation |
| 268 | + |
| 269 | +To use Microsoft Grapth API an application needs creating at https://apps.dev.microsoft.com |
| 270 | + |
| 271 | +Create a new application, name the application. Click continue the Application Id will then be displayed. |
| 272 | + |
| 273 | +Next click Generate New Password under Application Secrets it won't be shown again so ensure you've copied it and added to .env more details further down. |
| 274 | + |
| 275 | +``` |
| 276 | +MSGRAPH_CLIENT_ID= |
| 277 | +MSGRAPH_SECRET_ID= |
| 278 | +``` |
| 279 | + |
| 280 | +Now click Add Platform under Platforms and select web. |
| 281 | + |
| 282 | +Enter you desired redirect url. This is the url you're application will use to connect to Graph API. |
| 283 | + |
| 284 | +Now under Microsoft Grpaph Permissions click add and select which permissions to use, a maximum of 20 can be selected. |
| 285 | + |
| 286 | +The other options are optional, click save at the bottom of the page to save your changes. |
| 287 | + |
| 288 | + |
| 289 | +Via Composer |
| 290 | + |
| 291 | +``` bash |
| 292 | +$ composer require daveismynamelaravel/msgraph |
| 293 | +``` |
| 294 | + |
| 295 | +In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file: |
| 296 | + |
| 297 | +``` |
| 298 | +'providers' => [ |
| 299 | + // ... |
| 300 | + DaveismynameLaravel\MsGraph\MsGraphServiceProvider::class, |
| 301 | +]; |
| 302 | +``` |
| 303 | + |
| 304 | +You can publish the migration with: |
| 305 | + |
| 306 | +``` |
| 307 | +php artisan vendor:publish --provider="DaveismynameLaravel\MsGraph\MsGraphServiceProvider" --tag="migrations" |
| 308 | +``` |
| 309 | + |
| 310 | +After the migration has been published you can create the tokens tables by running the migration: |
| 311 | + |
| 312 | +``` |
| 313 | +php artisan migrate |
| 314 | +``` |
| 315 | + |
| 316 | +You can publish the config file with: |
| 317 | + |
| 318 | +``` |
| 319 | +php artisan vendor:publish --provider="DaveismynameLaravel\MsGraph\MsGraphServiceProvider" --tag="config" |
| 320 | +``` |
| 321 | + |
| 322 | +When published, the config/msgraph.php config file contains: |
| 323 | + |
| 324 | +``` |
| 325 | +<?php |
| 326 | +
|
| 327 | +return [ |
| 328 | +
|
| 329 | + /* |
| 330 | + * the clientId is set from the Microsoft portal to identify the application |
| 331 | + * https://apps.dev.microsoft.com |
| 332 | + */ |
| 333 | + 'clientId' => env('MSGRAPH_CLIENT_ID'), |
| 334 | +
|
| 335 | + /* |
| 336 | + * set the application secret |
| 337 | + */ |
| 338 | +
|
| 339 | + 'clientSecret' => env('MSGRAPH_SECRET_ID'), |
| 340 | +
|
| 341 | + /* |
| 342 | + * Set the url to trigger the oauth process this url should call return MsGraph::connect(); |
| 343 | + */ |
| 344 | + 'redirectUri' => env('MSGRAPH_OAUTH_URL'), |
| 345 | +
|
| 346 | + /* |
| 347 | + * set the url to be redirected to once the token has been saved |
| 348 | + */ |
| 349 | +
|
| 350 | + 'msgraphLandingUri' => env('MSGRAPH_LANDING_URL'), |
| 351 | +
|
| 352 | + /* |
| 353 | + set the authorize url |
| 354 | + */ |
| 355 | +
|
| 356 | + 'urlAuthorize' => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize', |
| 357 | +
|
| 358 | + /* |
| 359 | + set the token url |
| 360 | + */ |
| 361 | + 'urlAccessToken' => 'https://login.microsoftonline.com/common/oauth2/v2.0/token', |
| 362 | +
|
| 363 | + /* |
| 364 | + set the scopes to be used, Microsoft Graph API will accept up to 20 scopes |
| 365 | + */ |
| 366 | +
|
| 367 | + 'scopes' => 'offline_access openid calendars.readwrite contacts.readwrite files.readwrite mail.readwrite mail.send tasks.readwrite mailboxsettings.readwrite user.readwrite', |
| 368 | +
|
| 369 | + /* |
| 370 | + The default timezone is set to Europe/London this option allows you to set your prefered timetime |
| 371 | + */ |
| 372 | + 'preferTimezone' => env('MSGRAPH_PREFER_TIMEZONE', 'outlook.timezone="Europe/London"'), |
| 373 | +]; |
| 374 | +``` |
| 375 | + |
| 376 | +Ensure you've set the following urls in your .env file: |
| 377 | + |
| 378 | +``` |
| 379 | +MSGRAPH_CLIENT_ID= |
| 380 | +MSGRAPH_SECRET_ID= |
| 381 | +MSGRAPH_OAUTH_URL= |
| 382 | +MSGRAPH_LANDING_URL= |
| 383 | +``` |
| 384 | + |
| 385 | +Optionally add |
| 386 | +``` |
| 387 | +MSGRAPH_PREFER_TIMEZONE= |
| 388 | +``` |
| 389 | + |
| 390 | + |
| 391 | +## Usage |
| 392 | + |
| 393 | +>Note this package expects a user to be logged in. |
| 394 | +
|
| 395 | +A routes example: |
| 396 | + |
| 397 | +``` |
| 398 | +
|
| 399 | +Route::group(['middleware' => ['web', 'auth']], function(){ |
| 400 | + Route::get('msgraph', function(){ |
| 401 | +
|
| 402 | + if (!is_string(MsGraph::getAccessToken())) { |
| 403 | + return redirect(env('MSGRAPH_OAUTH_URL')); |
| 404 | + } else { |
| 405 | + //display your details |
| 406 | + return MsGraph::get('me'); |
| 407 | + } |
| 408 | + }); |
| 409 | +
|
| 410 | + Route::get('msgraph/oauth', function(){ |
| 411 | + return MsGraph::connect(); |
| 412 | + }); |
| 413 | +}); |
| 414 | +``` |
| 415 | + |
| 416 | +Or using a middleware route, if user does not have a graph token then automatically redirect to get authenticated |
| 417 | + |
| 418 | +``` |
| 419 | +Route::group(['middleware' => ['web', 'MsGraphAuthenticated']], function(){ |
| 420 | + Route::get('msgraph', function(){ |
| 421 | + return MsGraph::get('me'); |
| 422 | + }); |
| 423 | +}); |
| 424 | +
|
| 425 | +Route::get('msgraph/oauth', function(){ |
| 426 | + return MsGraph::connect(); |
| 427 | +}); |
| 428 | +``` |
| 429 | + |
| 430 | +Once authenticated you can call MsGraph:: with the following verbs: |
| 431 | + |
| 432 | +``` |
| 433 | +MsGraph::get($endpoint, $array = [], $id = null) |
| 434 | +MsGraph::post($endpoint, $array = [], $id = null) |
| 435 | +MsGraph::put($endpoint, $array = [], $id = null) |
| 436 | +MsGraph::patch($endpoint, $array = [], $id = null) |
| 437 | +MsGraph::delete($endpoint, $array = [], $id = null) |
| 438 | +``` |
| 439 | + |
| 440 | +The second param of array is not always required, it's requirement is determined from the endpoint being called, see the API documentation for more details. |
| 441 | + |
| 442 | +The third param $id is optional when used the accesstoken will be attempted to be retrieved based on the id. When omited the logged in user will be used. |
| 443 | + |
| 444 | +These expect the API endpoints to be passed, the url https://graph.microsoft.com/beta/ is provided, only endpoints after this should be used ie: |
| 445 | + |
| 446 | +``` |
| 447 | +MsGraph::get('me/messages') |
| 448 | +``` |
| 449 | + |
| 450 | + |
| 451 | +API documenation can be found at https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/beta-overview |
| 452 | + |
| 453 | +To make things a little easier there is also trait classes provided: |
| 454 | + |
| 455 | +Each Trait class provides confinient methods that call the end points processes the data and returns json of the results. |
| 456 | + |
| 457 | +Contacts |
| 458 | +* contacts($limit = 25, $offset = 50, $skip = 0) |
| 459 | + |
| 460 | +Emails |
| 461 | +* emails($limit = 25, $skip = 0, $folderId = null) |
| 462 | +* emailAttachments($email_id) |
| 463 | +* emailInlineAttachments($email) |
| 464 | +* emailSend($subject, $message, $to, $cc, $bcc, $attachments = null) |
| 465 | +* emailSendReply($id, $message, $to, $cc, $bcc, $attachments = null) |
| 466 | +* emailSendForward($id, $message, $to, $cc, $bcc, $attachments = null) |
| 467 | + |
| 468 | +These can be called directly: |
| 469 | + |
| 470 | +List all emails (limited to 25): |
| 471 | + |
| 472 | +``` |
| 473 | +MsGraph::emails(); |
| 474 | +``` |
| 475 | + |
| 476 | +You can optionally override the details set the limit to be 50 emails: |
| 477 | + |
| 478 | +``` |
| 479 | +MsGraph::emails(50); |
| 480 | +
|
| 481 | +``` |
| 482 | +Calendars |
| 483 | +* calendars($limit = 25, $offset= 50, $skip = 0) |
| 484 | +* createCalendar($data) |
| 485 | +* getCalendar($calendarId) |
| 486 | +* updateCalendar($calendarId, $data) |
| 487 | +* deleteCalendar($calendarId, $data) |
| 488 | + |
| 489 | +Calendar Events |
| 490 | +* calendarEvents($limit = 25, $offset= 50, $skip = 0) |
| 491 | +* getEvent($eventId) |
| 492 | +* createEvent($data) |
| 493 | +* updateCalendarEvent($calendarId, $eventId, $data) |
| 494 | +* deleteCalendarEvent($calendarId, $eventId) |
| 495 | + |
| 496 | +Events |
| 497 | +* events($limit = 25, $offset= 50, $skip = 0) |
| 498 | +* getCalendarEvent($calendarId, $eventId) |
| 499 | +* createCalendarEvent($data) |
| 500 | +* updateEvent($eventId, $data) |
| 501 | +* deleteEvent($calendarId, $eventId) |
| 502 | + |
| 503 | + |
| 504 | +More trais will be added over the coming months. |
| 505 | + |
| 506 | +To restrict access to routes only to authenticated users there is a middleware route called 'MsGraphAuthenticated' |
| 507 | + |
| 508 | +Add MsGraphAuthenticated to routes to ensure the user is authenticated: |
| 509 | + |
| 510 | +``` |
| 511 | +Route::group(['middleware' => ['web', 'MsGraphAuthenticated'], function() |
| 512 | +``` |
| 513 | + |
| 514 | +Access token model, to access the model reference this ORM model |
| 515 | + |
| 516 | +``` |
| 517 | +use DaveismynameLaravel\MsGraph\Models\MsGraphToken; |
| 518 | +``` |
| 519 | + |
| 520 | +## Change log |
| 521 | + |
| 522 | +Please see the [changelog](changelog.md) for more information on what has changed recently. |
| 523 | + |
| 524 | + |
241 | 525 | ## Contributing
|
242 | 526 |
|
243 | 527 | Contributions are welcome and will be fully credited.
|
|
0 commit comments