从 Postman 向 Salesforce 发送 JSON ([{"message":"会话已过期或无效","errorCode":"INVALID_SESSION_ID"}])

问题描述 投票:0回答:1

我正在尝试将 JSON 字符串从 Postman 发送到 Salesforce 但我收到此错误消息

[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
,其中包含 401 未经授权状态代码

我通过创建集合然后使用 OAuth2.0 生成令牌(密码凭证)成功创建了令牌 之后,我在该 Collection 下创建了一个 Post 请求,并使用了我得到的相同令牌 但我还是同样的错误。

这是我要发布的对象

{ "FirstName": "your_value_here", "LastName": "your_value_here", "Email": "your_value_here", "MailingCity": "your_value_here", "MailingStreet": "your_value_here", "MailingPostalCode": "your_value_here", "House_Number__c": "your_value_here", "CRM__c": "your_value_here", "Salutation": "your_value_here", "GenderIdentity": "your_value_here", "Phone": "your_value_here" }

这是我创建的 Apex 类 该类解析 JSON 文件并创建一个新联系人 然后返回创建的联系人的ID

`@RestResource(urlMapping='/createdRelationWebhook') 全局共享类关系CreatedWebhook {

@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);

    // Extracting required fields from the JSON
    String firstName = (String) relationValues.get('Firsname');
    String lastName = (String) relationValues.get('Lastname');
    String Phone = (String) relationValues.get('Phone');
    String email = (String) relationValues.get('Email');
    String city = (String) relationValues.get('City');
    String street = (String) relationValues.get('Street');
    String houseNumber = (String) relationValues.get('HouseNumber');
    String postcode = (String) relationValues.get('Postcode');
    String Prefix = (String) relationValues.get('Prefix');
    String CRM = (String) relationValues.get('CRM');
    String Gender = (String) relationValues.get('Gender');

   
    // Create a new Contact record
    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 = Prefix;
    newContact.GenderIdentity = Gender; 


    // Insert the new Contact record
    try {
        insert newContact;

        // Send response with the ID of the newly created Contact
        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) {
        // Handle any exceptions or errors
        Map<String, String> errorResponse = new Map<String, String>();
        errorResponse.put('error', 'Error creating contact: ' + e.getMessage());
        response.responseBody = Blob.valueOf(JSON.serialize(errorResponse));
    }
}

}`

网址为 https://-dev-ed.develop.lightning.force.com/services/apexrest/createdRelationWebhook

希望大家帮我解决这个问题 谢谢

salesforce salesforce-development salesforce-developer
1个回答
0
投票

您第二次通话的端点错误。 “Lightning”域仅用于 UI 访问。 您应该从成功的登录响应中获取两件事 - 访问令牌(也称为会话 ID)和实例 URL - 从现在开始使用的端点。

您可以对调用的端点进行硬编码,但是当您将集成从开发移动到 UAT 再到产品时,记住更改端点是很糟糕的...(或者当 SF 偶尔将您的实例迁移到另一个数据中心或 hyperforce 时)。这需要更多工作,但请阅读端点并使用它。

啊,并确保您的用户有权在配置文件/权限集中访问此类

© www.soinside.com 2019 - 2024. All rights reserved.