问题
We are working on a project which uses Microsoft Graph SDK to implement Excel/OneDrive related functionalities. We have a use-case where we need to serialize and deserialize the IGraphServiceClient client reference/object.
We tried to deserialize the object but we're getting a NotSerializableException
exception. We were exploring SDK and find ISerializer.java class but unable to use it in serialization/Deserialization.
Could you please help us how can we get over this issue?
UsernamePasswordProvider authProvider =
new UsernamePasswordProvider(clientId, scopes, userName, password, null, tenantid, clientSecret);
IGraphServiceClient client= GraphServiceClient
.builder()
.authenticationProvider((IAuthenticationProvider) authProvider).buildClient());
回答1:
It isn't possible and, frankly, there is no value in serializing/deserializing the client itself.
What you really want is to request the offline_access
scope so that you'll receive a refresh_token
at the same time as the access_token
your using to call Microsoft Graph. You can then store the refresh_token
string and use it to receive an updated/fresh access_token
. You can then create a new IGraphServiceClient
instance using that token whenever you need to make a call to Microsoft Graph.
回答2:
You can get the IAuthenticationProvider as below.
public static void main(String[] args) {
IAuthenticationProvider authProvider = new UsernamePasswordProvider(
"{clientId}",
Arrays.asList("https://graph.microsoft.com/User.Read"),
"{userName}",
"{password}",
NationalCloud.Global,
"{tenantId}",
"{clientSecret}");
GraphServiceClient graphClient = (GraphServiceClient) GraphServiceClient.builder()
.authenticationProvider(authProvider)
.buildClient();
User user = graphClient.me().buildRequest().get();
}
By the way, if you used Maven to install microsoft-graph-auth
, there will be some issues. Currently there is a mismatch between the source code and maven repository. The source code of microsoft-graph-auth
is fine. So you can download the source code of msgraph-sdk-java-auth and exported it as jar file. Use this jar file instead of using com.microsoft.graph.0.1.0-SNAPSHOT. This will work.
Another way is to use Gradle to install microsoft-graph-auth
. This works fine.
repository {
jcenter()
jcenter{
url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
}
}
dependency {
// Include the sdk as a dependency
compile('com.microsoft.graph:microsoft-graph-auth:0.1.0-SNAPSHOT')
}
来源:https://stackoverflow.com/questions/57169069/how-to-serialize-and-deserialize-igraphserviceclient-client-object