The realization of the function of OAuth2.0 website (i) Get user authorization and Token access token

Source: Internet
Author: User
Tags curl iqiyi

When landing some Web sites, you can choose to login for third-party landing, such as micro-blog landing, to Iqiyi art for example, into the home page, click "Login", will pop up the login box:

In addition to this site landing, you can also choose other third-party landing, such as Weibo landing, QQ landing, landing and so on.

After the choice of Weibo login, Iqiyi will apply to the user to authorize the use of Weibo login (when the user has landed on the micro-blog will directly apply for authorization, when the user does not log in will prompt users to log in Weibo):

The URL for this prompt window is: https://api.weibo.com/oauth2/authorize?scope=&redirect_uri=http%3A%2F%2Fpassport.iqiyi.com% 2fapis%2fthirdparty%2fncallback.action%3ffrom%3d2&display=default&client_id=1925825497

When the connection is selected, jump to the login page, prompting the user to log in using Sina Weibo and require users to use Sina Weibo to bind to the login account:

Record the implementation process of using Sina Weibo one-click login on your website.

① Landing Sina Development platform:http://open.weibo.com/;

② Select "My Apps"--"Admin Center"-"perfect developer Info",

③ Perfect Information (website fill your online website)--Submit--verification mail--verification completed;

④ Select "Micro-connect"--"website access"--"Access Now":

Add a new website (your online site):

⑤ add meta tags to the homepage of the website as prompted to verify and add:

⑥ Select "Deep Deployment"--"Weibo login":

⑦ next read the Weibo OpenAPI document: Description of the Sina Weibo licensing mechanism

The OAuth2.0 process is as follows:

Where client refers to a third-party application, Resource owner refers to the user, Authorization server is our authorization server, and Resource server is the API server.

Weibo offers a range of OAuth2.0 interfaces:

Authorization process for Web sites:

⑧ Create a test page in the site that directs the user to the address that needs to be authorized ( get user authorization , interface document address: Http://open.weibo.com/wiki/Oauth2/authorize):https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI:

<a href= "https://api.weibo.com/oauth2/authorize?client_id=4*******&redirect_uri=http://www.ofim.com.cn/ Index/response/response&response_type=code "> Login </a>

Visit this page (click "Login") and when my Weibo is logged in, I get:

When Weibo is not logged in, click "Login (<a href=" https://api.weibo.com/oauth2/authorize?client_id=4*******&redirect_uri=http:// Www.****.cn/index/response/response&response_type=code "> Login </a>)", Get:

Select "Connect" or "connect" after logging in, get the authorization to jump to the authorization callback page (Redirect_uri) after the license is successful:

A code parameter is returned in the URL, and the data description is returned:

⑨ in exchange for Access Token

Sina's interface address is:https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE

The authorization callback address set above is: Http://www.****.cn/index/response

So add the response method to the index controller and use post to request the interface address:

   Public functionresponse () {$code= I (' Code '); $register _url= ' Http://www.****.cn/index/register '; $url= ' Https://api.weibo.com/oauth2/access_token '; $data= ' client_id=4***&client_secret=f*********&grant_type=authorization_code&redirect_uri= '.$register _url.‘ &code= '.$code; $curl=Curl_init (); curl_setopt ($curl, curlopt_post,1);//Post Modecurl_setopt ($curl, Curlopt_postfields,$data);//set the parameters of the postcurl_setopt ($curl, Curlopt_url,$url); Curl_exec ($curl); $output= Curl_exec ($curl); Curl_close ($curl); $this-display (); }

Repeat the "login" step of the previous step to return the JSON-formatted data to the authorization callback page:

{"Access_token": "2.00b_e*******", "remind_in": "1****", "expires_in": 1****, "UID": "167*****"}

Access_token get success.

Description: The page used has landing guide page, authorization callback page.

Reference: Sina Weibo Oauth2.0 authorized access Token

The realization of the function of OAuth2.0 website (i) Get user authorization and Token access token

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.