Salesforce RestAPI使用PHP Guzzle可以进行身份验证,但无法进行查询



Im使用php-Guzle使用下面的脚本对salesforce沙箱进行身份验证。身份验证有效,我从salesforce获得了一个令牌,但当我进行下一次调用$response = $client->request()->get('services/data/v45.0/sobjects/Account/describe

我得到一个错误Missing argument 1 for GuzzleHttpClient::request() on line 59,它是request((->get。它似乎失败了,我无法解决它。如果我用PostMan api客户端和工作台尝试它,它可以毫无问题地工作。感谢您提前提供的帮助。

require 'vendor/autoload.php';
use GuzzleHttp{Client, RequestOptions};
$apiCredentials = [
'client_id' => 'myclientid',
'client_secret' => 'myclientsecret',
'security_token' => 'mysecuritytoken',
];
$userCredentials = [
'username' => 'my@domain.com.sandbox1',
'password' => 'mypassword',
];
$client = new Client(['base_uri' => 'https://test.salesforce.com/']);
try {
$response = $client->post('services/oauth2/token', [
RequestOptions::FORM_PARAMS => [
'grant_type' => 'password',
'client_id' => $apiCredentials['client_id'],
'client_secret' => $apiCredentials['client_secret'],
'username' => $userCredentials['username'],
'password' => $userCredentials['password'] . $apiCredentials['security_token'],
]
]);
$data = json_decode($response->getBody());
print_r($data);
echo '<hr>';
} catch (Exception $exception) {;
echo 'Unable to connect to Salesforce';
}

$hash = hash_hmac(
'sha256', 
$data->id . $data->issued_at, 
$apiCredentials['client_secret'], 
true
);
if (base64_encode($hash) !== $data->signature) {
echo 'Access token is invalid';
}
$accessToken = $data->access_token; // Valid access token

try {
$response = $client->request()->get('services/data/v45.0/sobjects/Account/describe', [
RequestOptions::HEADERS => [
'Authorization' => 'Bearer ' . $accessToken,
'X-PrettyPrint' => 1,
],
]);
} catch (Exception $exception) {
echo '<hr>Unable to describe Account object<hr>';
}

$accountObject = json_decode($response->getBody());
print_r($accountObject);

您混淆了客户端构造和请求样式。这项工作:

$client = new Client(...);
$client->get('services/data/...');

这也起作用:

$client = new Client(...);
$client->request('GET', 'services/data/...');

相关内容

最新更新