英文:
Grant type not supported error in Salesforce
问题
我正在使用Sales Force OAuth API。我正在使用PHP
和Curl
进行API集成。当我使用POST方法调用API时,我收到以下错误消息:unsupported grant type
。
当我使用Postman以相同的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"
}
英文:
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.
<?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;
?>
Below is the error message
{"error":"unsupported_grant_type","error_description":"grant type not supported"}1
I tried many online solutions. But not getting any output. API works fine in Postman and getting below output:
{
"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"
}
答案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);
?>
英文:
<?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);
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论