2

我正在为我的大学项目使用YouTube API,并且一直收到错误消息。在这里,我将它们发送到授权页面进行登录,当它们允许访问时,它将发送$ _GET ['code']字符串。然后我发送这个以及其他一些数据,它应该发回一个JSON对象。相反,我刚开无法打开流:HTTP请求失败! HTTP/1.0 400错误请求 - OAuth 2.0 POST

警告:的file_get_contents(https://accounts.google.com/o/oauth2/token) [function.file-GET-内容]:未能打开流:HTTP请求 失败! HTTP/1.0 400 http://www.example.net/callback.php线坏请求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') ; 
    } 

?>

回答

0

file_get_contents将会做出一个GET请求指定的网址,但oauth2/token需要POST请求。

参见参考Google OAuth2,PHP HTTP

+0

AHAH,谢谢你,我是混合和匹配的例子了互联网,所以我必须结束与GET – 2012-03-24 20:23:25

+0

添加PHP HTTP文档以供参考:http://www.php.net/manual/en/ref.http.php – dldnh 2012-03-24 20:29:52

+2

这个问题怎么样http://stackoverflow.com/a/2445332?似乎仍然可以使用file_get_contents()发送POST – vellotis 2012-07-04 21:53:47

0

如果你正在使用的oauth2,转到库/的oauth2/provider.php,并取消对代码行182个显示

  $ci = get_instance(); 
      $ci->load->spark('curl/1.2.1'); 
      $ci->curl 
       ->create($url) 
       ->post($params, array('failonerror' => false)); 

      $response = $ci->curl->execute(); 
相关问题