如何使用Java的Microsoft Graph缓存令牌
我使用 Microsoft Graph SDK 来处理某些请求,但是每次执行 GET 请求时,它都会执行另一个请求来获取令牌。我尝试阅读有关此的文档,但在 Java 中找不到任何内容。
这是我的客户端的实现,
ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(clientId)
.clientSecret(clientSecret)
.tenantId(tenantId)
.httpClient(httpClient)
.build();
我还尝试在我的构建器中使用方法 .tokenCachePersistenceOptions()
但我收到此警告/错误,
c.m.a.m.CrossProcessCacheFileLock : null
谢谢!
I am using Microsoft Graph SDK for some requests however everytime I perform a GET request it does another request to get a token. I've tried reading documentation about this but I cannot find anything in Java.
Here is my implementation of my client
ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(clientId)
.clientSecret(clientSecret)
.tenantId(tenantId)
.httpClient(httpClient)
.build();
I have also tried using the method .tokenCachePersistenceOptions()
in my builder but I get this warning/error
c.m.a.m.CrossProcessCacheFileLock : null
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要实现上述要求,首先您需要进行身份验证以实施 MSAL 以从 Azure AD 获取令牌。
要获取访问令牌,您的应用程序必须向 Microsoft 标识平台注册,并由添加为该应用程序所有者的用户或管理员批准访问其所需的 Microsoft Graph 资源。
有关完整设置,请参阅此 MS 文档身份验证和Microsoft Graph 授权基础知识、此示例 & GitHub 示例|msgraph-sdk -java-core
To achieve the above requirement Firstly you need to Authenticate for implementing MSAL to get the token from Azure AD.
To obtain an access token, your app must be registered with the Microsoft identity platform and approved to access the Microsoft Graph resources it requires by either a user who is added as an owner for that application or an administrator.
For complete setup please refer this MS DOCUMENT Authentication and authorization basics for Microsoft Graph , This sample & GitHub sample|msgraph-sdk-java-core
我一直在寻找相同的思考,这是我为此实施的:
实现您自己的身份验证提供商类:
然后您可以使用它如下:
如果您获得 GraphServiceException 代码 401 401 < /em>您应该更新您的令牌。
当您成功登录客户时,您可以通过以下方式获得令牌:
希望这会有所帮助。
I was looking for the same think and here's what I've implemented for it:
Implement your own authentication provider class:
Then you can use it as follow:
If you get an GraphServiceException code 401 you should renew your token.
When you are successfully logged in with your clientSecretCredential, here's how you can get the token:
Hope this helps.
您可以覆盖为GraphServiceClient提供的Authentication Provider,
这将缓存令牌,直到一分钟不再有效。
You can override the authenticationProvider which is provided for the GraphServiceClient
This will cache the token until it one minute before it is no longer valid.