Software engineering notes

PHP Weibo Oauth

申請

  1. 如果你的網站純粹只是要串接微博登入, 就選擇我的網站
  2. 我的應用是給如手機登入或是如 facebook app 那樣的去申請

申請我的網站

[1] 申請

[2] 網站信息 -> 高級信息, 在安全設置裡面,填寫網站的域名: www.example.com 及 IP: 127.0.0.1

[3] 網站信息 -> 測試賬號, 設置一個管理賬號

[4] 將域名綁定到 127.0.0.1

[5] 審核

申請我的應用

在開發階段要記得在 管理中心 -> 應用信息 -> 測試帳號 新增帳號, 否則在 Oauth 時會得到 applications over the unaudited use restrictions 這樣的錯誤

注意:修改後約半小時左右時間生效

管理中心->應用信息->高級信息設置好授權回調頁(連結要與 callback_url 一樣)

否則會得到 error:redirect_uri_mismatch 這樣的錯誤訊息

補充申請手機 app

在做之前先了解登入 Oauth 流程

[1] 在頁面上放置一個登入按鈕, 而按鈕的 link 導向你自己的某一隻程式 [2] 判斷是否己登入過 (檢查 token), 如果未登入就導向微博 Oauth 頁面, 成功後即可取得 token [3] 就可以利用 token 拿你想拿的使用者資料

開始動手做 (程式碼部份)

首先下載微博 php sdk github

設定好 config 方便開發 :

$config['app_id']           = (填上 app_id)
$config['secret']           = (填上 secret)
$config['callback_url']     = (填上 callback_url)

此頁檢查 token 是否存在, 如果沒有代表未登入

引入微博 PHP SDK, 並建立物件:

require 'saetv2.ex.class.php';
$weiboOauth = new SaeTOAuthV2($config["app_id"], $config["secret"]);

取得登入及授權 url, 並且導向過去:

$weiboOauth->getAuthorizeURL($config["callback_url"])

User 會被要求登入及授權, 如果通過的話, 會導向 callback_url, 並且網址後面會帶 GET : code=一串亂數

callback_url 網址的那一隻程式

取得 token :

require 'saetv2.ex.class.php';
$weiboOauth = new SaeTOAuthV2($config["app_id"], $config["secret"]);

$code = $_GET['code'];
if ($code)
{
    $keys = array();
    $keys['code'] = $code;
    $keys['redirect_uri'] = $config["callback_url"];
    try
    {
        $token = $weiboOauth->getAccessToken('code', $keys);
    }
    catch (OAuthException $e)
    {
        log_message('info', $e->getMessage());
    }
}

建議將 token 存到 SESSION, 方便下次判斷是否已登入及授權

取得 user 資料 (將 token 帶入):

if (is_array($token))
{
    $weiboClient = new saeTClientV2($config["app_id"], $config["secret"], $token['access_token']);
    $user_data = $weiboClient->show_user_by_id($token['uid']);
    if ( ! empty($user_data['error']))
    {
        log_message('info', "Weibo Error: {$user_data['error']}");
    }
}

User data 的值如下 :

array (
  'id' => 3865801255,                    #用戶UID
  'idstr' => '3865801255',
  'class' => 1,
  'screen_name' => 'test_user',            #微博暱稱
  'name' => 'test_user',                   #友好顯示名稱,同微博暱稱
  'province' => '400',                   #省份編碼(參考省份編​​碼表)
  'city' => '4',                         #城市編碼(參考城市編碼表)
  'location' => '海外 俄罗斯',            #所在地
  'description' => '',                   #個人描述
  'url' => '',                           #用戶博客地址
  'profile_image_url' => 'http://tp4.sinaimg.cn/3865801255/50/0/1',      #自定義圖像
  'profile_url' => 'u/3865801255',
  'domain' => '',                        #用戶個性化URL
  'weihao' => '',
  'gender' => 'm',                       #性別,m--男,f--女,n--未知
  'followers_count' => 15,               #粉絲數
  'friends_count' => 23,                 #關注數
  'statuses_count' => 0,                 #微博數
  'favourites_count' => 0,               #收藏數
  'created_at' => 'Mon Oct 21 11:48:34 +0800 2013',                      #創建時間
  'following' => false,                  #是否已關注(此特性暫不支持)
  'allow_all_act_msg' => false,
  'geo_enabled' => true,
  'verified' => false,                   #加V標示,是否微博認證用戶
  'verified_type' => -1,
  'remark' => '',
  'ptype' => 0,
  'allow_all_comment' => true,
  'avatar_large' => 'http://tp4.sinaimg.cn/3865801255/180/0/1',
  'avatar_hd' => 'http://tp4.sinaimg.cn/3865801255/180/0/1',
  'verified_reason' => '',
  'follow_me' => false,
  'online_status' => 1,
  'bi_followers_count' => 1,
  'lang' => 'zh-cn',
  'star' => 0,
  'mbtype' => 0,
  'mbrank' => 0,
  'block_word' => 0,
)

登出微博 :

以下連結加上 token ($token[‘access_token’])

https://api.weibo.com/2/account/end_session.json?access_token=2.00JVVcNE0O_NXn3b7d524c0cl3ekeD

會返回同 get_user_data 的基本資料

使用不同帳號登入

如果使用者已登入過微博並且也已經 Oauth 過了, 程式將使用者導到登入 Oauth 的頁面, 如果 token 沒過期, 微博並不會再次要求登入 Oauth, 因為它會當你已經認證過了, 而直接導到帶入的 callback_url, 這是正常的, 但是有時候需要讓使用者直接切換帳號登入 Oauth 的話, 如果要求使用者先到微博登出, 那不就太不方便了嗎?

微博提供的作法是只要在授權連結上添加forcelogin參數,將forcelogin參數設置為true, 就可以達到強制再次做登入 Oauth

$weiboOauth->getAuthorizeURL($config["callback_url"]) . '&forcelogin=true';

ref : http://open.weibo.com/wiki/Direct_messages/new 官方 Q&A - 接口問題 http://www.oooink.com/blog/find/c/47 http://open.weibo.com/qa/index.php?qa=1457&qa_1=%E8%AF%B7%E9%97%AE%EF%BC%8C%E5%A6%82%E4%BD%95%E5%9C%A8%E6%9C%AC%E5%9C%B0%E6%B5%8B%E8%AF%95%E6%96%B0%E6%B5%AA%E7%9A%84%E7%99%BB%E5%BD%95%E6%8C%89%E9%92%AE%E7%99%BB%E5%BD%95%E7%AC%AC%E4%B8%89%E6%96%B9%E7%BD%91%E7%AB%99%EF%BC%9F