【第三方登录】Twitter

创建应用

APPID 和 相关回调配置

重新设置api key 和 api secret

设置回调和网址 还有 APP的类型

拿到ClientID 和 Client Secret

源码实现

  • 获取Twitter 的登录地址
php 复制代码
public function twitterUrl()
{
     global $db,$request,$comId;
 
      require "inc/twitter_client/twitteroauth.php";    
      // Create TwitterOAuth obj instance
      $twitteroauth = new \TwitterOAuth('配置 API key', '配置 API Key Secret');
      // Requesting authentication tokens, the parameter is the URL we will be redirected to
      $redirectUrl = 'https://tocmodels.com/Twitter';
      $request_token = $twitteroauth->getRequestToken($redirectUrl);
      // save to mysql 记录token和token_secret
      $log = array(
          'oauth_token' => $request_token['oauth_token'],
          'oauth_token_secret' => $request_token['oauth_token_secret'],
          'status' => 0,
          'user_id' => 0,
          'screen_name' => '',
          'dtTime' => date("Y-m-d H:i:s")
      );
      
      $db->insert_update("twitter_token_log", $log, "id");
      
      // no error
      if($twitteroauth->http_code==200){
          	// Let's generate the URL and redirect
          	$loginUrl = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);     
          
	        $return['code'] = 1;
	  		$return['message'] = '获取成功';
	  		$return['data'] = array();
	  		$return['data']['url'] = $loginUrl;
	  		
	  		return json_encode($return,JSON_UNESCAPED_UNICODE); 
      } else {
          // error
          return '{"code":0,"message":"Get Twitter login url fail!"}';
      }        
}
  • 回调跳转链接上有两个参数 oauth_verifieroauth_token,通过两个值获取用户标识
php 复制代码
	public function twitterLogin()
    {
        global $db,$request,$comId;
        
        require "inc/twitter_client/twitteroauth.php";        
        
        $oauth_verifier = $request['oauth_verifier'];
        if(empty($oauth_verifier)) return '{"code":0,"message":"oauth_verifier 为必传参数"}';  
        
        $oauth_token = $request['oauth_token'];
        if(empty($oauth_token)) return '{"code":0,"message":"oauth_token 为必传参数"}';  
                
        
        $log = $db->get_row("select * from twitter_token_log where oauth_token = '$oauth_token' and status = 0 ");
        if(!$log) return '{"code":0,"message":"无效的 oauth_token "}';  
    
        $oauth_token_secret = $log->oauth_token_secret;
        //api key  和 api key secret
        $twitteroauth = new \TwitterOAuth('配置 api key', 'api key secret ', $oauth_token, $oauth_token_secret);

        $access_token = $twitteroauth->getAccessToken($oauth_verifier);           
        
        $twitter_uid = $access_token['user_id'];//Twitter 的用户id
        $screen_name = $access_token['screen_name'];//Twitter 的用户 名称
    
        $user = $db->get_row("select * from users where twitter_uid = '$twitter_uid' ");
        //TODO 剩下就是存入自己的数据和登录逻辑
       
    }
相关推荐
Raas10016 分钟前
AI网关和OpenRouter区别?MAI Gateway(魔芋企业级AI网关)企业级方案对比指南
大数据·开发语言·人工智能·gateway·php·ai网关·mai gateway
我爱写代码i10 小时前
BeLink - 支持生成多种URL 缩短网址PHP源码
开发语言·php·短网址php源码
啊阿狸不会拉杆16 小时前
《计算机网络-自顶向下方法》5.7 网络管理、SNMP和NETCONF/YANG 读书笔记
开发语言·计算机网络·php
mjhcsp18 小时前
DeepSeek V4 Flash 0731 (Batch) 深度评测报告
log4j·php·batch
leoZ23119 小时前
第 8 篇:与 AI 协作的工作流 + 完整案例
前端·人工智能·神经网络·自然语言处理·性能优化·c#·php
qetfw19 小时前
Debian 部署 phpMyAdmin:Apache、PHP 与 MariaDB 管理界面配置
linux·debian·php·apache
AC赳赳老秦20 小时前
农产品公开数据应用:OpenClaw 抓取农产品价格、产销公开数据,实现农产品行情动态监测
java·c语言·javascript·python·php·deepseek·openclaw
三84420 小时前
redis三大机制:配置、持久化、主从复制(getshell 原理地基)
开发语言·php
G佳伟20 小时前
宝塔面板打不开且 Bt-Panel 未运行:从 HTTP 502 到 gevent 缺失的完整排查与修复
网络协议·http·php
名字还没想好☜21 小时前
Go 的 TCP 粘包与拆包:用长度前缀协议 + bufio 正确读消息
后端·tcp/ip·golang·go·php