Github api登录 - 获取用户

时间:2014-02-16 11:16:27

标签: api github oauth


我有Github api的问题。 我解析access_token但是当我尝试获取用户时,我收到以下错误:

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

当我尝试通过我的浏览器中的地址栏获取内容时正在运行!

我的代码:

//Get the access token
$postdata = http_build_query(
    array(
        'client_id' => 'mplamplaID',
        'client_secret' => 'mplamplaSECRET',
        'code' => trim($_GET['code']),
        'redirect_uri' => 'http://vlingos.com/github.php'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context  = stream_context_create($opts);
$vip = file_get_contents('https://github.com/login/oauth/access_token',false,$context);
parse_str($vip);
//Get the User
$json = file_get_contents('https://api.github.com/user?access_token='.$access_token);
if(isset($json) and (is_array($json)===true or is_object($json)===true)){
    $jsonIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($json, TRUE)),RecursiveIteratorIterator::SELF_FIRST);
            foreach ($jsonIterator as $key => $val) {
                $git_user[$key] = $val;
            }
print_r($git_user);
}else{
    echo "can't get user";
}

有人可以提出一些建议吗? 最亲切的问候

1 个答案:

答案 0 :(得分:0)

启用令牌是正确的。生成新令牌,请参阅:https://help.github.com/articles/creating-an-access-token-for-command-line-use/

测试它: curl https://api.github.com/user?access_token=your令牌