SOLVED

Suddenly getting 401 error accessing SPS Online

%3CLINGO-SUB%20id%3D%22lingo-sub-191507%22%20slang%3D%22en-US%22%3ESuddenly%20getting%20401%20error%20accessing%20SPS%20Online%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-191507%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All.%3C%2FP%3E%3CP%3ESince%20yesterday%20my%20c%23%20code%20stopped%20working%20when%20trying%20to%20access%20SPS%20Online%20(and%20Project%20PWA%20in%20specific)%20through%26nbsp%3BHttpWebRequest%20interface%20while%20CSOM%20continue%20to%20work%20as%20expected.%3C%2FP%3E%3CP%3Ein%20both%20cases%20I%20use%20the%20same%20SharePointOnlineCredentials%3A%20while%20CSOM%20calls%20return%20successfully%2C%20the%20HttpWebRequest%20now%20fails%20with%20a%20401%20Unauthorized.%3C%2FP%3E%3CP%3ESame%20code%20worked%20until%20yesterday%3B%20any%20idea%3F%3C%2FP%3E%3CP%3EThank%20you%20!%3C%2FP%3E%3CP%3EPaolo%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-191834%22%20slang%3D%22en-US%22%3ERe%3A%20Suddenly%20getting%20401%20error%20accessing%20SPS%20Online%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-191834%22%20slang%3D%22en-US%22%3E%3CP%3EFor%20anyone%20interested%20in%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fpwmather.wordpress.com%2F2018%2F05%2F09%2Fchange-required-for-sharepoint-online-projectonline-rest-api-calls-when-using-webrequest-powershell-dotnet-office365dev%2F%26nbsp%3B%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpwmather.wordpress.com%2F2018%2F05%2F09%2Fchange-required-for-sharepoint-online-projectonline-rest-api-calls-when-using-webrequest-powershell-dotnet-office365dev%2F%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-191795%22%20slang%3D%22en-US%22%3ERe%3A%20Suddenly%20getting%20401%20error%20accessing%20SPS%20Online%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-191795%22%20slang%3D%22en-US%22%3EYou%20saved%20my%20day%2C%20Sunil.%3CBR%20%2F%3EWorking%20like%20a%20charm.%3CBR%20%2F%3EThank%20you!%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-191794%22%20slang%3D%22en-US%22%3ERe%3A%20Suddenly%20getting%20401%20error%20accessing%20SPS%20Online%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-191794%22%20slang%3D%22en-US%22%3EThank%20you%20Juan%20Carlos.%3CBR%20%2F%3EIn%20this%20specific%20case%20it%20looks%20like%20a%20specific%20problem%20with%20HttpWebRequest%20authentication%20while%20throttling%20impacts%20CSOM%20as%20well.%3CBR%20%2F%3EAnyway%20it's%20a%20very%20good%20point%20too%20take%20care%20of.%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-191772%22%20slang%3D%22en-US%22%3ERe%3A%20Suddenly%20getting%20401%20error%20accessing%20SPS%20Online%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-191772%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJust%20FYI%2C%20here%20is%20the%20piece%20of%20updated%20code%20i%20have%20used%20and%20now%20it%20is%20working%20fine.%3C%2FP%3E%3CP%3EThe%20one%20in%20BOLD%20is%20the%20new%20line%20of%20code%20that%20i%20have%20used%20to%20make%20it%20working.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ESecureString%20passWord%20%3D%20new%20SecureString()%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eforeach%20(char%20c%20in%20m_Password.ToCharArray())%20passWord.AppendChar(c)%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Evar%20credentials%20%3D%20new%20SharePointOnlineCredentials(%22UserName%2C%20passWord)%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Estring%20metaDataURL%20%3D%20%22ProjectOnlineURL%22%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Evar%20req%20%3D%20(HttpWebRequest)WebRequest.Create(metaDataURL)%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Ereq.Credentials%20%3D%20credentials%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3Ereq.Headers%5B%22X-FORMS_BASED_AUTH_ACCEPTED%22%5D%20%3D%20%22f%22%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3Ereq.Headers.Add(HttpRequestHeader.Cookie%2C%26nbsp%3Bcredentials.GetAuthenticationCookie(new%20Uri(metaDataURL)))%3B%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSPAN%3Evar%20resp%20%3D%20(HttpWebResponse)req.GetResponse()%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ERegards%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3ESunil%20G.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-191560%22%20slang%3D%22en-US%22%3ERe%3A%20Suddenly%20getting%20401%20error%20accessing%20SPS%20Online%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-191560%22%20slang%3D%22en-US%22%3EHave%20you%20decorated%20your%20code%20as%20suggested%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fgeneral-development%2Fhow-to-avoid-getting-throttled-or-blocked-in-sharepoint-online%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fgeneral-development%2Fhow-to-avoid-getting-throttled-or-blocked-in-sharepoint-online%3C%2FA%3E%3F%3C%2FLINGO-BODY%3E
Highlighted
New 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
Highlighted
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.

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


Highlighted
You saved my day, Sunil.
Working like a charm.
Thank you!