Today's Question:  What does your personal desk look like?        GIVE A SHOUT

PHP to get access token for Sina Weibo app

  Pi Ke        2013-05-16 12:07:39       18,462        2    

Previously I wrote two articles about getting access token for Facebook and Twitter apps using PHP. Today I will write one more article about getting access token for Sina Weibo app using PHP.

OAuth 2.0 is now the authorization mechanism of Sina Weibo API. The API authorization process is similar to the process of Twitter. It has basically two steps: 1. Authorization; 2. Get access token.

1. Create an app.

I hope you know how to create an app in Sina Weibo now. If not. You can access this page and it will guide you on how to create the app. After you creating the app. You should write down the App Key and App Secret. These will be used later to get authorization and access token.

2. Download SDK

Next, you need to download the SDK for PHP here. Now it's being maintained by Sina App Engine(SAE). Also the API is switched to V2 version to use the OAuth 2 authorization mechanism.

3. Write the code

Now it's show time. We will implement the two authorization steps mentioned above to get the access token.

First we need to include the SDK file downloaded which is saetv2.ex.class.php.

include_once('./lib/external/sina/saetv2.ex.class.php');  //Path to the saetv2.ex.class.php. Modify it accordingly

Next, we need to define two constants WB_KEY and WB_SKEY which are the App Key and App Secret you write down in step 1.

define( "WB_AKEY" , '[App Key]' );  //Modify accordingly
define( "WB_SKEY" , '[App Secret]' );  //Modify accordingly

Ok, continue, we will define the authentication url and callback url. Authentication url is the page which we want to authenticate the user. Callback url is the page to be redirected to after access token retrieved.

$auth_page='http://'.$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];  //Change it accordingly
$callback = 'http://'.$_SERVER["HTTP_HOST"]."/index.php"; //Change it accordingly

We will get the access token. First it will check whether the access token is already retrieved. If it's yes, then we just move ahead and retrieve user data. If not, we will then check whether the authorization is completed. If not, we will get the authorization url and then direct the user to the authorization page to ask for their authorization. If they clicked authorize, the page will be redirected to the authentication page. After authorization, a code will be generated and appended to the authentication url so that it can be used to get the access token.

if(!isset($_SESSION["access_token"])){
	 if(!isset($_REQUEST['code'])){
		//Get unauthorized request token
		$oAuth=new SaeTOAuthV2(WB_AKEY,WB_SKEY);
		//Get request token
		$aurl = $oAuth->getAuthorizeURL($auth_page);	
		header("Location:$aurl");
	}else{
		$keys = array();
		$keys['code'] = $_REQUEST['code'];
		$keys['redirect_uri'] = $auth_page;
	}
	$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
	$access_token = $o->getAccessToken('code',$keys) ;
	$_SESSION["access_token"]=$access_token;
}

The access token retrieved is an array. It has following structure:

{ "access_token":"SlAV32hkKG", "remind_in ":3600, "expires_in":3600 }

After getting the access token, we can start get user data.

if($_SESSION["access_token"]!=null){
	$client = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION["access_token"]['access_token']);
	$uid_get = $client->get_uid();
	$uid = $uid_get['uid'];
	$userObj = $client->show_user_by_id( $uid);      //The returned data is an array
	
        header("location:".$callback);     //Depend on whether you need to redirect to the callback url
}

Here is the complete code:

session_start();
include_once('./lib/external/sina/saetv2.ex.class.php');

//Define app key and app secret
define( "WB_AKEY" , '[App Key]' );
define( "WB_SKEY" , '[App Secret]' );

//Define callback URL
$auth_page='http://'.$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
$callback = 'http://'.$_SERVER["HTTP_HOST"]."/index.php";

//Get access token
if(!isset($_SESSION["access_token"])){
	 if(!isset($_REQUEST['code'])){
		//Get unauthorized request token
		$oAuth=new SaeTOAuthV2(WB_AKEY,WB_SKEY);
		//Get request token
		$aurl = $oAuth->getAuthorizeURL($auth_page);	
		header("Location:$aurl");
	}else{
		$keys = array();
		$keys['code'] = $_REQUEST['code'];
		$keys['redirect_uri'] = $auth_page;
	}
	$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
	$access_token = $o->getAccessToken('code',$keys) ;
	$_SESSION["access_token"]=$access_token;
}

//Get user data
if($_SESSION["access_token"]!=null){
	$client = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION["access_token"]['access_token']);
	$uid_get = $client->get_uid();
	$uid = $uid_get['uid'];
	$userObj = $client->show_user_by_id( $uid);
	
	header("location:".$callback);
}

PHP  ACCESS TOKEN  SINA WEIBO 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  2 COMMENTS


pavan [Reply]@ 2013-05-17 04:27:33

Great information. This articl may useful for the Php developers. Thanks for this post..!

Night Walker [Reply]@ 2013-05-17 05:23:24

I think the logic in the code is applicable to other languages as well.