php Salesforce中不支持授权类型错误

n7taea2i  于 2023-06-21  发布在  PHP
关注(0)|答案(1)|浏览(111)

我正在使用Sales Force Oauth API。我正在使用PHPCurl进行API集成。当我使用Post方法访问API时,我得到了这个错误消息unsupported grant type
当我使用Postman使用相同的headersbody调用相同的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"
}
fcg9iug3

fcg9iug31#

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

?>

相关问题