3

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());
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Rais Ali
  • 51
  • 6
  • 1
    This is a similar problem that I'm struck on. Hope there is someone brilliant folk, to help us out. Thanks for posting this question! – taurus05 Jul 23 '19 at 17:20
  • I was also trying to come over this situation but was stucked here. – Prakhar Khandelwal Jul 23 '19 at 17:30
  • 2
    I'm not sure I understand why you'd want to serialize the client itself. For what purpose? The client is stateless and the token isn't portable so you'd need to deserialize and then request a token which is a _lot_ more overhead to accomplish the same thing as creating a new instance. – Marc LaFleur Jul 23 '19 at 18:27
  • Hi @MarcLaFleur, Is it possible to store authentication and refresh token and later use them to create a new IGraphServiceClient instance? – Rais Ali Jul 23 '19 at 20:27

2 Answers2

2

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.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • Hi Marc, Thanks for helping me out. I got your point on "serializing/deserializing the client". As you suggested to create a new IGraphServiceClient instance using access_token, I explored "msgraph-sdk-java-auth" but could not find any AuthenticationProvider class whose constructor accepts token as arguments. IGraphServiceClient client= GraphServiceClient .builder() .authenticationProvider((IAuthenticationProvider) authProvider).buildClient()); – Rais Ali Jul 23 '19 at 22:23
  • In the above LOC, I need to pass an IAuthenticationProvider object to build a client object. Is there any implementations available to get IAuthenticationProvider instance through the token. – Rais Ali Jul 23 '19 at 22:23
1

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')
}
Tony Ju
  • 14,891
  • 3
  • 17
  • 31