Q:

Save documents on OneDrive using Microsoft Graph API and MSAL auth

An application my colleague and I are working on generates some PDF-files and streams the result back to the client. This MVC application is only targetted for one specific customer.
The idea is to centralize the export of the PDF-files to OneDrive (for Business) , so that the customer can view the document any time, any place, on any device.
Is it possible to use Microsoft Graph API in combination with msal authentication (v2.0 endpoint) to accomplish this?
Could we consider OneDrive for Business as a "central storage" solution
for small businesses?
Thank you for any advice.:)

A:

The v2 authentication endpoint does not currently support app-only scopes which authenticated without UI-interaction. You can get the access token and refresh token using the Microsoft Authentication Library (MSAL) by login in the Azure AD. Then you can use the access token and refresh token whiteout users interacted. 
The refresh tokens are valid for 14 days, and with continuous use, they can be valid up to 90 days. After 90 days, users will be asked to re-authenticate.
Here is an sample that for your reference to upload the PDF to OneDrive for bushiness:
   public static void CreateItem(string accessToken)
    {
        string itemCreateUri = "https://graph.microsoft.com/v1.0/me/drive/root/children";
        string itemData = "{\"name\":\"book2.pdf\",\"file\":{}}";
        var itemCreatedResult = RESTRequsetHelper.RESTRequsetHelper.PostAsync(itemCreateUri, itemData, accessToken).Result;
        DriveItem item = new DriveItem(itemCreatedResult);

        string itemUploadUri= $"https://graph.microsoft.com/v1.0/me/drive/items/{item.Id}/content";

        System.IO.FileStream fs = System.IO.File.Open(@"C:\users\user1\desktop\book1.pdf",System.IO.FileMode.Open);
        StreamContent streamContent = new StreamContent(fs);
        var uploadResult= RESTRequsetHelper.RESTRequsetHelper.SendHttpRequest(HttpVerb.PUT, itemUploadUri, streamContent, accessToken).Result;

        fs.Close();

    }

  public static async Task<string> PostAsync(string uri, string jsonBody, string accessToken)
    {
        return await SendHttpRequest(HttpVerb.POST, uri, jsonBody, accessToken);
    }

   private static async Task<string> SendHttpRequest(HttpVerb ver, string uri, string jsonBody, string accessToken)
    {
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", $"{accessToken}");
        var stringContent = new StringContent(jsonBody,Encoding.UTF8,"application/json");

        if (ver == HttpVerb.POST)
        {
            return await client.PostAsync(uri, stringContent).ContinueWith<string>((response) =>
           {
               return response.Result.Content.ReadAsStringAsync().Result;
           });
        }
        else
        {
            return await client.PutAsync(uri, stringContent).ContinueWith<string>((response) =>
            {
                return response.Result.Content.ReadAsStringAsync().Result;
            });
        }

    }

    public static async Task<string> SendHttpRequest(HttpVerb ver, string uri, StreamContent steamBody, string accessToken)
    {
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", $"{accessToken}");

        if (ver == HttpVerb.POST)
        {
            return await client.PostAsync(uri, steamBody).ContinueWith<string>((response) =>
            {
                return response.Result.Content.ReadAsStringAsync().Result;
            });
        }
        else
        {
            return await client.PutAsync(uri, steamBody).ContinueWith<string>((response) =>
            {
                return response.Result.Content.ReadAsStringAsync().Result;
            });
        }

    }
}

