SAP B1 Service Layer登录接口报-304空错误码,求排查建议
Let's break down your two login errors and walk through actionable fixes and checks to get you authenticated successfully.
First: Error Code 102 ("Invalid login credential")
This one has a simple root cause—you used the wrong case-sensitive field name for the username. SAP Business One Service Layer's Login endpoint expects UserName (note the capitalized "N"), not Username. When you sent the payload with Username, the API couldn't recognize the username field, so it treated your entire credential set as invalid.
Fix this by ensuring your payload uses the exact, case-sensitive field name:
{ "UserName": "my_username", "Password": "my_password", "CompanyDB": "NAME_OF_MY_DB" }
Second: Error Code -304 (Empty Error Message)
This vague error usually stems from issues with how your request is formatted, not just the payload content. Here are the most critical fixes to try:
Missing
Content-TypeHeader: Service Layer requires you to explicitly setContent-Type: application/jsonin your request headers. Without this, it can't properly parse your JSON payload, leading to the unclear -304 error.- In Postman: Add this header under the "Headers" tab (Key:
Content-Type, Value:application/json). - In Python
requests: Include the headers in your post call (see the example below).
- In Postman: Add this header under the "Headers" tab (Key:
Incorrect Endpoint URL: Double-check that your Login URL is properly structured. It should look something like
http://your-sap-server:50000/b1s/v1/Login(adjust the port and version if your environment uses HTTPS or a non-default port).Case-Sensitive Database Name: SAP B1's
CompanyDBvalue is case-sensitive. Make sure the name matches exactly what's configured in your SAP system—even minor case differences will cause authentication failures.
Working Python requests Example
Here's a tested snippet that includes the correct headers and payload structure to avoid both errors:
import requests # Update with your server details service_layer_url = "http://your-sap-server:50000/b1s/v1/Login" login_payload = { "UserName": "my_username", "Password": "my_password", "CompanyDB": "NAME_OF_MY_DB" } request_headers = { "Content-Type": "application/json" } try: response = requests.post(service_layer_url, json=login_payload, headers=request_headers) response.raise_for_status() # Raise error for HTTP status codes >=400 print("Login successful! Session details:", response.json()) except requests.exceptions.RequestException as e: print("Login failed:", e) print("Error response:", response.json())
Additional Checks If Issues Persist
- Firewall/Proxy Interference: Ensure your client (Postman/Python) can reach the SAP server's Service Layer port. Test connectivity with a simple
pingortelnetto the server and port. - Service Layer Logs: Check the Service Layer logs on your SAP B1 server—they often contain more detailed error messages that aren't returned in the API response.
- HTTPS Certificates: If you're using HTTPS, verify that your client trusts the server's SSL certificate. For testing, you can add
verify=Falseto the Pythonrequests.postcall (never do this in production!).
Once you get a successful login response, you'll receive a SessionId and Route in the JSON output—include these in subsequent requests (as headers B1SESSION and ROUTE) to fetch Items and other data.
内容的提问来源于stack exchange,提问作者neisor

