Answered by:
Odata call in .Net application giving 401 Unauthorized error for Project Online

Question
-
Hi
We have been using Odata call for Project Online in our various .Net applications from past one year and today we found that it has stopped working in all and giving "The remote server returned an error: (401) Unauthorized" error.
The code snippet we are using is
var cred = new SharePointOnlineCredentials(username,securepassword);
string url = odataurl
var req = (HttpWebRequest)WebRequest.Create(url);
req.Credentials = cred;
req.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";
var resp = (HttpWebResponse)req.GetResponse();
var receiveStream = resp.GetResponseStream();
var readStream = new StreamReader(receiveStream, Encoding.UTF8);
string data = readStream.ReadToEnd();
The error is occurring at line var resp = (HttpWebResponse)req.GetResponse(); as shown in screenshot . Can you please let us know are there any security updates being performed for Project Online which needs update for development purpose.
Please help us in overcoming the issue as various production environments are getting affected.Thanks in advance.
- Edited by Supriya Khamesra Tuesday, May 8, 2018 1:53 PM
Tuesday, May 8, 2018 1:04 PM
Answers
-
Hello,
You now need to get the authentication cookie, not sure if this is a permanent change etc. but see a post here with a fix for the PowerShell WebRequest:
Paul Mather | Twitter | http://pwmather.wordpress.com | CPS | MVP | Downloads
- Marked as answer by PWMatherMVP Thursday, May 10, 2018 5:24 PM
Wednesday, May 9, 2018 7:09 AM -
Thanks Paul.
Yes with the use of authentication cookie the error has been resolved. By replacing
req.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f"; with
req.Headers.Add(HttpRequestHeader.Cookie, cred.GetAuthenticationCookie(New Uri(url))) resolved the issue and now Odata call is working fine .
- Marked as answer by Supriya Khamesra Wednesday, May 9, 2018 9:11 AM
Wednesday, May 9, 2018 7:16 AM
All replies
-
Hello,
Something has changed somewhere as I can repro this with from using the PowerShell WebRequest trying to query either the the Project OData reporting API or a SharePoint list REST API. Not sure what the answer is at this stage but will try to find out.
Paul
Paul Mather | Twitter | http://pwmather.wordpress.com | CPS | MVP | Downloads
Tuesday, May 8, 2018 8:33 PM -
Hello,
You now need to get the authentication cookie, not sure if this is a permanent change etc. but see a post here with a fix for the PowerShell WebRequest:
Paul Mather | Twitter | http://pwmather.wordpress.com | CPS | MVP | Downloads
- Marked as answer by PWMatherMVP Thursday, May 10, 2018 5:24 PM
Wednesday, May 9, 2018 7:09 AM -
Thanks Paul.
Yes with the use of authentication cookie the error has been resolved. By replacing
req.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f"; with
req.Headers.Add(HttpRequestHeader.Cookie, cred.GetAuthenticationCookie(New Uri(url))) resolved the issue and now Odata call is working fine .
- Marked as answer by Supriya Khamesra Wednesday, May 9, 2018 9:11 AM
Wednesday, May 9, 2018 7:16 AM -
Hi Paul,
This article helped us alot solving same issue.We had outage due to this issue and now its fixed after adding authentication cookies.
Please help us with information about cause of this change(part fo any recent security patch etc) once you find it.
Sil
- Proposed as answer by Puran mishra Saturday, July 25, 2020 5:54 PM
Thursday, May 10, 2018 9:21 AM -
I understand this was a change the SharePoint group made but has since been reverted so the original code would work again for a short period but this is due to be enabled again soon. A blog post / message in the message center will be published soon to communciate the change.
Paul
Paul Mather | Twitter | http://pwmather.wordpress.com | CPS | MVP | Downloads
Thursday, May 10, 2018 5:28 PM