在SellingPartnerApi::seller()中使用自定义Guzzle客户端遇认证错误
解决Saloon\Http\Senders\GuzzleSender::$client未初始化错误
解决方案
你需要用Saloon的GuzzleSender包装自定义的GuzzleHttp\Client实例,再传入authenticationClient参数,而非直接传入GuzzleClient本身。
步骤代码
- 引入Saloon的GuzzleSender类:
use Saloon\Http\Senders\GuzzleSender;
- 创建自定义GuzzleClient并包装:
// 实例化带代理配置的GuzzleClient $httpClient = new \GuzzleHttp\Client([ 'proxy' => 'http://username:password@proxy_host:port', 'timeout' => 30, 'connect_timeout' => 30, ]); // 用GuzzleSender包装自定义客户端 $authSender = new GuzzleSender($httpClient); // 初始化SellingPartnerApi $api = SellingPartnerApi::seller( clientId: $credentials->api_client_id, clientSecret: $credentials->api_client_secret, refreshToken: $credentials->refresh_token, endpoint: Endpoint::NA, authenticationClient: $authSender );
- 发起请求:
$response = $api->sellersV1()->getMarketplaceParticipations();
原因说明
jlevers/selling-partner-api SDK基于Saloon框架构建,authenticationClient参数的实际类型要求是Saloon的Sender接口实现(如GuzzleSender),而非直接的GuzzleHttp\Client。直接传入GuzzleClient会导致Saloon内部无法正确初始化GuzzleSender的$client属性,从而触发Typed property Saloon\Http\Senders\GuzzleSender::$client must not be accessed before initialization错误。
即使是无额外配置的纯净GuzzleClient,也必须通过GuzzleSender包装后才能符合参数类型要求,文档描述可能存在歧义导致误解。
内容的提问来源于stack exchange,提问作者Serhii Bondarenko
相关产品推荐
相关产品推荐

