FACEBOOK登录
FACEBOOK登录
相关设置:https://developers.facebook.com/apps
调用方法
public function third(){if (empty($type = Input::get('type', [ 'facebook', 'tuite' ]))) {return $this->error('参数错误');}if (!Config::get('oauth.power')) {return $this->error('未开启三方登录');}$config = Config::get("oauth.{$type}");if (empty($config['appid']) || empty($config['appsecret'])) {return $this->error('三方登录配置错误');}$callback = Config::get('siteurl') . Url::build('user.auth.third', [ 'type' => $type ]);if (Input::has('code') && ($code = Input::get('code', 'str', ''))) {$oauth = Oauth::getInstance($type);$token = $oauth->getAccessToken($code, $callback); if (!is_array($token)) return $this->error($token);$thirdInfo = $oauth->getInfo();$openId = $thirdInfo['id'];$nickname = $thirdInfo['name'];$avatar = $thirdInfo['avatar'];$email = $thirdInfo['email'];$unionLogin = UnionLogin::I()->where([ 'type' => $type, 'openid' => $openId ])->find();if ($unionLogin && $unionLogin['user_id']) {if (!($info = UserModel::I()->getFullInfo($unionLogin['user_id'])) || !$info['status']) {return $this->error('您帐号已经被禁用了');}UserModel::I()->setLoginStatus($info['id'], $info['name']); $this->redirect(Url::build('user.index.index'));} else {if ($this->isLogin) {UserModel::I()->oauthBind($this->userInfo['id'], $type, $openId);$this->redirect(Url::build('user.info.index'));} else {$username = strtoupper($type) . "_".date ( 's' ).sp_random_string(6); $res = UserModel::I()->add($username, $avatar, $email);if (is_numeric($res)) { UserModel::I()->oauthBind($res, $type, $openId);return $this->message('注册成功', SUCCESS, Url::build('user.info.index')); } else {return $this->message($res, ERROR, '#back#');}}}} else {$this->redirect(Oauth::getInstance($type)->authorize($callback));}}
认证类
<?phpnamespace Kuxin\Oauth;use Kuxin\Config;
use Kuxin\Helper\Http;
use Kuxin\Loader;abstract class Oauth
{/*** 申请应用时分配的app_key** @var string*/protected $appid = '';/*** 申请应用时分配的 app_secret** @var string*/protected $appsecret = '';/*** 授权类型 response_type 目前只能为code** @var string*/protected $responseType = 'code';/*** grant_type 目前只能为 authorization_code** @var string*/protected $grantType = 'authorization_code';/*** 获取request_code的额外参数** @var array*/protected $authorizeParam = [];/*** 获取accesstoekn时候的附加参数** @var array*/protected $getTokenParam = [];/*** 获取request_code请求的URL** @var string*/protected $getRequestCodeURL = '';/*** 获取access_token请求的URL** @var string*/protected $getAccessTokenURL = '';/*** API根路径** @var string*/protected $apiBase = '';/*** 授权后获取到的TOKEN信息** @var array*/protected $token = null;/*** 授权后的用户id** @var null*/protected $openid = null;/*** 授权后的用户id** @var null*/protected $type = null;/*** 构造函数** @param array $config* @param null $token*/public function __construct(array $config, $token = null){$this->appid = $config['appid'];$this->appsecret = $config['appsecret'];$this->token = $token;$this->type = '';Config::set('user_agent', '');}/*** @param $type* @param null $token* @return static*/public static function getInstance($type, $token = null){$config = Config::get("oauth.{$type}");$classname = '\\Kuxin\\Oauth\\' . $type;return Loader::instance($classname, [ $config, $token ]);}/*** 前往认证页** @param $url* @return string*/public function authorize($url){$query = parse_url($url)['query']; $param = ["response_type" => $this->responseType, "redirect_uri" => $url,"state" => time(),];$param['client_id'] = $this->appid; $param = array_merge($param, $this->authorizeParam);if (strpos($this->getRequestCodeURL, '?') === false) {return $this->getRequestCodeURL . '?' . http_build_query($param);} else {return $this->getRequestCodeURL . '&' . http_build_query($param);}}/*** 获取access token** @param $code* @param $url* @return string*/public function getAccessToken($code, $url){$param = ["grant_type" => $this->grantType, "redirect_uri" => $url,"client_secret" => $this->appsecret,"code" => $code,];$query = parse_url($url)['query'];if ($query == 'type=weixin'){$param['appid'] = $this->appid; }else {$param['client_id'] = $this->appid; }$param = array_merge($param, $this->getTokenParam);$response = Http::post($this->getAccessTokenURL, http_build_query($param));return $this->parseToken($response);}/*** @param string $api 接口名* @param string $param 参数* @param string $method 是否POST* @param bool $multi 是否上传文件* @return array*/abstract protected function call($api, $param = '', $method = 'GET', $multi = false);/*** 抽象方法 解析access_token方法请求后的返回值** @param 待处理内容* @return string*/abstract protected function parseToken($result);/*** 抽象方法 获取当前授权用户的标识** @return mixed*/abstract public function getOpenId();/*** 抽象方法 获取当前授权用户的标识** @return mixed*/abstract public function getUionId();/*** 获取用户信息** @return mixed*/abstract public function getInfo();
}
FACEBOOK类
<?php
/*** Facebook登录* @author kamiya* @ctime 2021-07-15*/
namespace Kuxin\Oauth;use Kuxin\Helper\Http;
use Kuxin\Helper\Json;class Facebook extends Oauth{/*** 获取requestCode的api接口* @var string*/protected $getRequestCodeURL = 'https://www.facebook.com/dialog/oauth';/*** 获取access_token的api接口* @var string*/protected $getAccessTokenURL = 'https://graph.facebook.com/oauth/access_token';/*** 获取request_code的额外参数 URL查询字符串格式* @var srting*/protected $Authorize = 'scope=email';/*** API根路径* @var string*/protected $apiBase = 'https://graph.facebook.com/';/*** 构造函数** @param array $config* @param null $token*/public function __construct(array $config, $token = null){parent::__construct($config, $token);$this->getTokenParam = ['appid' => $this->appid,'secret' => $this->appsecret, ];} /*** 组装接口调用参数 并调用接口** @param string $api FB API* @param array $param 调用API的额外参数* @param string $method HTTP请求方法 默认为GET* @param bool $multi* @return string json*/public function call($api, $param = [], $method = 'GET', $multi = false){/* facebook 调用公共参数 */$params = ['access_token' => $this->token,];$params = array_merge($params, $param);$data = Http::get($this->apiBase . $api, $params);return Json::decode($data, true);}protected function parseToken($result){$data = Json::decode($result, true);if (isset($data['access_token'])) {$this->token = $data['access_token']; return [ 'token' => $this->token,'expires' => $data['expires_in'],'refresh' => $data['refresh_token'],];} elsereturn "获取 facebook ACCESS_TOKEN 出错:{$result}";}/*** 获取当前授权应用的openid* @return string*/public function getOpenId(){if(isset($this->openid))return $this->openid;$data = $this->call('me?fields=id');if(!empty($data['id']))return $data['id'];elsereturn '没有获取到 facebook 用户ID!';}/*** 获取uid** @return mixed* @throws \Exception*/public function getUionId() {}/*** 获取用户信息** @return array*/public function getInfo(){ $data = $this->call('/me?fields=id,name,email');return ['id' => $data['id'],'name' => $data['name'], 'email' => $data['email'], 'avatar' => '', ];}}
FACEBOOK登录相关推荐
- Android 应用程序集成FaceBook 登录及二次封装
1.首先在Facebook 开发者平台注册一个账号 https://developers.facebook.com/ 开发者后台 https://developers.facebook.com/ap ...
- 在GoogPlay上发布的包Facebook登录失败提示签名问题
在googplay提审的包发布后,发现Facebook登录功能异常,提示如下: 意识到可能是hashkey出问题了,但是之前测试都是好的,原来是上传包到googlePlay后有个二次签名,会修改has ...
- facebook、twitter、facebook登录、whatsapp分享、微信分享
facebook.twitter.facebook 登录.whatsapp 分享.微信分享 几个概念 爬虫 所谓爬虫,是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. html 元素图谱 对 ...
- (unity)新手接入Facebook登录,分享以及google登录,Android,IOS,OC接入篇
最近接Android,IOS的Facebook登录,分享 以及Google登录,分享流程以及遇到的问题整理. 一. Android接入 google登录 第一步,前往 [ firebase] http ...
- Android Facebook登录,进来看就对了
Facebook登录 一.目录 一.开始配置 Facebook Developers面板创建应用和基本配置 集成Facebook SDK 或者 使用依赖配置(二选一即可) 编辑资源和清单 开发秘钥散列 ...
- Android FaceBook登录问题记录
虽然按照官方文档 Facebook 登录 一步一步集成,但测试的时候还是遇到了问题,在这里记录一下.希望给其他出现相同问题的朋友一些借鉴. 问题1 提示没有权限,该账号不是测试账号 解决 在你申请的应 ...
- Facebook 登录、分享
第一次做海外项目,遇到需求,需要接入Facebook登录与分享. 只说遇到的麻烦,1.登录,「网址发不了--」 首先,创建应用,这里需要注意,创建完应用后,要按需开放部分权限,否则无法唤起应用, 另外 ...
- App上传GooglePlay后,微信登录及Facebook登录异常
App上传GooglePlay后,微信登录及Facebook登录异常 微信登录异常的处理 原因 因为App上传到Google Play后,会被重新签名(Play App Signing).所以保存在微 ...
- 前端学习——第三方登录(Google登录、Facebook登录)
文章目录 前言 一.前端对接第三方登录有什么用? 二.使用环境 三.FB第三方登录 引入且封装成组件 四.G第三方登录 引入且封装成组件 五.页面上使用 总结 前言 本文介绍了作者本人学习前端Java ...
最新文章
- pcb怎么画边框_关于PCB焊盘,你了解多少?
- 刚入Linux坑常见的8大问题
- 垃圾分类毕设java程序_垃圾“拍一拍”,分类不用愁!生活垃圾分类查询小程序上线啦...
- 1054 求平均值(PAT乙级 C++)
- c# 衍生类和基类的构造顺序
- 蓝色简约好看的个人接单HTML源码
- PHP学习记录_基本语法
- 一个能支持Flash的广告控件
- C语言指针的使用例子(1)指针地址的输出
- java 定时器 quartz_Java定时器和Quartz使用
- 小爱同学服务器维修,小爱同学TTS服务(2019年5月29日更新可用版本)
- Dinky 0.6.2 已发布,优化 Flink 应用体验
- 大恒halcon 深度学习公开课
- 记一次hydra密码破解神器的学习
- 新浪与Google(谷歌)结成战略合作伙伴关系
- 数字转字符串;字符串转数字
- 用Python分析《金鱼粼》,搞清楚侯龙涛最爱谁?
- 60 种数据图表,制作工具和使用场景(建议收藏)
- 由于uvc驱动函数缺少return语句而导致内核oops的一例
- python发微信提醒天气_通过Python发送天气信息给企业微信机器人