Salesforce 中不支持拨款类型错误

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

我正在使用 Sales Force Oauth API。我正在使用

PHP
Curl
进行 API 集成。当我使用 Post 方法点击 API 时,我收到此错误消息
unsupported grant type

当我使用具有相同

headers
body
的邮递员点击相同的 API 时,我得到了所需的响应。但我的网页上出现错误。下面是代码。感谢您的帮助。

<?php 

$url = 'https://mycompanynamegoeshere.my.salesforce.com/services/oauth2/token';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);

$data = [
    'grant_type' => 'client_credentials',
    'client_id' => 'client id goes here',
    'client_secret' => 'client secret goes here',
];

$data_json = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);

curl_setopt(
    $ch,
    CURLOPT_HTTPHEADER,
    array(
        'Content-Type: application/x-www-form-urlencoded',
        'Content-Length: ' . strlen($data_json),
        'Cache-Control: no-cache',
        'Connection: keep-alive',
        'Accept: application/json',
    ),
);

$response = curl_exec($ch);
curl_close($ch);
echo $response;

?>

以下是错误信息

{"error":"unsupported_grant_type","error_description":"grant type not supported"}1

我尝试了很多网上的解决方案。但没有得到任何输出。 API 在 Postman 中运行良好并得到以下输出:

{
    "access_token": "access token comes here",
    "signature": "signature comes here",
    "scope": "scope id comes here",
    "instance_url": "my company salesforce url comes here",
    "id": "my company id",
    "token_type": "token comes here",
    "issued_at": "issue comes here"
}
php curl salesforce salesforce-communities
1个回答
1
投票
<?php

$curl = curl_init();

curl_setopt_array(
    $curl,
    array(
        CURLOPT_URL => 'https://companyname.my.salesforce.com/services/oauth2/token',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => 'grant_type=client_credentials&client_id=client_id_goes_here&client_secret=client_secret_goes_here',
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/x-www-form-urlencoded',
            'Accept: application/json',
            'Cookie: BrowserId=browser_id_goes_here; CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1'
        ),
    )
);

$response = curl_exec($curl);
$decoded = json_decode($response, true);
$accessToken = $decoded["access_token"];
curl_close($curl);

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