0

SharePoint Graph API to generate an Access token.

URL to generate access token:

https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token

URL to download a file from SharePoint:

https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/{file-path}

When I access the above two URLs in Postman, it works fine.

I have written a code in Java Spring Boot to automate them. Generating access token works fine I can use the generated token in postman to download a file also works fine.

But When I use the generated token in Java spring boot it does gives 401 Unauthorized and the result is:

{"error":{"code":"InvalidAuthenticationToken","message":"CompactToken parsing failed with error code: 80049217","innerError":{"date":"2021-10-22T06:03:42","request-id":"857c8f56-a22c-4f63-b0be-5cd66b0bf790","client-request-id":"857c8f56-a22c-4f63-b0be-5cd66b0bf790"}}}

but with the same access token in post man it works fine.


To Generate Access Token:

 @FeignClient(url = "https://login.microsoftonline.com", name = "sharepoint", configuration = CoreFeignConfiguration.class)
    public interface SharepointAccessTokenFeignClient {
        @RequestMapping(value = "/{tenant-id}/oauth2/v2.0/token", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
        @Headers("Content-Type: application/x-www-form-urlencoded")
        Map<String, Object> generateAccessToken(@PathVariable("tenant-id") String tenantId,
                                                Map<String, ?> formParams);
    }

To Download File:

@FeignClient(url = "https://graph.microsoft.com/v1.0/sites", name = "sharepoint-graph")
public interface SharepointGraphFeignClient {

    @GetMapping(value = "/{site-id}/drive/root:/{file-path}")
    Map<String, Object> getDownloadURL(@PathVariable("site-id") String siteId,
                                       @PathVariable("file-path") String filePath,
                                       @RequestHeader(value = "Authorization", required = true) String token);
}
2
  • Could it be missing the bearer before the token in the header value? Commented Oct 29, 2021 at 11:18
  • bearer is added correctly Commented Oct 29, 2021 at 11:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.