gpt4 book ai didi

PHP:将 POST 请求 curl 到外部 Oauth2 API

转载 作者:可可西里 更新时间:2023-11-01 00:39:12 25 4
gpt4 key购买 nike

我将尝试勾画我们的情况:

  • 我们有一个需要连接到 Oath2 API 的 Wordpress 网站
  • 我还没有使用 API 和 PHP 的经验
  • 我们使用的是 PHP-Oath2 库,可以在这里找到:
    https://github.com/adoy/PHP-OAuth2/tree/master/src/OAuth2

  • 我们陷入困境的库中的方法是executeRequest。
    这个方法可以在 https://github.com/adoy/PHP-OAuth2/blob/master/src/OAuth2/Client.php 的第 404 行(哦,讽刺)上找到。

    现在,就像我说的,我在 PHP 方面的经验很少,所以我的调试方式只是在脚本中的给定点回显变量的状态。
    尽管这可能是业余的,但我至少可以验证 url 和参数是否正确。我可以打印完整的 URL(c 的虚拟参数值):

    Mind the following parameters used: code, redirect_uri, grant_typen client_id and client_Secret.

    https://oauth.smartschool.be/OAuth/index/token?code=irsyB0VSmg4V5dVjHFgdl85iRvvu3gYpsuIE4cOk&redirect_uri=https%3A%2F%2Fmycallbackurl.com%2Fmy-page%2F&grant_type=authorization_code&client_id=5d6t5ev5a6d8&client_secret=pada9c54a6sc



    当您执行此请求时,您将收到一条 JSON 错误消息。这是正常的,因为我不能给你我们的 client_id 和 client_secret:
    {"error":"error_occured","error_description":"Client authentication failed."}

    当我在浏览器中手动执行此操作(使用正确的参数值)时,我得到了所需的 JSON 结果:

    {"access_token":"fa4a6s4axsaxxsfacc56c4aca8acac4q6d5z4fsv","token_type":"Bearer","expires_in":3600,"refresh_token":"X5s1aq56xaq6bhz56aGY6SCb9845465czxqde56a"}



    问题是,当 CURL 构建并作为 POST 请求执行它时,我得到一个错误的 JSON 结果:
    {"error":"error_occured","error_description":"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the \u0022code\u0022 parameter."}

    我可以验证以下 CURL 选项的状态(请注意,该代码绕过了 ssl 验证,Client.php 文件中的第 462 行):
  • CURLOPT_RETURNTRANSFER = 真
  • CURLOPT_SSL_VERIFYPEER = false
  • CURLOPT_SSL_VERIFYHOST = false
  • CURLOPT_CUSTOMREQUEST = 'POST'
  • CURLOPT_POST = 真
  • CURLOPT_POSTFIELDS = url 编码的参数字符串

  • 例子:
    代码=irsyB0VSmg4V5dVjHFgdl85iRvvu3gYpsuIE4cOk&redirect_uri=https%3A%2F%2Fmycallbackurl.com%2Fmy-page%2Fgrant_type=authorization_code&client_id=5d6t5ev5a6d8&a6client_secret=
  • CURLOPT_URL = https://oauth.smartschool.be/OAuth/index/token
  • CURLOPT_HTTPHEADER => 空数组

  • 当像这样执行这个 Curl 处理程序时:
    $result = curl_exec($ch);

    他确实成功地联系了 API 并返回了结果,但 JSON 结果显示参数错误,声称代码不正确。
    但我知道它是正确的,因为当我通过浏览器手动执行请求时,它可以工作。如果你直接去 https://oauth.smartschool.be/OAuth/index/token没有参数,你会得到同样的错误。

    这让我想知道 curl 是否传递了任何参数。

    你能告诉我你是否在提供的 ifno 中发现错误,也许在 curl 选项中?
    或者您能否指导我采用正确的方法来调试此 curl 执行(请记住,我使用的是 Wordpress,如果这很重要的话)。

    如果这篇文章缺少必要的信息,请告诉我。
    先感谢您。

    - 编辑:

    库代码由以下代码行触发(同样,虚拟客户端 ID 和 secret )。该方法可以在 https://github.com/adoy/PHP-OAuth2/blob/master/src/OAuth2/Client.php 的第 211 行找到。
    $client = new Client('5d6t5ev5a6d8', 'pada9c54a6sc');
    $callBackUrl = 'https://mycallbackurl.com/my-page';
    $myResponse = $client->getAccessToken('https://oauth.smartschool.be/OAuth/index/token', 'authorization_code', ['code'=> $_GET['code'], 'redirect_uri' => $callBackUrl]);

    我已经删除了将 header 选项放入空数组的代码,只是为了确定。但是当使用 CURLINFO_HEADER_OUT 调试时,结果保持不变。它确实默认为 Content-Type: application/x-www-form-urlencoded。
    这是完整的 curl_getinfo() 数组:

    Array ( [url] => https://oauth.smartschool.be/OAuth/index/token [content_type] => application/json [http_code] => 200 [header_size] => 7414 [request_size] => 363 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.073514 [namelookup_time] => 3.9E-5 [connect_time] => 0.006093 [pretransfer_time] => 0.028232 [size_upload] => 216 [size_download] => 231 [speed_download] => 3142 [speed_upload] => 2938 [download_content_length] => -1 [upload_content_length] => 216 [starttransfer_time] => 0.073476 [redirect_time] => 0 [redirect_url] => [primary_ip] => xxx.xxx.xxx.xxx [certinfo] => Array ( ) [primary_port] => xxxxx [local_ip] => xxx.xxx.xxx.xxx [local_port] => xxxxx [request_header] => POST /OAuth/index/token HTTP/1.1 Host: oauth.smartschool.be Accept: */* Content-Length: 216 Content-Type: application/x-www-form-urlencoded )



    但是,结果保持不变(仍然表明根本没有传递任何参数):

    Array ( [result] => Array ( [error] => error_occured [error_description] => The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the "code" parameter. ) [code] => 200 [content_type] => application/json )



    有没有办法从这个 curl 请求打印参数/发布字段/正文,以验证所有信息是否已发送?

    - 编辑2:

    使用 Tobias 提供的 httbin.org/post 链接后,我得到了以下结果:
    Array ( 
    [args] => Array
    (
    )

    [data] =>
    [files] => Array
    (
    )

    [form] => Array
    (
    [client_id] => 5d6t5ev5a6d8
    [client_secret] => pada9c54a6sc
    [code] => fa4a6s4axsaxxsfacc56c4aca8acac4q6d5z4fsv
    [grant_type] => authorization_code
    [redirect_uri] => https://mycallbackurl.com/my-page
    )

    [headers] => Array
    (
    [Accept] => */*
    [Connection] => close
    [Content-Length] => 216
    [Content-Type] => application/x-www-form-urlencoded
    [Host] => httpbin.org
    )

    [json] =>
    [origin] => 83.xxx.xx.xx
    [url] => https://httpbin.org/post
    )

    将此与 Tobias 的示例进行比较,我没有看到任何值得注意的差异。我认为这意味着我们的 POST 请求至少包含所有必需的数据。我们在这里仍然一无所知,那么 API 不接受参数的可能原因是什么。如果您想出一些其他可能的解决方案,请不要犹豫,附加它们。感谢这个转储链接,这真的很有用!

    最佳答案

    您在浏览器中的测试与实际代码之间存在差异:在浏览器中,您将变量作为 GET 参数传递,而在您的代码中,您发送的是 POST 正文。

    为了调试这些问题,我经常发现使用 CLI 更容易 curl在去 PHP 之前。当我发送这样的请求(注意 Content-Type header )时,我设法得到了一个积极的结果:

    curl -X POST --data "code=irsyB0VSmg4V5dVjHFgdl85iRvvu3gYpsuIE4cOk&redirect_uri=https%3A%2F%2Fmycallbackurl.com%2Fmy-page%2F&grant_type=authorization_code&client_id=5d6t5ev5a6d8&client_secret=pada9c54a6sc" "https://oauth.smartschool.be/OAuth/index/token" -H "Content-Type: application/x-www-form-urlencoded"
    {"error":"error_occured","error_description":"Client authentication failed."}

    当我强行发送没有此 header 的请求时,我能够重现您的错误。这实际上并不那么容易,因为使用 --data自动添加:
    curl -X POST --data "code=irsyB0VSmg4V5dVjHFgdl85iRvvu3gYpsuIE4cOk&redirect_uri=https%3A%2F%2Fmycallbackurl.com%2Fmy-page%2F&grant_type=authorization_code&client_id=5d6t5ev5a6d8&client_secret=pada9c54a6sc" "https://oauth.smartschool.be/OAuth/index/token" -H "Content-Type: "
    {"error":"error_occured","error_description":"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the \u0022grant_type\u0022 parameter."}

    您需要确保您的代码发送带有标题的请求: Content-Type: application/x-www-form-urlencoded .

    我猜是 PHP- curl使用 POST(FIELDS) 时也会隐式添加它(与 CLI 版本类似),您的问题可能是您“强行”设置 CURLOPT_HTTPHEADER到一个空数组,从而将其删除。

    要调试正在发送的 header ,您可以使用:

    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    // curl_exec
    $information = curl_getinfo($ch);
    print_r($information);

    编辑:

    实际上更好的调试是更改您的 OAuth-URL,其中 POST 转到 https://httpbin.org/post .这只会反射(reflect)发送给它的所有内容,因此转储您得到的响应,它应该如下所示:

    Array
    (
    [result] => Array
    (
    [args] => Array
    (
    )

    [data] =>
    [files] => Array
    (
    )

    [form] => Array
    (
    [client_id] => 5d6t5ev5a6d8
    [client_secret] => pada9c54a6sc
    [code] => irsyB0VSmg4V5dVjHFgdl85iRvvu3gYpsuIE4cOk
    [grant_type] => authorization_code
    [redirect_uri] => https://mycallbackurl.com/my-page
    )

    [headers] => Array
    (
    [Accept] => */*
    [Connection] => close
    [Content-Length] => 180
    [Content-Type] => application/x-www-form-urlencoded
    [Host] => httpbin.org
    )

    [json] =>
    [origin] => 217.***.***.***
    [url] => https://httpbin.org/post
    )

    [code] => 200
    [content_type] => application/json
    )

    您可以确定从那里发送(而不是)发送的内容,包括正文数据。甚至没有 curl不幸的是,详细模式显示了这一点。

    关于PHP:将 POST 请求 curl 到外部 Oauth2 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51767361/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com