Microsoft Graph giving "The site in the encoded share URI is invalid" when trying to access shared one note item

355 Views Asked by At

I am unable to get the Microsoft graph OneDrive Share API to work with OneNote documents shared from a personal Microsoft account. A URL in the format https://1drv.ms/u/s!{text} is generated when sharing a OneNote document generated by a personal Microsoft account. However im unable to use the Microsoft Graph API. The following code is throwing this error:

Unhandled exception. Status Code: BadRequest
Microsoft.Graph.ServiceException: Code: invalidRequest
Message: The site in the encoded share URI is invalid.

C# console app code:

IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
    .Create(clientId)
    .WithRedirectUri("http://localhost")
    .Build();

InteractiveAuthenticationProvider authProvider =
    new InteractiveAuthenticationProvider(publicClientApplication, new[] {
        "https://graph.microsoft.com/Sites.ReadWrite.All",
        "https://graph.microsoft.com/Files.ReadWrite",
        "https://graph.microsoft.com/Files.ReadWrite.All"
});

var graphClient = new GraphServiceClient(authProvider);

var sharingUrl = "https://1drv.ms/u/s!...";
string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));
string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/', '_').Replace('+', '-');
var shares = await graphClient.Shares[encodedUrl].Permission.Request().GetAsync();

As far as i can tell from the docs this should work, any help appreciated.

0

There are 0 best solutions below