Trying to build an external c# web app that pulls information from SharePoint online using the rest api.

rest call: https://mysite.sharepoint.com/sites/mySubsite/_api/web/lists/GetByTitle('My Repo')/items pulls back the information I need from the browser.

Tried with the following code

 private static async Task<string>getWebTitle(webUrl) 
        string webUrl="MYSHAREPOINTSITE";
        //Creating Password 
        string PWD = "MYPASSWORD"; 
        string USER = "MYUSER"; 
        string RESTURL = "_api/web/lists/GetByTitle('My Repo')/items"; 

        //Creating Credentials 
        var passWord = new SecureString(); 
        foreach (var c in PWD) passWord.AppendChar(c); 
        var credential = new SharePointOnlineCredentials(USER, passWord); 

        using (var handler = new HttpClientHandler() { Credentials = credential }) 
            //Getting authentication cookies 
            Uri uri = new Uri(webUrl); 
            handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri)); 

            //Invoking REST API 
            using (var client = new HttpClient(handler)) 
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

                HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl)).ConfigureAwait(false); 

                string jsonData = await response.Content.ReadAsStringAsync(); 

                return jsonData; 

However it always returns 403 forbidden. Any help in resolving this would be great. Thanks in advance.

  • Did you get any solution for this..?? Apr 20, 2018 at 11:36


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.