github第三方登录(php实现)

官网文档

https://docs.github.com/cn/developers/apps/building-oauth-apps/authorizing-oauth-apps#web-application-flow

原理

  • 客户端发起请求redirect到OAuth接入方并附带上client_id
  • 用户在redirect之后的网站上输入用户名和密码
  • 登陆成功之后,OAuth接入方会返回给服务端一个code。
  • 服务端拿到code之后,拿着client_secret和code向OAuth接入方申请获得Token
  • 服务端拿到Token之后,进入授权窗口
  • 授权成功,跳转到客户端网站。
    在这里插入图片描述

创建好github账号后点击设置

在这里插入图片描述

之后进入前期工作

步骤一

在这里插入图片描述

步骤二

在这里插入图片描述

步骤三

在这里插入图片描述

步骤四添加好必填项

在这里插入图片描述

注册应用后会生成client_id 然后创建密码

在这里插入图片描述

配置好之后接下来是代码实现

在这里插入图片描述

http://chenyu.chuxuankq.top:39992/index/User/login对应配置的 Homepage URL(网站首页地址)
http://chenyu.chuxuankq.top:39992/index/User/github_callback对应配置的 Application callback URL(回调地址)

comtroller文件


	/**第一次请求接口get
	* @param [client_id,redirect_uri]
	* 请求方式需要使用get
	**/
    public function login()
    {
        $client_id = '5b5cd796f1d7a2d91588';
        $redirect_uri = 'http://chenyu.chuxuankq.top:39992/index/User/github_callback';
        $url='https://github.com/login/oauth/authorize?client_id='.$client_id.'&redirect_uri='.$redirect_uri;
        header('Location: '.$url); 
    }
    /**回调方法
    * 回调方法会带回code参数和对应的值,让后发起第二次请求获得access_token
    * 请求方式需要使用post
	* @param [client_id,client_secret,code]
	* @return json
	**/
    public function github_callback()
    {
        $param=input();
        // \think\facade\Log::write($param);
        if(!empty($param['code'])){
            $access_token_url='https://github.com/login/oauth/access_token';
            $client_id = '5b5cd796f1d7a2d91588';
            $client_secret='******';
            $arr=[
                'client_id'=>$client_id,
                'client_secret'=>$client_secret,
                'code'=>$param['code'],
                'state'=>time()
            ];
            $data_json=curl($access_token_url,$arr);
            $data=json_decode($data_json,true);
            Session::set('access_token',$data['access_token']);
        
        }
      
    }
     public function get_github_userinfo()
    {
        $url='https://api.github.com/user';
        $data=curlRequest($url);
        $data=json_decode($data,true);
        p($data);
    }

common.php文件

注意header头格式和Authorization验证

function curl($url, $post_data = [])
{
    $header=[
        'Accept: application/json',
    ];
    //初始化
    $curl = curl_init(); 
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // 'Accept: application/json'
  
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    if (!empty($post_data)) {
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置post数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
    } else {
        curl_setopt($curl, CURLOPT_POST, 0);
    }
    curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
    //注意,毫秒超时一定要设置这个
    curl_setopt($curl, CURLOPT_TIMEOUT_MS, 5000);
    //30秒 超时毫秒,
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    //执行命令
    $data = curl_exec($curl);
    //var_dump($data);exit;
    //关闭URL请求
    curl_close($curl);
    return $data;
}
function curlRequest($url, $post_data = [])
{
    $header=[
        'Accept: application/vnd.github+json',
        'Authorization: token '.Session::get('access_token')
    ];
    //初始化
    $curl = curl_init(); 
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // 'Accept: application/json'
  
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    if (!empty($post_data)) {
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置post数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
    } else {
        curl_setopt($curl, CURLOPT_POST, 0);
    }
    curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
    //注意,毫秒超时一定要设置这个
    curl_setopt($curl, CURLOPT_TIMEOUT_MS, 5000);
    //30秒 超时毫秒,
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    //执行命令
    $data = curl_exec($curl);
    //var_dump($data);exit;
    //关闭URL请求
    curl_close($curl);
    return $data;
}

获取该用户信息的结果

Array
(
    [login] => chenyu125
    [id] => 109620514
    [node_id] => U_kgDOBoitIg
    [avatar_url] => https://avatars.githubusercontent.com/u/109620514?v=4
    [gravatar_id] => 
    [url] => https://api.github.com/users/chenyu125
    [html_url] => https://github.com/chenyu125
    [followers_url] => https://api.github.com/users/chenyu125/followers
    [following_url] => https://api.github.com/users/chenyu125/following{/other_user}
    [gists_url] => https://api.github.com/users/chenyu125/gists{/gist_id}
    [starred_url] => https://api.github.com/users/chenyu125/starred{/owner}{/repo}
    [subscriptions_url] => https://api.github.com/users/chenyu125/subscriptions
    [organizations_url] => https://api.github.com/users/chenyu125/orgs
    [repos_url] => https://api.github.com/users/chenyu125/repos
    [events_url] => https://api.github.com/users/chenyu125/events{/privacy}
    [received_events_url] => https://api.github.com/users/chenyu125/received_events
    [type] => User
    [site_admin] => 
    [name] => 
    [company] => 
    [blog] => 
    [location] => 
    [email] => 
    [hireable] => 
    [bio] => 
    [twitter_username] => 
    [public_repos] => 1
    [public_gists] => 0
    [followers] => 0
    [following] => 0
    [created_at] => 2022-07-19T17:42:10Z
    [updated_at] => 2022-07-19T17:42:10Z
)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值