Google服务器不支持POST请求 [英] POST Request not supported by Google Server
问题描述
将请求从服务器发布到google.com时出现以下错误:
I am getting following error while posting request from my server to google.com:
警告:file_get_contents(http://accounts.google.com):无法打开流:HTTP请求失败!第23行的C:\ ... \ index.php中不允许使用HTTP/1.0 405方法
Warning: file_get_contents(http://accounts.google.com): failed to open stream: HTTP request failed! 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;
推荐答案
这已经很晚了,希望对您这样的人有所帮助.
This is quite late, hope it helps someone else like me.
有两个问题.首先,不能使用client_secret
生成访问令牌.因此,要包含成功的请求,必须包含client_secret
.
There are two issues. First of all an access token cannot be generated with client_secret
. So to have a successful request client_secret
must be included.
第二,谷歌希望识别client_id
,因此它也应该是POST
参数的一部分.
Secondly google wants to identify the client_id
, so it should also be a part of POST
parameters.
用于生成访问令牌的正确参数应类似于
The correct parameters for generating access tokens should be like
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata,
'client_id' => '######################',
'client_secret' => '##########################'
)
)
这篇关于Google服务器不支持POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!