欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

新浪微博OAuth認(rèn)證和儲存的主要過程詳解

 更新時間:2015年03月27日 11:06:40   作者:Yourtion  
本文給大家介紹的是參考Twitter的認(rèn)證過程實現(xiàn)的新浪微博OAuth認(rèn)證和儲存的主要過程詳解

網(wǎng)上很多關(guān)于OAuth的文章,但是包括sina本身都都沒有詳細(xì)的的介紹,包括驗證過程和驗證后數(shù)據(jù)的儲存,所以參考了Twitter的認(rèn)證過程寫下一些詳細(xì)的注釋代碼。

在我們開始前,我們先建立一張數(shù)據(jù)庫來保存用戶信息,下面是一個基本的 Mysql 的例子:

CREATE TABLE `oauth_users` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `oauth_provider` VARCHAR(10),
  `oauth_uid` text,
  `oauth_token` text,
  `oauth_secret` text,
  `username` text,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

注意 oauth_token 和 oauth_secret 這兩個字段。sina的 OAuth 認(rèn)證需要 token 和 token_secret 兩個參數(shù)來完成認(rèn)證,所以我們需要預(yù)留兩個字段來記錄他們。

然后我們需要依次完成以下工作:

向 SinaAPI發(fā)起認(rèn)證申請 注冊/或者登錄,如果用戶已經(jīng)有帳號的情況下 將相關(guān)數(shù)據(jù)保存在 Session 中

基于 OAuth 的認(rèn)證流程從生成一個網(wǎng)址開始。用戶被重定向到該網(wǎng)址要求認(rèn)證,認(rèn)證通過后,會重定向到我們的應(yīng)用服務(wù)器,并會將兩個認(rèn)證后的參數(shù)通過 URL 方式傳回。

建立index.php

<?php
session_start();
//if( isset($_SESSION['last_key']) )
  header("Location: weibolist.php");
include_once( 'config.php' );
include_once( 'weibooauth.php' );
// 創(chuàng)建 sinaOAuth 對象實例
$sinaOAuth = new WeiboOAuth( WB_AKEY , WB_SKEY );
$keys = $sinaOAuth->getRequestToken();
// Requesting authentication tokens, the parameter is the URL we will be redirected to
$aurl = $sinaOAuth->getAuthorizeURL( $keys['oauth_token'] ,false , 'http://t.yourtion.com/sina/callback.php');
// 保存到 session 中
$_SESSION['keys'] = $keys;
?>
<a href="<?=$aurl?>">Use Oauth to login</a>

接下來,我們還需要在這個文件中完成以下三件事:

驗證 URL 中的數(shù)據(jù)
驗證 Session 中的 token 數(shù)據(jù)
驗證 Session 中的 secret 數(shù)據(jù)

如果所有數(shù)據(jù)庫都是合法的,我們需要創(chuàng)建一個新的 SinaOAuth 對象實例,跟之前不同的是,我們要把獲取到的 token 數(shù)據(jù)做為參數(shù)傳入對象。之后,我們應(yīng)該可以獲取到一個 access token,這個獲取到的數(shù)據(jù)應(yīng)該是一個數(shù)組,這個 access token 是我們唯一需要保存起來的數(shù)據(jù)。

建立callback.php

<?php
session_start();
include_once ('config.php');
include_once ('weibooauth.php');
if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['keys']['oauth_token']) &&
  !empty($_SESSION['keys']['oauth_token']))
{
  // SinaOAuth 對象實例,注意新加入的兩個參數(shù)
  $sinaOAuth = new WeiboOAuth(WB_AKEY, WB_SKEY, $_SESSION['keys']['oauth_token'],
    $_SESSION['keys']['oauth_token_secret']);
  // 獲取 access token
  $access_token = $sinaOAuth->getAccessToken($_REQUEST['oauth_verifier']);
  // 將獲取到的 access token 保存到 Session 中
  $_SESSION['access_token'] = $access_token;
  // 獲取用戶信息
  $user_info = $sinaOAuth->get('account/verify_credentials');
  // 打印用戶信息
  mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PSSWORD);
  mysql_select_db(DATABASE_DB_NAME);
  //更換成你的數(shù)據(jù)庫連接,在config.php中
  if (isset($user_info->error) or empty($user_info['id']))
  {
    // Something's wrong, go back to square 1
    header('Location: index.php');
  } else
  {
    // Let's find the user by its ID
    $sql = "SELECT * FROM oauth_users WHERE oauth_provider='sina' AND oauth_uid=" .$user_info['id'];
    $query = mysql_query($sql);
    $result = mysql_fetch_array($query);
    // If not, let's add it to the database
    if (empty($result))
    {
      $sql = "INSERT INTO oauth_users (oauth_provider, oauth_uid, username, oauth_token, oauth_secret) VALUES ('sina', '" .
        $user_info['id'] . "', '" . $user_info['screen_name'] . "', '" . $access_token['oauth_token'] .
        "', '" . $access_token['oauth_token_secret'] . "')";
      $query = mysql_query($sql);
      $query = mysql_query("SELECT * FROM oauth_users WHERE id = ".mysql_insert_id());
      $result = mysql_fetch_array($query);
    } else
    {
      // Update the tokens
      $query = mysql_query("UPDATE oauth_users SET oauth_token = '" . $access_token['oauth_token'] .
        "', oauth_secret = '" . $access_token['oauth_token_secret'] .
        "' WHERE oauth_provider = 'sina' AND oauth_uid = " . $user_info['id']);
    }
    $_SESSION['id']=$result['id'];
    $_SESSION['username']=$result['username'];
    $_SESSION['oauth_uid']=$result['oauth_uid'];
    $_SESSION['oauth_provider']=$result['oauth_provider'];
    $_SESSION['oauth_token']=$result['oauth_token'];
    $_SESSION['oauth_secret']=$result['oauth_secret'];
    header('Location: update.php');
  }
} else
{
  // 數(shù)據(jù)不完整,轉(zhuǎn)到上一步
  header('Location: index.php');
}

?>

你可以通過 $user_info->id 來獲得用戶的 ID,通過 $user_info->screen_name 來獲取用戶名,等等,其它的信息也可以通過同樣的方式獲取。

需要重點指出的是,oauth_verifier 這個傳回來的參數(shù)不能被重用,如果上面的代碼已經(jīng)正確輸出了用戶信息,你可以試著重新刷新頁面,應(yīng)該會看到頁面會拋出一個錯誤信息,因為 oauth_verifier 已經(jīng)被我們用過一次了。要再次使用,需要到 index.php 頁面重新發(fā)起一個認(rèn)證請求。

用戶注冊

獲得了用戶信息后,現(xiàn)在我們要開始把用戶信息注冊到我們自己的數(shù)據(jù)庫中,當(dāng)然前提是用戶沒有在本地數(shù)據(jù)庫注冊過。

上面代碼中的數(shù)據(jù)庫鏈接信息要改成你自己的。如果用戶已經(jīng)存在于我們的數(shù)據(jù)庫中,我們需要更新用戶的 tokens 字段,因為這說明 Twitter 生成了新的 tokens,數(shù)據(jù)庫中的 tokens 已經(jīng)過期了。如果用戶不存在,我們需要新加一條記錄,并將相關(guān)的數(shù)據(jù)保存在 Session中,最后重定向回 update.php 頁面。

其中update.php代碼如下:

需要注意的是,上面代碼中的 SQL 沒有經(jīng)過驗證,你在實際使用的時候可能要經(jīng)過修改。連接數(shù)據(jù)庫前,我們需要先驗證一下用戶是否已經(jīng)登錄。有了用戶名,我們就可以展示一條個性的歡迎信息了:

<?php
include_once ('config.php');
include_once ('weibooauth.php');
session_start();
if(!empty($_SESSION['username'])){
  // User is logged in, redirect
  header('index.php');
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
  <title>通過 OAuth 進行身份驗證--Yourtion</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<h2>Hello <?=$_SESSION['username'] ?></h2>
</body>
</html>

這就是OAuth認(rèn)證和儲存的主要過程,希望對你有幫助。 代碼下載:SinaOauth

以上就是本文所述的全部內(nèi)容了,希望大家能夠喜歡。

請您花一點時間將文章分享給您的朋友或者留下評論。我們將會由衷感謝您的支持!

相關(guān)文章

最新評論