Search code examples
c#microsoft-graph-apionenote-api

Is it necessary to have business account for use OneNote API


I am developing an application using Microsoft Graph and I am testing some basic features with OneNote. I can use the users, but I'm getting errors regarding SharePoint licenses when it comes to OneNote.

Error that i am receiving: { "error": { "code": "30121", "message": "The tenant does not have a valid SharePoint license.", "innerError": { "request-id": "12d9555f-ab42-43d6-a5ad-ed4be2fe0c93", "date": "2019-11-11T12:58:55" } } }

Do I need to buy Office of Business if I already have Office for Home, and if no how do I solve this error?

According to Calendar endpoint returns OrganizationFromTenantGuidNotFound, I need to have an Office 365 Business subscription, but it still not clear for me why it should include SharePoint.

This is the code which I am running:

public class X
{
    public void thing()
    {
        static void Main(string[] args)
        {
            Task<string> task = GetAccessToken();
            task.Wait();

            var token = task.Result;
            Console.WriteLine(token);

            Task<string> usersTask = GetUser(token);
            usersTask.Wait();

            var user = usersTask.Result;
            Console.WriteLine(user);

            Task<string> notesTask = GetNotes(token, user); //GetEmail(token, user);
            notesTask.Wait();

            var notes = notesTask.Result;
            Console.WriteLine(notes);

            Console.ReadKey();
        }

        static async Task<string> GetAccessToken()
        {
            string token = "";
            StringContent content =
                new StringContent($"client_id={clientId}&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret={appKey}&grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
            using(var response = await httpClient.PostAsync($"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token", content))
            {
                if (response.IsSuccessStatusCode)
                {
                    dynamic result = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
                    token = result.access_token;
                }
            }

            return token;
        }

        static async Task<string> GetUser(string token)
        {
            string user = "";
            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

            using(var response = await httpClient.GetAsync($"https://graph.microsoft.com/v1.0/users"))
            {
                if (response.IsSuccessStatusCode)
                {
                    var res = await response.Content.ReadAsStringAsync();
                    dynamic users = JsonConvert.DeserializeObject(res);
                    return users.value[1].id;
                }
            }

            return user;
        }

        static async Task<string> GetNotes(string token, string userId)
        {
            var data = "";
            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

            using(var response = await httpClient.GetAsync($"https://graph.microsoft.com/v1.0/users/{userId}/onenote/notebooks"))
            {
                if (response.IsSuccessStatusCode) //error
                {
                    var res = await response.Content.ReadAsStringAsync();
                    return res;
                }
            }

            return data;
        }
    }
}

Solution

  • As you are trying to access cloud based information/notebooks, you'll need an Office 365 subscription.

    OneNote Notebooks are stored on Sharepoint in Office 365.

    There's also the option to use the Interop C# assemblies...