单击网站上的按钮进行 Salesforce 身份验证

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

单击按钮时,它应该调用 Salesforce Authentication API 并检索访问令牌。然后,它应该使用标头中的访问令牌和正文中的数据调用实际的 Salesforce API。最后,它应该在输出框中显示响应。我没有得到任何回应。总是出错。这是我的代码。有人可以帮忙吗

我试过下面的代码。出现错误

<!DOCTYPE html>
<html>
<head>
    <title>Salesforce API Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js">. 
</script>
    <script>
        function authenticate() {
            var authUrl = "https://test.sandbox.my.salesforce.com/services/oauth2/token?grant_type=password&client_id=3345gfege56e5yr&client_secret=43656457gbyn7inrby6&[email protected]&password=test@123!";
            $.post(authUrl, function(data) {
                var token = data.access_token;
                postToSalesforce(token);
            });
        }
    function postToSalesforce(token) {
        var apiUrl = "https://test.sandbox.my.salesforce.com/services/data/v53.0/sobjects/Account";
        var data = {
            "Name": "A1934NRS",
            "Type": "Customer",
            "CurrencyIsoCode": "USD",
            "Account_Country__c": "Germany",
            "Account_Street__c":"Rödelheimer Bahnweg 23",
            "Account_City__c":"Frankfurt a.M.",
            "Account_ZIP_Code__c":"60489"
        };
        $.ajax({
            type: "POST",
            url: apiUrl,
            headers: {
                "Authorization": "Bearer " + token
            },
            data: JSON.stringify(data),
            contentType: "application/json",
            success: function(response) {
                $("#output").text(JSON.stringify(response));
            }
        });
    }
</script>
</head>
<body>
    <button onclick="authenticate()">Authenticate and Post to 
Salesforce</button>
    <br><br>
    <label>Output:</label>
    <textarea id="output" rows="10" cols="50"></textarea>
</body>
</html>
javascript html salesforce-lightning
© www.soinside.com 2019 - 2024. All rights reserved.