You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using `MsGraph` within jobs, commands, or other queued processes, you need to authenticate the user explicitly. Ensure the user model has `access_token` and `refresh_token` stored in the database. Use the `login` method to authenticate the user before making any API calls:
6
+
7
+
```php
8
+
MsGraph::login(User::find(1));
9
+
MsGraph::get('me');
10
+
```
11
+
12
+
Here's an example of how to structure a job that uses `MsGraph`:
13
+
14
+
```php
15
+
<?php
16
+
17
+
namespace App\Jobs;
18
+
19
+
use App\Models\User;
20
+
use Dcblogdev\MsGraph\Facades\MsGraph;
21
+
use Illuminate\Bus\Queueable;
22
+
use Illuminate\Contracts\Queue\ShouldQueue;
23
+
use Illuminate\Foundation\Bus\Dispatchable;
24
+
use Illuminate\Queue\InteractsWithQueue;
25
+
use Illuminate\Queue\SerializesModels;
26
+
27
+
class ExampleMsGraphJob implements ShouldQueue
28
+
{
29
+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
30
+
31
+
protected $user;
32
+
33
+
public function __construct(User $user)
34
+
{
35
+
$this->user = $user;
36
+
}
37
+
38
+
public function handle(): void
39
+
{
40
+
MsGraph::login($this->user);
41
+
$userData = MsGraph::get('me');
42
+
// Process $userData as needed
43
+
}
44
+
}
45
+
```
46
+
47
+
Dispatch this job with a user instance:
48
+
49
+
```php
50
+
ExampleMsGraphJob::dispatch($user);
51
+
```
52
+
53
+
This approach ensures that the Microsoft Graph API calls are made with the correct user context, even in background processes.
0 commit comments