【问题标题】:Using PHP cURL to POST data on /oauth2/access_token and GET data as jSON使用 PHP cURL 在 /oauth2/access_token 上发布数据并以 JSON 格式获取数据
【发布时间】:2015-10-03 15:44:34
【问题描述】:

我已经按照Path API 的步骤了解如何验证用户。在教程验证过程中,用户开始重定向到以下 URL 并提示授予访问权限:

https://partner.path.com/oauth2/authenticate?response_type=code&client_id=THE_CLIENT_ID

然后,服务器将通过 URL 地址以授权 code 的形式给出响应(我已完成此步骤并获得了 code)。

正如文档解释的那样,代码应该使用 /oauth2/access_token 交换为访问令牌,只要带有客户端 ID 和客户端密码(获取 access_token)

但我不知道如何通过 cURL 将数据 POST 到服务器,我尝试了很多 curl_setopt() 选项和组合,但它仍然没有给我任何东西。

从文档中,请求看起来像这样:

POST /oauth2/access_token HTTP/1.1
Host: partner.path.com
Content-Type: application/x-www-form-urlencoded
Content-Length: <LENGTH>

grant_type=authorization_code&client_id=CLIENT&client_secret=SECRET&code=CODE

还有这样的cURL格式:

curl -X POST \
     -F 'grant_type=authorization_code' \
     -F 'client_id=CLIENT_ID' \
     -F 'client_secret=CLIENT_SECRET' \
     -F 'code=CODE' \
     https://partner.path.com/oauth2/access_token

服务器会给出这样的响应:

HTTP/1.1 201 CREATED
Content-Type: application/json
Content-Length: <LENGTH>

{
    "code": 201,
    "type": "CREATED"
    "reason": "Created",
    "access_token": <ACCESS_TOKEN>,
    "user_id": <USER_ID>,
}

【问题讨论】:

    标签: php json api curl oauth-2.0


    【解决方案1】:

    要在 PHP 中使用 cURL 执行 POST 请求,您可以执行以下操作:

    $handle = curl_init('https://partner.path.com/oauth2/access_token');
    $data = array('grant_type' => 'authorization_code', 'client_id' => 'CLIENT', 'client_secret' => 'SECRET', 'code' => 'CODE');
    curl_setopt($handle, CURLOPT_POST, true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    $resp = curl_exec($handle);
    

    然后您可以使用json_decode($json_encoded) 从服务器响应中获取关联数组。

    【讨论】:

    • 我已经在使用这种方式并再次重新测试,但它仍然对我不起作用:(尽管我已经在使用 $resp 变量中的 echo/print_r/var_dump
    • 它只是不起作用,还是您收到任何错误消息?您是否从服务器得到任何响应?
    • 不,我没有从服务器得到任何响应,它只是不工作:(但是当我使用 Postman 测试时它工作正常
    • 你必须使用http_build_query($data)否则content-type不会被设置为x-www-form-urlencoded
    【解决方案2】:

    不确定您是否已经解决了这个问题,因为我看到它是从不久前开始的,但我只是遇到了这个问题,这就是我的解决方法。

    $code = $_GET['code'];
    $url = 'https://YourPath/token?response_type=token&client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type=authorization_code&code='.$code.'&redirect_uri='.$redirect_uri;
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST,true);
    
    $exec = curl_exec($ch);
    $info = curl_getinfo($ch);
    print_r($info);
    curl_close($ch);
    
    $json = json_decode($exec);
    if (isset($json->refresh_token)){
    global $refreshToken;
    $refreshToken = $json->refresh_token;
    }
    
    $accessToken = $json->access_token;
    $token_type = $json->token_type;
    print_r($json->access_token);
    print_r($json->refresh_token);
    print_r($json->token_type);
    

    希望有帮助

    【讨论】:

      【解决方案3】:

      除了 Fox Wilson 的回答:

      curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
      

      【讨论】:

      • 您应该将此作为评论添加到@Fox Wilson 的所谓答案
      • 我本来想去的,但得到“你必须有 50 声望才能发表评论”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2012-06-20
      相关资源
      最近更新 更多