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

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

我正在使用php Guzzle,使用以下脚本向Salesforce沙箱进行身份验证。身份验证有效,我从Salesforce获得了令牌,但是当我进行下一个调用时$response = $client->request()->get('services/data/v45.0/sobjects/Account/describe

我收到错误Missing argument 1 for GuzzleHttp\Client::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' => '[email protected]',
    '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);
php api salesforce guzzle
1个回答
0
投票

您正在混合客户端构造和请求样式。这有效:

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

这也适用:

$client = new Client(...);
$client->request('GET', 'services/data/...');
© www.soinside.com 2019 - 2024. All rights reserved.