php - 向Youtube API的PHP POST请求:获取服务器错误

标签 php post youtube

我正在尝试使用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/

相关文章:

javascript - 涉及 post json 调用的同步函数调用,其中一个函数应该在另一个函数成功后成功

java - Spring Security with Java Config - 允许 GET 方法/拒绝 POST 方法(403),没有任何明显的原因

facebook - 如何在joomla网站中更改元标记?

youtube - 有什么方法可以避免在搜索结果中获取带有 “sign in to confirm your age”的视频?

php - 在 Linux 中使用 PHP/Shell 脚本从浏览器重启 MySql 服务器

php - 添加到数据库字段时使用其中之一

java - 将内容添加到 Couchdb 中的文档

windows-phone-7 - WebBrowser控件WP7 Youtube重定向

javascript - PHP 不向 AJAX 返回数据

PHP:将唯一的随机数写入数据库