1

我正在尝试使用 Youtube API 来自动允许用户通过基于浏览器的上传来上传视频到 Youtube。目前,我正在通过 PHP 进行 POST 请求,但没有curl. 我无法访问curl我正在使用的 Web 服务器。我到目前为止的代码是:

<?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);

?>

但是,当我尝试它时,我不断收到服务器错误。

4

1 回答 1

0

如果您要添加:

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 - 错误请求意味着身份验证失败!

于 2012-08-31T04:14:56.797 回答