GitHub oauth api返回无法打开流:HTTP请求失败! HTTP / 1.0 403禁止使用file_get_contents

时间:2014-11-02 08:35:16

标签: cakephp curl github oauth file-get-contents

public function get_code()
{
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $client_id      = 'my_client_id';
    $redirect_url   = HTTP_ROOT.'users/get_code';

    $code = $_GET['code'];

    //perform post request now
    $opts = array(
        'http' => array(
            'method'  => 'POST',
            'header'  => "Accept: application/json\r\nContent-type: application/x-www-form-urlencoded\r\n",
            'user_agent' => $user_agent,
            'content' => http_build_query(
                array(
                    'client_id' => $client_id,
                    'client_secret' => 'my_secert_id',
                    'code' => $code
                )
            )
        )
    );

    $context = stream_context_create($opts);

    $json_data = file_get_contents("https://github.com/login/oauth/access_token", false, $context);

    $r = json_decode($json_data , true);

    $access_token = $r['access_token'];

    $url = "https://api.github.com/user?access_token=". urlencode($access_token);

    // $data =  file_get_contents($url);
    $data =  $this->curl_get_contents($url);

    $user_data = json_decode($data , true);
    echo "<pre>";print_r($user_data);die;
    $username = $user_data['login'];

    $emails =  file_get_contents("https://api.github.com/user/emails?access_token=$access_token");
    $emails = json_decode($emails , true);
    $email = $emails[0];

    $signup_data = array(
        'username' => $username ,
        'email' => $email ,
        'source' => 'github' ,
    );

    echo "<pre>";
    print_r($signup_data);die;

}

function curl_get_contents($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

我正在使用cakephp框架。如果我使用file_get_contents($ url)。

然后我收到以下警告..

  

file_get_contents(https://api.github.com/user?access_token=dec39b61a997d1509c03a7128573187f1ed02684):无法打开流:HTTP请求失败! HTTP / 1.0 403禁止

但正如此链接所示:file_get_contents returns 403 forbidden

我使用curl,在打印$user_data变量后使用它时,我得到了空白页。没有错误没有警告,但没有检索到数据。

如果我复制这个网址并直接放在浏览器中,我会得到我需要的所有信息,所以请告诉我我错在哪里..

谢谢

1 个答案:

答案 0 :(得分:0)

尝试下面的代码 - :

if(isset($_GET['code']))
    {
            $code = $_GET['code'];
            $post = http_build_query(array(
                'client_id' => 'my_client_id',
                'redirect_url' => 'redirect_url',
                'client_secret' => 'client_secret',
                'code' => $code,
            ));

            $context = stream_context_create(
                array(
                    "http" => array(
                        "method" => "POST",
                        'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
                                    "Content-Length: ". strlen($post) . "\r\n".
                                    "Accept: application/json" ,  
                        "content" => $post,
                    )
                )
            );

            $json_data = file_get_contents("https://github.com/login/oauth/access_token", false, $context);
            $r = json_decode($json_data , true);
            $access_token = $r['access_token'];
            $scope = $r['scope']; 

            /*- Get User Details -*/
            $url = "https://api.github.com/user?access_token=".$access_token."";
            $options  = array('http' => array('user_agent'=> $_SERVER['HTTP_USER_AGENT']));
            $context  = stream_context_create($options);
            $data = file_get_contents($url, false, $context); 
            $user_data  = json_decode($data, true);
            $username = $user_data['login'];

            /*- Get User e-mail Details -*/                
            $url = "https://api.github.com/user/emails?access_token=".$access_token."";
            $options  = array('http' => array('user_agent'=> $_SERVER['HTTP_USER_AGENT']));
            $context  = stream_context_create($options);
            $emails =  file_get_contents($url, false, $context);
            $email_data = json_decode($emails, true);
            $email_id = $email_data[0]['email'];
            $email_primary = $email_data[0]['primary'];
            $email_verified = $email_data[0]['verified'];

    }
  

错误:无法打开流:HTTP请求失败! HTTP / 1.0 403   禁

此错误由以下代码解决: -

    $url = "https://api.github.com/user?access_token=".$access_token."";
    $options  = array('http' => array('user_agent'=> $_SERVER['HTTP_USER_AGENT']));
    $context  = stream_context_create($options);
    $data = file_get_contents($url, false, $context); 
    $user_data  = json_decode($data, true);
    $username = $user_data['login'];