SharePoint / sp-dev-docs

SharePoint & Viva Connections Developer Documentation
https://docs.microsoft.com/en-us/sharepoint/dev/
Creative Commons Attribution 4.0 International
1.24k stars 1k forks source link

400 Bad Request Issue in Sharepoint online(through C#) #4401

Closed haniffait closed 5 years ago

haniffait commented 5 years ago

Hi ,

I have issue in SharePoint authentication through C#. I downloaded the library (sharepoint.client and sharepoint.client.runtime). I send password (secure password) to sharepoint for authentication. but application throwing error 400(Bad request). but I have admin access for my credentials. Is any other special permission required from sharepoint online for develop a desktop application

msft-github-bot commented 5 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

bcameron1231 commented 5 years ago

It's not a permission error, because that would likely be a 401 or 403.

Can you share your code that you are using to help diagnose the problem?

haniffait commented 5 years ago

Hi

Please find my below code and error message.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Net; using System.Security; using System.Threading.Tasks; using Microsoft.SharePoint.Client; namespace CSOMOffice365 { class Program { static void Main(string[] args) { string userName = "haniffait@Plan8890.onmicrosoft.com"; Console.WriteLine("Enter your password."); string password = "**"; SecureString securePassword = new SecureString(); foreach (char ch in password.ToCharArray()) { securePassword.AppendChar(ch); } try {

            using (var clientContext = new ClientContext("https://Plan8890.sharepoint.com"))
            {
                clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.ExecuteQuery();    
                Console.WriteLine("Title: ");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error is: " + ex.Message);
        }
    }
}

} Error Message : I got error message " mail id or password wrong" if i gave wrong password.

I got below error message while i am sending correct password Error is: The remote server returned an error: (400) Bad Request.

bcameron1231 commented 5 years ago

Hi, so I took your code and copied it into a console app in my solution. I have no problem connecting to my SharePoint environment, I am able to load the web successfully.

image

Can you clarify what Nuget packages are you are loading for the Microsoft.SharePoint.Client assembly?

haniffait commented 5 years ago

I have below nuget package. GriffinPlus.Lib.FastActivator(1.0.10) Microsoft.SharePointOnline.CSOM(16.1.9021.1200) SharePointPnPCoreOnline(3.11.1907) System.Configuration.ConfigurationManager(4.5.0) System.Runtime(4.3.1)

what nudget package you have?

Microsoft.Sharepoint.Client Nuget package available in Microsoft.SharePointOnline.CSOM(16.1.9021.1200)

bcameron1231 commented 5 years ago

I also have Microsoft.SharePointOnline.CSOM(16.1.9021.1200)

I'm going to take a stab in the dark here. When you created your console app... did you create it as a .NET core app instead of .NET Framework?

If so, this would return a 400 error. This is because the current version of CSOM is built on .NET Framework and not .NET core.

Try to re-create the console app as a .NET Framework console app.

haniffait commented 5 years ago

Hi Cameron, Thank you so much for your help. it was really amazing. Yes, i done that, I am purely from java background, so i dont know, how to start built in C# or .Net. First i tried to connect the sharepoint with java, but it not allowing me, it was expecting secure password(unfortunately it was not present for java) from me. so i forcefully started to using C#. Do you know how to connect sharepoint online with java.. anyway Thanks again for your help

bcameron1231 commented 5 years ago

Can you clarify that yes you can created it as a .net core app instead of a .net framework app?

haniffait commented 5 years ago

Of course, yes I can able to connect my SharePoint though .net framework instead of .net core

bcameron1231 commented 5 years ago

Great! We'll close this ticket.

But to answer your second question, you can use OAuth to connect to SharePoint Online with Java. However, you have to do it through a registered application in Azure AD. If you search google for a little, there should be plenty of resources on how to implement OAuth with SharePoint.

Thanks,

msft-github-bot commented 4 years ago

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues