【问题标题】:POST Request not supported by Google ServerGoogle 服务器不支持 POST 请求
【发布时间】:2013-10-24 11:52:54
【问题描述】:

从我的服务器向 google.com 发布请求时出现以下错误:

警告:file_get_contents(http://accounts.google.com):打开流失败:HTTP 请求失败! HTTP/1.0 405 Method Not Allowed in C:\...\index.php on line 23

我的代码如下:

$postdata = http_build_query(
    array(
        'code'          => '####',
        'client_id'     => '####',
        'client_secret' => '####',
        'grant_type'    => 'authorization_code'
    )
);

$opts = array(
    'http' => array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context = stream_context_create($opts);

$result = file_get_contents('http://accounts.google.com', false, $context);

echo $result;

【问题讨论】:

  • 不要发布凭据 :)
  • 好吧,你应该在这里联系谷歌。根据 RFC 1945,HTTP/1.0 没有 405 错误代码,因此适用 4xx 类:tools.ietf.org/html/rfc1945#section-9.4 - HTTP/1.1 中的“猜测”(具有 405 代码),此处对此进行了解释:tools.ietf.org/html/rfc2616#section-10.4.6
  • 已经在 google 报告了这个问题。等待他们的回应。虽然当我使用“fopen”或“curl”时它没有给出任何错误但它给出了空响应。

标签: php oauth-2.0 httpcontext google-authentication


【解决方案1】:

您选择的方法是“PUT”而不是“POST”。如果您想以 POST 形式发送请求,请更改

'方法' => 'PUT'

'方法' => 'POST'

【讨论】:

  • 我检查了 PUT 和 POST 但同样的事情发生了。对于 GET 它给出错误 400。
【解决方案2】:

首先,您在这里得到一个响应代码 (405),它在错误类中(400 到 499 或也写为 4xx)。

因此服务器向您报告协议级别的错误。使用的协议也被命名为:HTTP,更具体地说是 HTTP/1.0。

HTTP/1.0 包括状态代码在 RFC1945 中进行了概述:

这里的问题是没有定义代码 405(作为具体数字)。所以文档回退到4xx for error codes 的一般描述。

RFC2616 中概述了 HTTP/1.1:

它有 the 405 error code details 但是,正如代码示例中的响应标头所示:

// ...
$result = file_get_contents('http://accounts.google.com', false, $context);

var_dump($http_response_header);

输出:

array(6) {
  [0] => string(31) "HTTP/1.0 405 Method Not Allowed"
  [1] => string(38) "Content-Type: text/html; charset=UTF-8"
  [2] => string(19) "Content-Length: 958"
  [3] => string(35) "Date: Thu, 24 Oct 2013 12:03:09 GMT"
  [4] => string(15) "Server: GFE/2.0"
  [5] => string(27) "Alternate-Protocol: 80:quic"
}

所需的响应列表显示另一个协议违规,因为所需的 Allow 标头不是响应的一部分。它会显示允许使用哪些方法。

因此,您所能做的就是尝试使用常见的 HTTP 方法而不是 PUT,您可能正在寻找 POST。让我们使用 POST 运行示例,但没有运气:

警告:file_get_contents(http://accounts.google.com):打开流失败:HTTP 请求失败! HTTP/1.0 405 HTTP 方法 POST 不受此 URL 支持

我建议您联系 Google(您向其发送 HTTP 请求的在线服务的供应商),并询问您需要与您心目中的请求相匹配的技术规范。出于调试目的,我建议您阅读 PHP 的特殊 $http_response_header 变量,我有一些示例代码和解释以及一些使用 PHP's HTTP Wrapper 处理错误的提示:

【讨论】:

    【解决方案3】:

    使用了错误的 URI,(这就是你没有得到允许的方法头的原因)。访问令牌通过https://accounts.google.com/o/oauth2/token 检索,需要“https”。

    不相关:您的 postdata 缺少所需的“redirect_uri”。

    【讨论】:

      【解决方案4】:

      这已经很晚了,希望它可以帮助像我这样的其他人。

      有两个问题。首先,无法使用client_secret 生成访问令牌。所以要成功请求 client_secret 必须包括在内。

      其次google要识别client_id,所以它也应该是POST参数的一部分。

      生成访问令牌的正确参数应该是这样的

      $opts = array(
      'http' => array(
          'method'  => 'POST',
          'header'  => 'Content-type: application/x-www-form-urlencoded',
          'content' => $postdata,
          'client_id' => '######################',
          'client_secret' => '##########################'
       )
      )
      

      【讨论】:

        猜你喜欢
        • 2017-01-11
        • 2021-12-31
        • 2019-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-23
        相关资源
        最近更新 更多