【第三方登录】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 剩下就是存入自己的数据和登录逻辑
       
    }
相关推荐
moonsims6 小时前
星闪在跨域无人化系统作用
开发语言·php
灯澜忆梦11 小时前
GO_网络编程---文件传输实战
网络·golang·php
happy_0x3f12 小时前
前端应用的离线暂停更新策略
开发语言·前端·php
wWYy.15 小时前
RPC详解
qt·rpc·php
Kari1116 小时前
连锁门店开业网络验收怎么做:把 PDF 清单改造成可回放的 Skill
网络·人工智能·pdf·php
万亿少女的梦16816 小时前
基于PHP的碳排放减排管理系统设计与实现
mysql·数据分析·php·系统设计·碳排放
码云数智-大飞1 天前
PHP 接口开发规范:统一返回格式、异常处理与参数校验
开发语言·php
dadaobusi1 天前
仿真边界处理
开发语言·php
微学AI2 天前
内网穿透的应用-把飞牛NAS变成家庭录像中心:EasyNVR部署与远程回放实战
开发语言·php
峥嵘life2 天前
Android WiFi连接过程 wpa_supplicant 日志分析
android·开发语言·php