Java Code: SharePoint Graph API CompactToken parsing failed with error code: 80049217

Thomson Ignesious 1 Reputation point
2021-10-29T05:03:56.337+00:00

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.

<hr>

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);  
}  
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,733 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,756 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-10-29T07:37:22.317+00:00

    Hi @Thomson Ignesious ,

    I'm not familiar with Java code. But the error code means that token format is not right in Authorization header.

    When we try to generate Access Token from https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token, we would get JSON object contains some properties. We just need the value in access_token key.

    And when using the token to download the file, the Authorization header should be Bearer <access_token value>


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.