msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

Using package in laravel

Open edikurniawan-dev opened this issue 7 months ago • 3 comments

Please help me, i'm confused I have project using laravel and laravel socialite, i got access_token from laravel socialite. How to use that access_token in this package?

I use this method

$tokenRequestContext = new OnBehalfOfContext(
  config("services.microsoft.tenant"),
  config("services.microsoft.client_id"),
  config("services.microsoft.client_secret"),
  session()->get("microsoft_access_token"),
);

$graphServiceClient = new GraphServiceClient($tokenRequestContext);
$user = $graphServiceClient->me()->get()->wait();
dd($user);

why the process is very long, and the result is invalid grant? I have checked that all parameters (tenant id, client id, etc) are valid

Image

edikurniawan-dev avatar May 21 '25 02:05 edikurniawan-dev

why this package doesn't implement it easily like microsoft graph postman collection, i can even input manual access token in postman header and call api directly

edikurniawan-dev avatar May 21 '25 04:05 edikurniawan-dev

with symfony i use this code if it can help you

    $tokenRequestContext = new ClientCredentialContext(
        $this->tenantId,
        $this->clientId,
        $this->clientSecret,
    );

    $accessToken = new AccessToken([
        'access_token' => $oauthUser->getAccessToken(),
        'refresh_token' => $oauthUser->getRefreshToken(),
    ]);

    $scopes = ['User.Read', 'Mail.ReadWrite', 'Mail.Send'];

    $inMemoryCache = new InMemoryAccessTokenCache($tokenRequestContext, $accessToken);

    $graphServiceClient = GraphServiceClient::createWithAuthenticationProvider(
        GraphPhpLeagueAuthenticationProvider::createWithAccessTokenProvider(
            GraphPhpLeagueAccessTokenProvider::createWithCache(
                $inMemoryCache,
                $tokenRequestContext,
                $scopes
            )
        )
    );

Spawnrad avatar May 28 '25 18:05 Spawnrad

thanks for the suggestion, i will try it later. i followed this solution and it worked 1472

edikurniawan-dev avatar May 30 '25 10:05 edikurniawan-dev