question

DaveshPatel-3334 avatar image
0 Votes"
DaveshPatel-3334 asked saldana-msft edited

Java sharepoint via ms-graph now returning 500 error

Hi,
We have some code that will do a query to find all sites using the java ms graph libraries.

     IDriveCollectionPage drives = graphClient.drives().buildRequest().get();

     ISiteCollectionPage sitesPage = graphClient.sites().buildRequest().  get();


Checked that the application registration in Azure has NOT changed.


The first line still works, however the second line has suddenly started to fail with the error :


 500 : Internal Server Error
 Cache-Control : no-cache
 client-request-id : 6bc81403-33ca-4aae-84d3-7b711ef12b6b
 Content-Type : application/json
 Date : Mon, 26 Apr 2021 08:10:34 GMT
 request-id : e3004346-4297-4864-9802-cc17b81e875e
 Strict-Transport-Security : max-age=31536000
 Transfer-Encoding : chunked
 Vary : Accept-Encoding
 x-ms-ags-diagnostic : {"ServerInfo":{"DataCenter":"UK South","Slice":"E","Ring":"3","ScaleUnit":"000","RoleInstance":"LN2PEPF000039ED"}}
 {
   "error": {
     "code": "generalException",
     "message": "An assertion failed while processing this request",
     "innerError": {
       "code": "assertionFailed",
       "date": "2021-04-26T08:10:34",
       "request-id": "e3004346-4297-4864-9802-cc17b81e875e",
       "client-request-id": "6bc81403-33ca-4aae-84d3-7b711ef12b6b"
     }
   }
 }

Full Code:
FULL CODE ***
'''
package com.example.testsharepoint;

import com.microsoft.aad.msal4j.ClientCredentialFactory;
import com.microsoft.aad.msal4j.ClientCredentialParameters;
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
import com.microsoft.aad.msal4j.IAuthenticationResult;
import com.microsoft.graph.models.extensions.IGraphServiceClient;
import com.microsoft.graph.requests.extensions.GraphServiceClient;
import com.microsoft.graph.requests.extensions.IDriveCollectionPage;
import com.microsoft.graph.requests.extensions.ISiteCollectionPage;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

@Component
public class SharepointAdapter {

 private static final transient Logger log = LoggerFactory.getLogger(SharepointAdapter.class);
 private String accessToken = null;
 private String clientId = "XXX";
 private String clientSecret = "XXX";
 private String tennantId = "XXX";

 protected final static String authority = "https://login.microsoftonline.com/";
 public static final String DefaultScope = "https://graph.microsoft.com/.default";
 protected Set<String> scopeSet = new HashSet<String>();
 private ConfidentialClientApplication app;
 protected IAuthenticationResult result;

 private IGraphServiceClient graphClient = null;
 private SimpleAuthProvider authProvider = null;


 public SharepointAdapter()
 {

 }

 @PostConstruct
 public  void init() throws Exception {
     getUserAccessToken();
 }

 public void getUserAccessToken() throws Exception {

     app = ConfidentialClientApplication.builder(
             clientId,
             ClientCredentialFactory.createFromSecret(clientSecret))
             .authority(authority + tennantId + "/")
             .build();

     String[] appScopes = DefaultScope.split(",");
     CollectionUtils.addAll(scopeSet, appScopes);

    ClientCredentialParameters.ClientCredentialParametersBuilder builder = ClientCredentialParameters.builder(scopeSet);
     ClientCredentialParameters clientCredentialParam = builder.build();

     if ( log.isDebugEnabled())
     {
         log.debug("{} Getting token...", getLogPrefix());
     }

     CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);

     BiConsumer<IAuthenticationResult, Throwable> processAuthResult = (res, ex) -> {
         if (ex != null) {
             Throwable error = ex.getCause();
             if (error != null) {
                 log.error("{}Error connecting to Microsoft - {}", getLogPrefix(), error.getMessage());
             } else {
                 log.error("{}Error connecting to Microsoft - {}", getLogPrefix(), ex.getMessage());
             }
         }
     };

     future.whenCompleteAsync(processAuthResult);
     future.join();

     result = future.get();
     if (result == null) {
         throw new Exception("Unable to connect to Microsoft, did not get an authentication token.");
     }

     if ( log.isTraceEnabled())
     {
         log.trace("{}: TOKEN: {}", getLogPrefix(), result.accessToken() );
     }

     String token = result.accessToken();
     authProvider = new SimpleAuthProvider(token);
     // Build a Graph client
     graphClient = GraphServiceClient.builder()
             .authenticationProvider(authProvider)
             .logger(MSLogger.getLogger())
             .buildClient();

     IDriveCollectionPage drives = graphClient.drives().buildRequest().get();

     ISiteCollectionPage sitesPage = graphClient.sites().buildRequest().  get();
 }

 protected String getLogPrefix()
 {
     return "[ Client ID: "+ clientId + "] ";
 }

}



Relevant Maven Versions:


     <java.version>14</java.version>
     <spring-cloud.version>Hoxton.SR10</spring-cloud.version>
     <springframework.boot.version>2.3.9.RELEASE</springframework.boot.version>
     <microsoft-msal4j-version>1.9.1</microsoft-msal4j-version>
     <microsoft-graph-version>2.10.0</microsoft-graph-version>
     <azure.version>3.2.0</azure.version>



Thanks in advance,

office-sharepoint-onlinemicrosoft-graph-sdk
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DaveshPatel-3334 avatar image
0 Votes"
DaveshPatel-3334 answered DaveshPatel-3334 commented

For anyone else who has this issue, found the following change made the call successful:


 LinkedList<Option> requestOptions = new LinkedList<Option>();
 requestOptions.add(new QueryOption("search", "*"));
 ISiteCollectionPage sitesPage = graphClient.sites().buildRequest(requestOptions).get();

Not sure why the old code suddenly stopped working, but this has fixed it.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Glad that you fixed this issue. Thanks for the sharing :)

0 Votes 0 ·

Thanks Michael,
Is it possible to know the reason for this change in behaviour? Also, where it would have been announced. I follow the dev blogs, and the graph release notes, but I am clearly missing a source of updates.
Regards,

1 Vote 1 ·

This isn't fixed.

There's still an issue with the API - these are unexplained 500s on a request that isn't malformed.

The workaround described above excludes personal sites from the response, which isn't sufficient for my use case.

0 Votes 0 ·

Received report that This is listed as the "accepted answer," as though it's sufficient for the person who asked the question, but in the comment thread the person who asked the question explicitly said this isn't a sufficient answer.

However since the rule of this forum said only the person who asked the question can mark the answer, I can do nothing about it.

The "answer" did fixed his problem though, just the root cause of original problem is not found. (Also note that the answer is posted by the asker, so if he want to close his question by marking answer, it's his choice)

0 Votes 0 ·

Hi, I totally agree. My answer I accepted does not include the root cause; hence have marked this as unanswered until we understand this further.

0 Votes 0 ·
ZanubKhan-0597 avatar image
0 Votes"
ZanubKhan-0597 answered ZanubKhan-0597 commented

Thanks for sharing this solution.
But did you notice a change in the response now. I found some of the sites missing. @DaveshPatel-3334

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
I haven't seen that behaviour but have just noticed that my solution does not work if you are using the beta endpoint.
Regards

0 Votes 0 ·

no , I am using v1

0 Votes 0 ·

From what I've seen, it's the personal sites that are missing - for whatever reason adding the search=* makes it so that only non-personal sites show up.

For my use case, I need all sites.

0 Votes 0 ·

Yes, exactly. For me also, it is insufficient.
Need to know till when Microsoft will fix the issue or a privide any workaround...

0 Votes 0 ·