加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

转换paypal curl获取访问令牌请求进入guzzle

发布时间:2020-12-14 00:06:30 所属栏目:Linux 来源:网络整理
导读:我需要一些Paypal rest api的帮助. 我使用guzzle作为http客户端来消费paypal api 当我在curl的命令行中尝试paypal示例时,它确实有效 但是当我想用guzzle重现它时,我总是从paypal获得内部500错误.. 这是来自官方文档的paypal curl示例(请点击此处https://deve
我需要一些Paypal rest api的帮助.

我使用guzzle作为http客户端来消费paypal api

当我在curl的命令行中尝试paypal示例时,它确实有效

但是当我想用guzzle重现它时,我总是从paypal获得内部500错误..

这是来自官方文档的paypal curl示例(请点击此处https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/):

curl -v https://api.sandbox.paypal.com/v1/oauth2/token 
  -H "Accept: application/json" 
  -H "Accept-Language: en_US" 
  -u "clientId:clientSecret" 
  -d "grant_type=client_credentials"

这是我的guzzle代码:

/**
 * Etape 1 récuperer un access token
 */
$authResponse = $client->get("https://api.sandbox.paypal.com/v1/oauth2/token",[
    'auth' =>  [$apiClientId,$apiClientSecret,'basic'],'body' => ['grant_type' => 'client_credentials'],'headers' => [
    'Accept-Language' => 'en_US','Accept'     => 'application/json' 
    ]
]);

echo $authResponse->getBody();

我尝试过auth basic,digest但到目前为止还没有工作.

感谢您的帮助!

解决方法

这是使用guzzlehttp

$uri = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$clientId = Your client_id which you got on sign up;
$secret = Your secret which you got on sign up;

$client = new GuzzleHttpClient();
$response = $client->request('POST',$uri,[
        'headers' =>
            [
                'Accept' => 'application/json','Accept-Language' => 'en_US','Content-Type' => 'application/x-www-form-urlencoded',],'body' => 'grant_type=client_credentials','auth' => [$clientId,$secret,'basic']
    ]
);

$data = json_decode($response->getBody(),true);

$access_token = $data['access_token'];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读