locked
SharePoint Online CSOM The remote server returned an error: (401) Unauthorized RRS feed

  • Question

  • Hi there

    Don't seem to be getting anywhere with this, i'm trying to use CSOM to download and resize an image file. Sound simple enough but i seem to be having permission issue. Keep getting The remote server returned an error: (401) Unauthorized

    My code looks like this:

    using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
                {
                    if (clientContext != null)
                    {
                        try
                        {
                            clientContext.Credentials = new NetworkCredential("username", "password", "domain");

    I also tried to use App only credentials like this but no luck:

    Uri siteUri = new Uri(contextClient.Url);

                    string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);

                    //Get the Access token from the URL
                    //Requires this app to be registered with the tenant
                    string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;

    The permission is blows up here on this line: 

    FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);

    Any help will be appreciated. 

    Thursday, October 27, 2016 5:46 PM

Answers

  • Hi Lakshmanan

    None of the links you provided had the solution required. Many of the searches online and your links are referring to solutions on On premise SharePoint. Not Online.

    Solutions being suggested were saying to authenticate the context with the following:

    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("username","password","domain");
    clientContext.Credentials = credentials;

    I was trying this with no luck. Then i later realise that this won't work because i'm working on O365 of course. The network credentials required are:

    var login = "username";
    var password = "password";
    var securePassword = new System.Security.SecureString();
    foreach (char c in password)
    {
      securePassword.AppendChar(c);
    }
    var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
    clientContext.Credentials = onlineCredentials;

    I really should i've noted this much earlier because i've used this credentials in other apps so many times... Thanks to this link which nudge my memory: 

    http://sharepoint.stackexchange.com/questions/97870/csom-openbinarydirect-generates-401-error-on-office-365

      
    • Proposed as answer by sadomovalexMVP Friday, October 28, 2016 2:33 PM
    • Marked as answer by Mike_tech Saturday, October 29, 2016 3:53 PM
    Friday, October 28, 2016 10:56 AM

All replies

  • HI,

    Here yo go

    https://blogs.msdn.microsoft.com/sridhara/2014/02/06/sharepoint-2013-csom-call-from-web-part-fails-with-401-for-all-users/

    http://stackoverflow.com/questions/22841115/the-remote-server-returned-an-error-401-unauthorized-using-csom-in-asp-net

    http://www.sharepointpals.com/post/The-remote-server-returned-an-error-(401)-Unauthorized-in-SharePoint-2013-Provider-Hosted-Apps


    Please remember to click Mark as Answer on the answer if it helps you

    Thursday, October 27, 2016 7:00 PM
  • Hi Lakshmanan

    None of the links you provided had the solution required. Many of the searches online and your links are referring to solutions on On premise SharePoint. Not Online.

    Solutions being suggested were saying to authenticate the context with the following:

    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("username","password","domain");
    clientContext.Credentials = credentials;

    I was trying this with no luck. Then i later realise that this won't work because i'm working on O365 of course. The network credentials required are:

    var login = "username";
    var password = "password";
    var securePassword = new System.Security.SecureString();
    foreach (char c in password)
    {
      securePassword.AppendChar(c);
    }
    var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
    clientContext.Credentials = onlineCredentials;

    I really should i've noted this much earlier because i've used this credentials in other apps so many times... Thanks to this link which nudge my memory: 

    http://sharepoint.stackexchange.com/questions/97870/csom-openbinarydirect-generates-401-error-on-office-365

      
    • Proposed as answer by sadomovalexMVP Friday, October 28, 2016 2:33 PM
    • Marked as answer by Mike_tech Saturday, October 29, 2016 3:53 PM
    Friday, October 28, 2016 10:56 AM