post - 无法打开流 : HTTP request failed! HTTP/1.0 400 错

我正在为我的大学项目使用 YouTube API,但我总是遇到错误。在这里,我将他们发送到授权页面进行登录,当他们允许访问时,它会发回 $_GET['code'] 字符串。然后我将它与其他一些数据一起发送,它应该发回一个 JSON 对象。相反,我只是得到 ​​p>

Warning: file_get_contents(https://accounts.google.com/o/oauth2/token) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in http://www.example.net/callback.php on line 27

出于安全考虑,我已将我的域替换为 example.net

 urlencode($_GET['code']),
                'client_id' => urlencode('111522767640.apps.googleusercontent.com '),
                'client_secret' => urlencode('secret'),
                'redirect_uri' => urlencode('http://example.net/callback.php'),
                'grant_type' => urlencode('authorization_code')
            )
        );

        $params = 
        array('http' =>
            array(
                'method'  => 'POST /o/oauth2/token HTTP/1.1',
                'header'  => 'Host: accounts.google.com\r\n'.                           
                            'Content-Type: application/x-www-form-urlencoded',
                'content' => $postdata
            )
        );

        $context = stream_context_create($params);
        $result = file_get_contents('https://accounts.google.com/o/oauth2/token', false,$context);
        var_dump($_SESSION);
        var_dump($result);
    }
    else //If code isnt set, user must have come here erroniously or has denied access to this program
    {
        //header( 'Location: www.example.net/error.php' ) ;
    }

?>

最佳答案

file_get_contents 将向指定的 url 发出一个 GET 请求,但是 oauth2/token 需要一个 POST 请求。

参见引用资料 Google OAuth2 , PHP HTTP .

关于post - 无法打开流 : HTTP request failed! HTTP/1.0 400 错误请求 - OAuth 2.0 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9855022/

相关文章:

assembly - 与 NASM 打个近亲电话

ruby-on-rails - 从 Engine in Rails 3.x 应用程序重新打开模型和

android - 微调器依赖于 android 中的另一个微调器

css - 宽度为 100% 的额外一个像素

git - 如何从 git 存储库中完全删除数据?

google-chrome-extension - float popup.html div

.NET - WinDBG - 字符串的内容

sql-server - 将 bcp 放入带有另一个 sql 语句的事务中

c# - 以编程方式检查 WSDL 文件支持的 SOAP 版本(1.1 或 1.2 或两者)

ruby-on-rails-3 - 在 Rails 模型中一起验证日期和时间字段