Forum Discussion
Project Online/SharePoint Online "401 Unauthorized" error when accessing oData URLs programatically
- May 09, 2018
Hi All,
Another work around is to generate a SharePoint client context and then grab the auth cookie from those credentials and pass it to the rest api request.
authCookie = creds.GetAuthenticationCookie()
req.Headers.Add(HttpRequestHeader.Cookie, authCookie);
Let me know if you need more details
Thanks
Dan
Hi All,
Another work around is to generate a SharePoint client context and then grab the auth cookie from those credentials and pass it to the rest api request.
authCookie = creds.GetAuthenticationCookie()
req.Headers.Add(HttpRequestHeader.Cookie, authCookie);
Let me know if you need more details
Thanks
Dan
Daniel,
Thank you, i have tried your code and now its working.
Just FYI, here is the piece of updated code i have used.
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.
- shruti vyasMay 09, 2018Copper Contributor
Thanks Sunil. Changing the header worked fine.
- Daniel ColarossiMay 09, 2018Copper Contributor
Hi Sunil,
Glad to hear it worked, thanks for letting me know.
Joe - this is a little more tricky for you given you are calling the api direct in a workflow. You have a few options on how to fix this but they will all require development and will be a change to your current design.
If this isn't critical to get working I'd sit tight till someone hears back from Microsoft on a ETA in getting the api auth issues resolved. Given the impact of this issue I'd be hoping to hear something official soon.
If you have to get this working now and you have access to a developer then you could either create a standalone application that creates the sub sites and leave your workflow alone (benefit here is when MS fixes the issue your workflow will continue to work).
Or you could host your own API wrapper for the SharePoint api in azure that creates the sub sites and update your workflow to use that. That way in your api you could do the authentication. This is a pretty big change to your design tho so it might be better to wait.
Thanks
Dan
- Joe FedorowiczMay 09, 2018Iron ContributorThanks