我正在为我的大学项目使用 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/