Forum Discussion
Suddenly getting 401 error accessing SPS Online
Hi All.
Since yesterday my c# code stopped working when trying to access SPS Online (and Project PWA in specific) through HttpWebRequest interface while CSOM continue to work as expected.
in both cases I use the same SharePointOnlineCredentials: while CSOM calls return successfully, the HttpWebRequest now fails with a 401 Unauthorized.
Same code worked until yesterday; any idea?
Thank you !
Paolo
Hi,
Just FYI, here is the piece of updated code i have used and now it is working fine.
The one in BOLD is the new line of code that i have used to make it working.
SecureString passWord = new SecureString();
foreach (char c in m_Password.ToCharArray()) passWord.AppendChar(c);
var credentials = new SharePointOnlineCredentials("UserName, passWord);
string metaDataURL = "ProjectOnlineURL";
var req = (HttpWebRequest)WebRequest.Create(metaDataURL);
req.Credentials = credentials;req.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";
req.Headers.Add(HttpRequestHeader.Cookie, credentials.GetAuthenticationCookie(new Uri(metaDataURL)));
var resp = (HttpWebResponse)req.GetResponse();Regards,
Sunil G.
5 Replies
- Paolo AjaniCopper Contributor
For anyone interested in: https://pwmather.wordpress.com/2018/05/09/change-required-for-sharepoint-online-projectonline-rest-api-calls-when-using-webrequest-powershell-dotnet-office365dev/
- Sunil GuntupalliCopper Contributor
Hi,
Just FYI, here is the piece of updated code i have used and now it is working fine.
The one in BOLD is the new line of code that i have used to make it working.
SecureString passWord = new SecureString();
foreach (char c in m_Password.ToCharArray()) passWord.AppendChar(c);
var credentials = new SharePointOnlineCredentials("UserName, passWord);
string metaDataURL = "ProjectOnlineURL";
var req = (HttpWebRequest)WebRequest.Create(metaDataURL);
req.Credentials = credentials;req.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";
req.Headers.Add(HttpRequestHeader.Cookie, credentials.GetAuthenticationCookie(new Uri(metaDataURL)));
var resp = (HttpWebResponse)req.GetResponse();Regards,
Sunil G.
- Paolo AjaniCopper ContributorYou saved my day, Sunil.
Working like a charm.
Thank you!
- Have you decorated your code as suggested here: https://docs.microsoft.com/en-us/sharepoint/dev/general-development/how-to-avoid-getting-throttled-or-blocked-in-sharepoint-online?
- Paolo AjaniCopper ContributorThank you Juan Carlos.
In this specific case it looks like a specific problem with HttpWebRequest authentication while throttling impacts CSOM as well.
Anyway it's a very good point too take care of.