admin管理员组文章数量:1301517
I am trying to access outlook mails with msgraph-sdk-php. I could get the access token and refresh token with http request, but when I use the tokens to InMemoryAccessTokenCache class I get error below:
InvalidArgumentException: Unable to initialize cache key for context using access token in file /var/www/html/...
The code I am testing is below:
$tokenRequestContext = new AuthorizationCodeContext(
"tenantId",
"clientId",
"clientSecret",
"authCode<placeholder>",
"redirectUrl"
);
$cache = new InMemoryAccessTokenCache(
$tokenRequestContext,
new AccessToken(
[
'access_token' => $token->access_token,
'refresh_token' => $token->refresh_token,
'expires' => 3600
]
)
);
$client = GraphServiceClient::createWithAuthenticationProvider(
GraphPhpLeagueAuthenticationProvider::createWithAccessTokenProvider(
GraphPhpLeagueAccessTokenProvider::createWithCache(
$cache,
$tokenRequestContext,
["offline_access", "user.read", "mail.read", "mail.send", "mail.readbasic", "mail.readwrite", "imap.accessasuser.all", "smtp.send"]
)
)
);
I would like to know how to use tokens correctly.
And also, when I make GraphServiceClient class with "authCode", I could request only once and after that I could get "Invaild grant" error. How can I request many times with the class?
I am trying to access outlook mails with msgraph-sdk-php. I could get the access token and refresh token with http request, but when I use the tokens to InMemoryAccessTokenCache class I get error below:
InvalidArgumentException: Unable to initialize cache key for context using access token in file /var/www/html/...
The code I am testing is below:
$tokenRequestContext = new AuthorizationCodeContext(
"tenantId",
"clientId",
"clientSecret",
"authCode<placeholder>",
"redirectUrl"
);
$cache = new InMemoryAccessTokenCache(
$tokenRequestContext,
new AccessToken(
[
'access_token' => $token->access_token,
'refresh_token' => $token->refresh_token,
'expires' => 3600
]
)
);
$client = GraphServiceClient::createWithAuthenticationProvider(
GraphPhpLeagueAuthenticationProvider::createWithAccessTokenProvider(
GraphPhpLeagueAccessTokenProvider::createWithCache(
$cache,
$tokenRequestContext,
["offline_access", "user.read", "mail.read", "mail.send", "mail.readbasic", "mail.readwrite", "imap.accessasuser.all", "smtp.send"]
)
)
);
I would like to know how to use tokens correctly.
And also, when I make GraphServiceClient class with "authCode", I could request only once and after that I could get "Invaild grant" error. How can I request many times with the class?
Share asked Feb 11 at 4:59 Jongkwan ParkJongkwan Park 886 bronze badges 2- 1 Note that, auth code can be used only once and you need to use new auth code for every access token generation. Alternatively, you can persist the refresh token and use it to get a new access token when the old one expires. – Sridevi Commented Feb 11 at 5:22
- Any update the issue? – Rukmini Commented Feb 19 at 11:48
1 Answer
Reset to default 0Note: The auth code can be used only once and if you try to reuse the auth code then you will get error.
- It is one-time use code used to exchange for an access token.
- Authorization codes expire after about 10 minutes.
Hence you will get an invalid grant error when you try to reuse the auth code:
For sample, I used below parameters:
Hence you need to generate auth-code again if you want to reuse it. And you can make use of refresh token to generate access token whenever the old access token expires.
- The Authorization Code is primarily used during the initial authorization process, so it’s not necessary to pass it every time once you have obtained the access and refresh tokens.
- After obtaining these tokens, you can use them to make requests for resources without needing to re-authenticate the user like below:
Generated access and refresh tokens:
https://login.microsoftonline/TenantID/oauth2/v2.0/token
client_id: ClientID
grant_type: authorization_code
scope: offline_access user.read mail.read mail.send mail.readbasic mail.readwrite imap.accessasuser.all smtp.send
redirect_uri: RedirectURL
code: code
client_secret: xxx
Now use the refresh token to generate new access token:
https://login.microsoftonline/TenantID/oauth2/v2.0/token
client_id: CLientID
grant_type: refresh_token
refresh_token: RefreshTokenFromAbove
client_secret: Secret
Sample code:
// Example of exchanging the refresh token for a new access token
$tokenUrl = "https://login.microsoftonline/{tenantId}/oauth2/v2.0/token";
$data = [
"client_id" => "clientId",
"client_secret" => "clientSecret",
"grant_type" => "refresh_token",
"refresh_token" => $token->refresh_token,
"scope" => "offline_access user.read mail.read mail.send"
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($data),
],
];
$context = stream_context_create($options);
$response = file_get_contents($tokenUrl, false, $context);
$tokenData = json_decode($response, true);
// The new token data should contain a new access_token and refresh_token
$newAccessToken = $tokenData['access_token'];
$newRefreshToken = $tokenData['refresh_token'];
To avoid the need for repeated re-authentication, you should properly cache the access token and refresh token and refresh the access token when it expires.
- Token Cache: Save the tokens either in memory or a persistent storage (like a database) to avoid re-authenticating with each request.
- Token Expiry: Before making any request, verify if the access token has expired and refresh it when necessary.
本文标签: laravelHow can I access Microsoftgraphapi with access token in PHPStack Overflow
版权声明:本文标题:laravel - How can I access Microsoft-graph-api with access token in PHP? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741678085a2392003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论