1

I am tring to consume sharepoint REST Api in SSIS Script task but I am getting 403 forbidden error, although I am passing proper credentials

following is my code:-

public void Main()
{
HttpWebRequest endpointRequest = 
(HttpWebRequest)HttpWebRequest.Create("https://domainname.sharepoint.com/_api/web/currentuser/?$expand=groups");
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json;odata=verbose";
NetworkCredential cred = new System.Net.NetworkCredential("userName", "Password", "domain name");
endpointRequest.Credentials = cred;


        try
        {
            HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
            WebResponse webResponse = endpointRequest.GetResponse();
            Stream webStream = webResponse.GetResponseStream();
            StreamReader responseReader = new StreamReader(webStream);
            string response = responseReader.ReadToEnd();
            MessageBox.Show(response);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            //Console.Out.WriteLine(e.Message); Console.ReadLine();
        }

    }
0

Try this.

string siteUrl = "https://tenant.sharepoint.com/sites/developer/";
            string userName = "lee@tenant.onmicrosoft.com";
            string password = "password";

            using (var ctx = new ClientContext(siteUrl))
            {
                SecureString securePassword = new SecureString();
                foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
                ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);

                string sHtml = "";
                HttpWebRequest getRequest;
                HttpWebResponse response = null;
                Stream stream = null;

                var uri = new Uri("https://tenant.sharepoint.com/sites/Developer/_api/web/currentuser/?$expand=groups");
                var credentials = new SharePointOnlineCredentials(userName, securePassword);
                var authCookie = credentials.GetAuthenticationCookie(uri);
                var cookieContainer = new CookieContainer();
                cookieContainer.SetCookies(uri, authCookie);

                getRequest = (HttpWebRequest)WebRequest.Create(uri);
                getRequest.CookieContainer = cookieContainer;
                getRequest.Credentials = credentials;
                getRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                getRequest.Method = "GET";
                getRequest.Accept = "application/json;odata=verbose";

                response = (HttpWebResponse)getRequest.GetResponse();
                stream = response.GetResponseStream();
                StreamReader responseReader = new StreamReader(stream);
                string _Response = responseReader.ReadToEnd();
                Console.ReadKey();
            }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.