1

我試圖訪問使用Microsoft圖形的SharePoint網站,並在響應消息中獲得「未授權」。訪問SharePoint網站在圖形API中不起作用

這裏就是我試圖訪問獲得SP網站:https://graph.microsoft.com/beta/sharePoint/sites

查詢工作在圖探險家,但不是通過我的客戶端代碼。

注意:該應用具有SharePoint Online和Microsoft Graph的完全委派權限。

我嘗試從我的應用程序調用其他SharePoint資源,我仍然收到未經授權的錯誤消息。

代碼:

using (var client = new HttpClient()) 
      { 
       using (var request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/beta/sharePoint/sites")) 
       { 
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); 
        using (HttpResponseMessage response = await client.GetAsync("https://graph.microsoft.com/beta/sharePoint/sites")) 
        { 
         if (response.IsSuccessStatusCode) 
         { 
          msgResponse.Status = SendMessageStatusEnum.Sent; 
          msgResponse.StatusMessage = await response.Content.ReadAsStringAsync(); 
         } 
         else 
         { 
          msgResponse.Status = SendMessageStatusEnum.Fail; 
          msgResponse.StatusMessage = response.ReasonPhrase; 
         } 
        } 
       } 

回答

3

我發現這個問題,我需要指定JSON作爲內容類型和使用SendAsync代替GetAsync(),因爲我已經有一個請求對象。

using (var client = new HttpClient()) 
      { 
       using (var request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/beta/sharePoint/sites")) 
       { 
        request.Headers.Accept.Add(Json); 
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); 
        using (HttpResponseMessage response = await client.SendAsync(request)) 
        { 
         if (response.IsSuccessStatusCode) 
         { 
          msgResponse.Status = SendMessageStatusEnum.Sent; 
          msgResponse.StatusMessage = await response.Content.ReadAsStringAsync(); 
         } 
         else 
         { 
          msgResponse.Status = SendMessageStatusEnum.Fail; 
          msgResponse.StatusMessage = response.ReasonPhrase; 
         } 
        } 
       } 
      } 

希望這會有所幫助。