Postman向Salesforce发JSON遇INVALID_SESSION_ID错误求助
问题分析与解决方案
一、401 Unauthorized(INVALID_SESSION_ID)错误排查
以下是可能的原因及对应解决步骤:
- Token请求头格式错误:确保Postman请求头中添加
Authorization: Bearer <你的OAuth2 Token>,注意Bearer与Token之间有一个空格,且Token是完整的生成值。 - 环境不匹配:确认生成Token的Salesforce实例域名(如
xxx-dev-ed.my.salesforce.com)与请求URL的域名完全一致,避免沙盒与生产环境混淆。 - Token过期:密码式OAuth2生成的Token默认有效期为15分钟,重新生成新的Token后再发起请求。
- 权限不足:
- 检查生成Token的用户是否拥有访问该Apex REST类的权限(可在Salesforce设置中为用户分配对应的Apex类权限集)。
- 确保用户拥有创建Contact记录的权限,以及Contact对象相关字段的编辑权限。
- 请求URL错误:确认URL格式正确,正确格式应为
https://<实例域名>/services/apexrest/createdRelationWebhook,替换<实例域名>为实际的Salesforce实例地址(建议使用xxx-dev-ed.my.salesforce.com这类实例域名,而非lightning.force.com结尾的体验域名)。
二、Apex类字段映射错误修正
你发送的JSON字段名与Apex类中解析时使用的键名不匹配,会导致字段值无法正确获取,甚至插入Contact时因必填字段缺失报错。以下是修正后的Apex类:
@RestResource(urlMapping='/createdRelationWebhook') global with sharing class relationCreatedWebhook { @HttpPost global static void doPost() { RestResponse response = RestContext.response; response.addHeader('Content-Type', 'application/json'); String requestBody = RestContext.request.requestBody.toString(); System.debug(System.LoggingLevel.DEBUG, 'Received JSON: \n' + requestBody); Map<String, Object> relationValues = (Map<String, Object>) JSON.deserializeUntyped(requestBody); // 修正字段名映射,与发送的JSON字段保持一致 String firstName = (String) relationValues.get('FirstName'); String lastName = (String) relationValues.get('LastName'); String phone = (String) relationValues.get('Phone'); String email = (String) relationValues.get('Email'); String city = (String) relationValues.get('MailingCity'); String street = (String) relationValues.get('MailingStreet'); String houseNumber = (String) relationValues.get('House_Number__c'); String postcode = (String) relationValues.get('MailingPostalCode'); String salutation = (String) relationValues.get('Salutation'); String crm = (String) relationValues.get('CRM__c'); String gender = (String) relationValues.get('GenderIdentity'); // 创建新Contact记录 Contact newContact = new Contact(); newContact.FirstName = firstName; newContact.LastName = lastName; newContact.Phone = phone; newContact.Email = email; newContact.MailingCity = city; newContact.MailingStreet = street; newContact.MailingPostalCode = postcode; newContact.House_Number__c = houseNumber; newContact.CRM__c = crm; newContact.Salutation = salutation; newContact.GenderIdentity = gender; // 插入Contact记录 try { insert newContact; // 返回创建成功的响应 Map<String, String> jsonResponse = new Map<String, String>(); jsonResponse.put('message', 'Contact created successfully'); jsonResponse.put('contactId', newContact.Id); response.responseBody = Blob.valueOf(JSON.serialize(jsonResponse)); } catch (Exception e) { // 处理异常 Map<String, String> errorResponse = new Map<String, String>(); errorResponse.put('error', 'Error creating contact: ' + e.getMessage()); response.responseBody = Blob.valueOf(JSON.serialize(errorResponse)); } } }
主要修正点:
- 解析JSON时使用的键名与发送的JSON字段完全一致(如
FirstName而非拼写错误的Firsname,MailingCity而非City等)。 - 变量名统一为小写开头,符合代码规范(可选优化)。
内容的提问来源于stack exchange,提问作者IMAD EL ANNASI
相关产品推荐
相关产品推荐

