SOLVED

Suddenly getting 401 error accessing SPS Online

Copper Contributor

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

 

5 Replies
best response confirmed by Paolo Ajani (Copper Contributor)
Solution

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.

Thank 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.


You saved my day, Sunil.
Working like a charm.
Thank you!
1 best response

Accepted Solutions
best response confirmed by Paolo Ajani (Copper Contributor)
Solution

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.

View solution in original post