Baidu Third Party Login

Source: Internet
Author: User
Tags oauth

First, third-party applications to work with the account, and then follow the following methods:

At present, Baidu OAuth2.0 supports five kinds of access token process and a refresh get Accesstoken method, the third party can choose the appropriate way according to the demand:

Baidu authorized access tokens are valid, which can affect the user experience and increase the work of the developer. So the platform provides a way to guarantee that the license is valid for permanent.

    • implementation : Returns the refresh token for access Token + 10-year validity for a third-party one-month validity period.
    • The principle of implementation : The function of refresh token is to refresh the token before the expiry date to obtain a new access token.

The first step: registration Baidu Direct number application.

Can register an application by the Guardian Baidu's open platform. After that you will get an app key and an app Secret. If you have them, you can apply for permission.

Assuming your app key is "1234567890", App secret is "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Step Two: Write the code.

Add the PHP version of the SDK that gets to the OAuth to join your project. Define and assign values for the key and secret you have applied to as two variables.

For OAuth, there are a lot of details that we don't need to focus on, as long as we know a few important steps:

1. Third party initiated Baidu authorized login request, Baidu User allows authorized third-party application, Baidu will pull up the application or redirect to the third party website, and with the authorized temporary note code parameters;

2. Access Token: Credentials for user authentication and authorization. Third-party apps before calling Baidu Open API, you need to get access tokens first.

3. Through the code parameter plus AppID and Appsecret, etc., through the API in exchange for Access_token;

4. Interface calls are made through Access_token to obtain basic data resources for the user or to help the user achieve basic operations.

4. Obtaining an unauthorized Access_token this unauthorized access_token is equivalent to the release strip, which means that Baidu allows you to start gaining access to the user.

5. Based on the content of this access_token, get a URL address, which is where you want to log in to your app's user input username and password. Note that this URL belongs to the callback address created by Baidu for your application.

6. Users enter their user name and password in the above login interface, after successful login, you can obtain the authorized access KEY. This access key contains the user's multiple login information (including nickname, user OpenID, etc.), where the nickname refers to the user's name displayed on Baidu, not the user's login name.

Instance code:

1. Call Authorization page: login.php

//Baidu third-party login functionfunctionBaidu () {require_once(' u/baidu/baidu.php '); $clientId= ' 1234567890 '; $clientSecret= ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '; $redirectUri= ' Http://www.bookask.com:8080/?a=login&b=callback&c=baidu '; $domain= '. Book.com '; $baidu=NewBaidu ($clientId,$clientSecret,$redirectUri,NewBaiducookiestore ($clientId)); //get the user ID and user name    $user=$baidu-Getloggedinuser (); //Baidu third-party login URL address and exit URL address    if($user) {      $LOGOUTURL=$baidu->getlogouturl (' http://robin928.sinaapp.com/demos/website/logout_callback.php?u= ').UrlEncode(Baiduutils::Getcurrenturl ())); Header("Location:".)$LOGOUTURL); } Else {      $LOGINURL=$baidu->getloginurl (', ' popup ')); Header("Location:".)$LOGINURL); }}

2. Callback Program callback.php

//Baidu Third party callback functionfunctionBaidu () {require_once(' u/baidu/baiduapi.inc.php '); if($user) {            $apiClient=$baidu-Getbaiduapiclientservice (); $profile=$apiClient->api ('/rest/2.0/passport/users/getinfo ',Array(' Fields ' = ' userid,username,sex,birthday ')); Var_dump($profile); //third-party landing data processing started        $userid=$profile[' UserID ']; $sql= "SELECT * from ' ub01 ' where ub01004 = '$userid‘"; $chek= _selectone ($sql) ? _selectone ($sql) :Array(); if(!Empty($chek)){            $_session[' UID ']=$res[' ua01001 ']; Echo' Authorized Success '; //Header ("Location:?");}Else{            Echo' Authorization failed '; }    }Else{        Echo"Authorization Failed"; }Exit;}

3, Php-sdk-1.0.rar Package decompression core file, pay attention to the path problem.

Baidu Third Party Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.