C# OneDrive upload client error CompactToken parsing failed with error code: 80049217

iz111 86 Reputation points
2023-03-16T08:04:28.36+00:00

I successfuly got access token for OneDrive. But when i try to upload a file i got following error:

Code: InvalidAuthenticationToken

Message: CompactToken parsing failed with error code: 80049217

Note that same code always works on my development machine, on two other machines i also got access token but always fail on upload.

Scopes: "user.read", "Files.ReadWrite", "offline_access"

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,266 questions
OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
1,142 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,822 questions
0 comments No comments
{count} votes

Accepted answer
  1. CarlZhao-MSFT 42,451 Reputation points
    2023-03-16T10:36:51.14+00:00

    Hi @iz111

    When you get an access token, it is actually an object that contains JSON key-value pairs, and you only need to pass the Token value. So please change your code to:

    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accesstoken.Token);
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-03-16T09:03:17.96+00:00

    Hello iz111,

    Thanks for reaching out!

    This error message "CompactToken parsing failed with error code: 80049217" generally occurs when the token used is invalid.

    Please refer to the similar post:
    https://learn.microsoft.com/en-us/answers/questions/1188446/compacttoken-parsing-failed-with-error-code-800492

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have further questions about this answer, please click "Comment"

    0 comments No comments

  2. iz111 86 Reputation points
    2023-03-16T09:42:11.62+00:00

    I don't understand, it is working on my development machine. I received the token.
    If token is invalid it might be something wrong with permissions or the way I ask for token?
    Following are permissions on the Azure for my app:
    permisssions

    Code to receive access token:

    
    publicClientApp = PublicClientApplicationBuilder.Create(ApplicationID)
                                                                      .WithAuthority(AzureCloudInstance.AzurePublic, "common")
                                                                      .WithDefaultRedirectUri()
                                                                      .Build();   
    
    authResult = publicClientApp.AcquireTokenInteractive(scopes)
                                                    .WithAccount(accounts.FirstOrDefault())
                                                    .WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount)
                                                    .ExecuteAsync().GetAwaiter().GetResult();
    
    

    When I do upload I pass token as follows:

    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", <accesstoken>);
    requestMessage.Version = new Version(1, 1);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.