2016-08-04 81 views
1

我的同事和我正在處理的應用程序會生成一些PDF文件並將結果傳輸回客戶端。此MVC應用程序僅針對一個特定客戶。使用Microsoft Graph API和MSAL身份驗證將文檔保存在OneDrive上

這個想法是將PDF文件的導出集中到OneDrive(for Business),以便客戶可以隨時隨地查看任何設備上的文檔。

是否可以使用Microsoft Graph API結合msal驗證(v2.0端點)來完成此操作?
我們可以考慮將OneDrive for Business作爲小型企業的「中央存儲」解決方案 嗎?

謝謝你的任何建議。:)

回答

1

v2身份驗證端點當前不支持在沒有UI交互的情況下進行身份驗證的僅應用程序範圍。您可以通過在Azure AD中登錄,使用Microsoft身份驗證庫(MSAL)獲取訪問令牌和刷新令牌。然後,您可以使用訪問令牌和刷新令牌whiteout用戶進行交互。

刷新令牌有效期爲14天,持續使用時可以有效期長達90天。 90天后,用戶將被要求重新認證。

下面是一個樣本是供你參考上傳PDF到OneDrive爲繁茂:

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; 
      }); 
     } 

    } 
} 
+0

謝謝。真的需要具有僅應用程序範圍的身份驗證,無需UI交互。 希望團隊可以帶來這個功能 – Jens

1

我假設你使用的是C#應用程序(ASP.NET,UWP,Xamarin)的一些變種。在任何這些情況下,你試圖做的事絕對有可能。

UWP Snippets sampleXamarin Snippets sample都使用MSAL結合使用Microsoft Graph Client Library for .NET的各種Microsoft Graph調用來演示驗證。兩者都有在OneDrive上上傳,下載,更新和其他操作的示例。

如果您使用的不是.NET以外的其他平臺/語言,MSAL不會應用,但Github上的many other samples可供您檢出。其中有幾個演示了使用v2.0驗證端點。

+0

謝謝你的快速響應羅伯特。 PDF文檔是在Web API C#部分生成的,需要使用MSAL和Graph API上傳到OneDrive。 (我認爲這些技術是目前最好的選擇。) 這些操作應該在沒有UI交互的情況下進行。 此致敬禮, Jens – Jens

+0

MSAL將需要運行認證用戶界面流程,並通過用戶授予您的應用程序的權限。假設您維護刷新令牌,您可以繼續刷新訪問令牌很長一段時間。 一旦有訪問令牌,對圖的調用不需要用戶交互。 有許多方法可以使用[客戶端憑據流](https://azure.microsoft.com/zh-cn/documentation/articles/active-directory-protocols-oauth-service-to-service/)進行身份驗證來自守護進程/服務的圖形沒有用戶流量。你可能也想看看。 –

相關問題