The main process of OAuth Authentication and storage on Sina Weibo is detailed, and oauth is detailed. The main process of OAuth Authentication and storage on sina Weibo is described in detail. oauth is described in many articles about OAuth on the Internet, but sina itself is not described in detail, including the verification process and the main process of verifying OAuth and storage on Sina Weibo, oauth details
There are a lot of articles on OAuth on the Internet, but sina itself is not described in detail, including the verification process and storage of verified data, therefore, I wrote some detailed comments to the Twitter authentication process.
Before we start, we first create a database to save user information. Below is a basic Mysql example:
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;
Note the oauth_token and oauth_secret fields. The OAuth Authentication of sina requires the token and token_secret parameters to complete the authentication. Therefore, we need to reserve two fields to record them.
Then we need to complete the following tasks in sequence:
Initiate an authentication request to SinaAPI for registration/or logon. if the user already has an account, save the relevant data in the Session.
The OAuth-based authentication process starts from generating a website. The user is redirected to this URL for authentication. after the authentication is passed, the user will be redirected to our application server and the two authenticated parameters will be returned through URL.
Create index. php
<? Phpsession_start (); // if (isset ($ _ SESSION ['last _ key']) header ("Location: weibolist. php "); include_once ('config. php '); include_once ('weibooauth. php '); // Create a sinaOAuth object instance $ 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 ['Oss _ token' ], False, 'http: // t.yourtion.com/sina/callback.php'); // save it to the session $ _ SESSION ['Keys '] = $ keys;?> "> Use Oauth to login
Next, we need to complete the following three tasks in this file:
Verify the data in the URL
Verify the token data in the Session
Verify the secret data in the Session
If all databases are valid, we need to create a new SinaOAuth object instance. different from the previous one, we need to pass in the obtained token data as a parameter object. Then, we should be able to get an access token. the obtained data should be an array, and this access token is the only data we need to save.
Create callback. php
<? Phpsession_start (); include_once ('config. php'); include_once ('weibooauth. php'); if (! Empty ($ _ GET ['Oss _ verifier ']) &! Empty ($ _ SESSION ['keys'] ['auth _ token']) &! Empty ($ _ SESSION ['Keys '] ['Oss _ token']) {// SinaOAuth object instance. pay attention to the newly added two parameters $ sinaOAuth = new WeiboOAuth (WB_AKEY, WB_SKEY, $ _ SESSION ['Keys '] ['Oss _ token'], $ _ SESSION ['Keys'] ['Oss _ token_secret ']); // get access token $ access_token = $ sinaOAuth-> getAccessToken ($ _ REQUEST ['Oss _ verifier ']); // Save the obtained access token to the Session $ _ SESSION ['Access _ token'] = $ access_token; // obtain the user information $ user_info = $ sinaOAuth-> ge T ('account/verify_credentials '); // Print the user information mysql_connect (DATABASE_HOST, DATABASE_USER, DATABASE_PSSWORD); mysql_select_db (DATABASE_DB_NAME); // replace it with your database connection in config. in 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 = 'Sin A' 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 ['Oss _ token']. "','". $ access_toke N ['auth _ 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 ['Oss _ token']. "', oauth_secret = '". $ access_token ['auth _ token_secret ']. "'Where oauth_provider = 'sina' D oauth_uid = ". $ user_info ['id']);} $ _ SESSION ['id'] = $ result ['id']; $ _ SESSION ['username'] = $ result ['username']; $ _ SESSION ['Oss _ uid'] = $ result ['Oss _ uid']; $ _ SESSION ['Oss _ provider'] = $ result ['Oss _ provider']; $ _ SESSION ['Oss _ token'] = $ result ['Oss _ token']; $ _ SESSION ['Oss _ secret'] = $ result ['Oss _ secret']; header ('Location: update. php ') ;}} else {// The data is incomplete. go to the header ('Location: index. php ');} ?>
You can get the user id through $ user_info-> ID, and get the user name through $ user_info-> screen_name. Other information can also be obtained in the same way.
It should be noted that the parameter oauth_verifier passed back cannot be reused. if the above code has correctly output user information, you can try to refresh the page again, the page will throw an error message because oauth_verifier has been used once. To use it again, you need to initiate a new authentication request on the index. php page.
User registration
After obtaining the user information, we need to register the user information in our own database, provided that the user has not been registered in the local database.
The database link information in the code above should be changed to your own. If the user already exists in our database, we need to update the tokens field of the user, because it indicates that Twitter has generated a new tokens and the tokens in the database has expired. If the user does not exist, we need to add a new record, save the relevant data in the Session, and finally redirect back to the update. php page.
The update. php code is as follows:
Note that the SQL statements in the above code have not been verified, and you may need to modify them in actual use. Before connecting to the database, we need to verify whether the user has logged on. With the username, we can display a personalized welcome message:
<? Phpinclude_once ('config. php'); include_once ('weibooauth. php'); session_start (); if (! Empty ($ _ SESSION ['username']) {// User is logged in, redirect header ('index. php') ;}?> Use OAuth for identity authentication-Yourtion
Hello <? = $ _ SESSION ['username']?>
This is the main process of OAuth Authentication and storage, and I hope to help you. Download Code: SinaOauth
The above is all the content described in this article. I hope you will like it.
Please take a moment to share your article with your friends or leave a comment. Thank you for your support!
I have a lot of articles on OAuth on the Alibaba Cloud website, but sina itself is not described in detail, including the verification process and verification...