新浪sae授权流程的理解

1.去新浪开放平台下载一个授权demo

2.index.php

<?php
session_start();

include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );

$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
//跳转到授权的弹出框
$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新浪微博PHP SDK V2版 Demo - Powered by Sina App Engine</title>
</head>

<body>
    <!-- 授权按钮 -->
    <p><a href="<?php echo $code_url;?>"><img src="weibo_login.png" title="点击进入授权页面" alt="点击进入授权页面" border="0" /></a></p>

</body>
</html>

3.config.php

<?php
header('Content-Type: text/html; charset=UTF-8');

define( "WB_AKEY" , '2145580683' );
define( "WB_SKEY" , '5a21c49f0e05a772c6df66f1d34dbed9' );
//回调地址:这个必须调用线上的才行
define( "WB_CALLBACK_URL" , 'http://zrpzy.sinaapp.com/callback' ); ?>

4.callback.php

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhangys
 * Date: 12-3-22
 * Time: 上午7:43
 * To change this template use File | Settings | File Templates.
 */
//if ($this->user->is_logged) {
//    $this->user->logout();
//    //   $this->redirect('login/sina');
//}
$o = new SaeTOAuthV2($C->sina_appkey, $C->sina_appsecrect);
if (isset($_REQUEST['code'])) {
    $keys = array();
    $keys['code'] = $_REQUEST['code'];
    $keys['redirect_uri'] = $C->SITE_URL . 'login/sina';
    $token = $o->getAccessToken('code', $keys);
  //如果用户授权了就会得到一个token值
if ($token) { //echo '1';die; //$this->redirect('home');die; //保存token $exptime = $token['remind_in'] + time(); $dbm->query('replace into token_sina set sina_uid= ' . $token['uid'] . ',access_token="' . $token['access_token'] . '",exptime=' . $exptime); //保存用户信息 $c = new SaeTClientV2($C->sina_appkey, $C->sina_appsecrect, $token['access_token']); $me = $c->show_user_by_id($token['uid']); //根据ID获取用户等基本信息 //print_r($me); $dbm->query('SELECT uid FROM users_sina WHERE sina_uid="'.$token['uid'].'" LIMIT 1'); if( 0 == $dbm->num_rows() ) { //写入用户信息 $dbm->query('INSERT INTO users SET email="' .$me['id'] . '@weibo.com", username="' . $dbm->e($me['name']) . '", password="no",reg_date="' . time() . '", reg_ip="' . ip2long($_SERVER['REMOTE_ADDR']) . '", active=1'); $user_id = intval($dbm->insert_id()); $dbm->query('replace into users_sina set uid= '.$user_id .' ,sina_uid='.$me['id'].' ,name="'.$me['name'].'" ,province='.$me['province'].' ,city='.$me['city'].' ,domain="'.$me['domain'].'",gender="'.$me['gender'].'",followers_count='.$me['followers_count'].',friends_count='.$me['friends_count'].' ,statuses_count='.$me['statuses_count'].' ,created_at='.strtotime($me['created_at']).' ,verified='.intval($me['verified']).',verified_type='.$me['verified_type'].' ,description="'.$dbm->e($me['description']).'"'); //登录 $this->user->login($me['id'] . "@weibo.com", "no"); $this->network->get_user_by_id($this->user->info->id,true); $this->redirect('home'); }else{ //登录 $this->user->login($me['id'] . "@weibo.com", "no"); $this->network->get_user_by_id($this->user->info->id,true); $this->redirect('home'); } }else { $code_url = $o->getAuthorizeURL($C->SITE_URL . 'login/sina'); $this->network->get_user_by_id($this->user->info->id,true); $this->redirect($code_url); } } else { //未授权时跳转 // echo '4';die; $code_url = $o->getAuthorizeURL($C->SITE_URL . 'login/sina'); $this->redirect($code_url); } ?>
时不我待,不负韶华!立刻行动!不吃学习的苦就会吃生活的苦!
原文地址:https://www.cnblogs.com/zrp2013/p/3074773.html