Skip to content

Web API Token and Refresh Token #1595

Open
@thorby68

Description

@thorby68

Hi,
I am very new to MS Graph API, but have successfully developed connectivity with other product APIs such as Xero.

I have an open item TrackingID#2410040050002672 and was advised to post a question here.

Using the PHP SDK, I am struggling with the generating a token & refresh token, then storing both in the InMemoryAccessTokenCache for later use. It is my understanding that once this method has been established, I can call the InMemoryAccessTokenCache. If the token is still valid a valid token is returned, or, if the token is expired a refresh token returned.

My setup is complete and I can get a token using postman.

I have been advised to use the following code to establish the token & refresh token, and store them InMemoryAccessTokenCache. However, this fails as it seems Microsoft\Graph\Auth\OAuth2\OAuth2Client does not exist in the SDK !

use Microsoft\Graph\Graph;
use Microsoft\Graph\Http\GraphRequest;
use Microsoft\Graph\Core\GraphConstants;
use Microsoft\Graph\Auth\OAuth2\InMemoryAccessTokenCache;
use Microsoft\Graph\Auth\OAuth2\OAuth2Client;
	
require 'vendor/autoload.php';
$tenantId = 'MY TENANT ID';	
$clientId = 'MY CLIENT ID';
$clientSecret = 'MY SECRET';
$scopes = 'https://graph.microsoft.com/.default';
		
$oauthClient = new OAuth2Client($clientId, $clientSecret, $tenantId, $scopes);
$tokenCache = new InMemoryAccessTokenCache();
		
$accessToken = $oauthClient->getAccessToken($tokenCache);
		
$graph = new Graph();
$graph->setAccessToken($accessToken);
	
// Example request to get users		
$request = $graph->createRequest("GET", "/users")
		->setReturnType(\Microsoft\Graph\Model\User::class);
$users = $request->execute();
		
foreach ($users as $user) {
	echo "User: " . $user->getDisplayName() . "\n";
}

I would appreciate any help in configuring this (it's diving me mad!!).

It is also my understanding that once the token and refresh token has been established, I can use the following to establish/re-extablish the token, prior and API call to my designated endpoint ????

use Microsoft\Kiota\Authentication\Cache\InMemoryAccessTokenCache;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAccessTokenProvider;
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAuthenticationProvider;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;

require 'vendor/autoload.php';

$tokenRequestContext = new ClientCredentialContext(
	'MY TENANT ID',
	'MY CLIENT ID',
	'MY SECRET'
);
	
$graphServiceClient = new GraphServiceClient($tokenRequestContext);
	
$scopes = ['https://graph.microsoft.com/.default'];

$inMemoryCache = new InMemoryAccessTokenCache();

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

$accessToken = $inMemoryCache->getTokenWithContext($tokenRequestContext);

Honestly, it's day 4 of going in circles, please help?
Thank you

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions