0

i want to get all the lists within a specific SharePoint site. I managed to get the token, but I'm struggeling to use the REST Api in c#. It always says "unauthorized", but in Postman I can use it with the Bearer Token.

The problem is that I use an old class and I need to use httpClient approve, but I'm not very sure how I can use this.

The other problem is that I always need to authenticate myself. How can it be done silently and for a longer period of time.

here is my code so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Identity.Client;

namespace TestSPGetFolders
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }
        public string TokenText { get; set; }
        private void ButtonGetAccessToken(object sender, RoutedEventArgs e)

        {
            

            var authentionProvider = new AccessADTokenProvider();
            var token = authentionProvider.GetAccessTokenAsync("5345352", "1232432", "43253523", "https://login.microsoftonline.com/");

            TokenText = token.Result;


            //call SharePoint

            var SiteURL = "https://{mytentant}.sharepoint.com/sites/TestGetFolder";

            var Url = SiteURL + "/_api/Web/Lists";

            HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(Url);

            endpointRequest.Method = "Get";

            endpointRequest.Accept = "application/json;odata=verbose";
            endpointRequest.Headers.Add("Authorization", "Bearer " + token);

            HttpWebResponse endpointResponse =   
    (HttpWebResponse)endpointRequest.GetResponse();

        }
    }
}

Matthias
  • 9
  • 1
  • 6

0 Answers0