我正在尝试使用Youtube API自动允许用户通过基于浏览器的上传到Youtube来上传视频。目前,我正在通过PHP进行POST请求,但没有curl
。我无法使用我正在使用的Web服务器上的curl
。到目前为止,我的代码是:
<?php
$theCode = $_GET['code'];
$urltopost = "https://accounts.google.com/o/oauth2/token";
$datatopost = http_build_query( array(
"code" => $theCode,
"client_id" => "312231647220287.apps.googleusercontent.com",
"client_secret" => "ULGrcIJOBfjka;dsfasdfyCd7Z5M",
"redirect_uri" => ".../youtubeTestCode/hello.php",
"grant_type" => "authorization_code"
));
$opts = array('http' =>
array(
'method => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'header' => 'Host: accounts.google.com',
'content' => $datatopost
)
);
$context = stream_context_create($opts);
$result = file_get_contents($urltopost, false, $context);
?>
当我尝试时,我一直收到服务器错误。
最佳答案
如果要添加:
var_dump($http_response_header);
在
get_file_content
之后,您将看到:array(10) {
[0]=>
string(24) "HTTP/1.0 400 Bad Request"
[1]=>
string(61) "Cache-Control: no-cache, no-store, max-age=0, must-revalidate"
[2]=>
string(16) "Pragma: no-cache"
[3]=>
string(38) "Expires: Fri, 01 Jan 1990 00:00:00 GMT"
[4]=>
string(35) "Date: Fri, 31 Aug 2012 04:12:12 GMT"
[5]=>
string(30) "Content-Type: application/json"
[6]=>
string(31) "X-Content-Type-Options: nosniff"
[7]=>
string(27) "X-Frame-Options: SAMEORIGIN"
[8]=>
string(31) "X-XSS-Protection: 1; mode=block"
[9]=>
string(11) "Server: GSE"
}
这意味着您的请求结构不正确:400-错误的请求意味着身份验证失败!
关于php - 向Youtube API的PHP POST请求:获取服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12208449/