不支持的授权类型错误在Salesforce中

huangapple go评论96阅读模式
英文:

Grant type not supported error in Salesforce

问题

我正在使用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"
}
英文:

I am using Sales Force Oauth API. I am using PHP and Curl for API Integration. When I hit API using Post Method, I am getting this error message unsupported grant type.

When I hit the same API using the Postman with the same headers and body I'm getting desired response. But getting an error on my webpage. Below is the code. Thanks for the help.

&lt;?php 

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

$data = [
    &#39;grant_type&#39; =&gt; &#39;client_credentials&#39;,
    &#39;client_id&#39; =&gt; &#39;client id goes here&#39;,
    &#39;client_secret&#39; =&gt; &#39;client secret goes here&#39;,
];

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

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

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

?&gt;

Below is the error message

{&quot;error&quot;:&quot;unsupported_grant_type&quot;,&quot;error_description&quot;:&quot;grant type not supported&quot;}1

I tried many online solutions. But not getting any output. API works fine in Postman and getting below output:

{
    &quot;access_token&quot;: &quot;access token comes here&quot;,
    &quot;signature&quot;: &quot;signature comes here&quot;,
    &quot;scope&quot;: &quot;scope id comes here&quot;,
    &quot;instance_url&quot;: &quot;my company salesforce url comes here&quot;,
    &quot;id&quot;: &quot;my company id&quot;,
    &quot;token_type&quot;: &quot;token comes here&quot;,
    &quot;issued_at&quot;: &quot;issue comes here&quot;
}

答案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);

?>
英文:
&lt;?php

$curl = curl_init();

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

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

?&gt;

huangapple
  • 本文由 发表于 2023年6月12日 15:26:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定