Understanding of oau2's development document on Weibo Official Website

Source: Internet
Author: User

The client interacts with Weibo according to the following three major steps. 1) obtain authorization grant 2) use this grant to obtain access token 3) the consumer stores the token and uses it to access resources.

The client is a third-party application or consumer. Do not confuse it with end users. The final access token is not saved to the user cookie on the browser side, but saved to the client. Weibo can be understood as a service provider.

The consumer Authorization result is an authorization code (in some articles, it is called oauth_token or authorization grants), and the consumer obtains the access token (access grants) through this authorization code ).

I.
An access token can be obtained using an authorization code:
Authorization code for Web Applications)
Basic Process:

1. guide the user to the following address:
Https://api.weibo.com/oauth2/authorize? Client_id = your_client_id & response_type = Code & redirect_uri = your_registered_redirect_uri
The user triggers the operation in the browser. For example, if you click to log on to Sina Weibo, UA will send the above request to WB. WB to accept the request and load the login page to UA. For example, B. The user enters the user name and password in UA to request authenticates.
Tip 2. If the user agrees to the authorization, the logon and authorization page is the Weibo page. The agreed result is the page of the callback client. The URL is your_registered_redirect_uri /? Code = code. Because your_registered_redirect_uri is the resource on the client, you can capture the code. Note that redirect_uri is optional according to the oath2 protocol. If redirect_uri is not set, multiple callback URLs registered at the time of application will be used.

Reference oau2draft http://tools.ietf.org/html/draft-ietf-oauth-v2-22

3.1.2. Redirection endpoint
After completing its interaction with the resource owner,
Authorization server directs the resource owner's User-Agent back
The client. The authorization server redirects the User-Agent to
Client's redirection endpoint previusly established with
Authorization server during the client registration process or when
Making the authorization request.

The redirection endpoint URI must be an absolute URI as defined
[Rfc3986] section 4.3. The endpoint URI may include
"Application/X-WWW-form-urlencoded" formatted
([W3C. REC-html401-19991224]) query component ([rfc3986] section 3.4 ),
Which must be retained when adding additional query parameters.
Endpoint URI must not include a fragment component.

3.1.2.2. Registration ments

The authorization server shocould require all clients to register their
Redirection URI prior to using the authorization endpoint, and must
Require the following clients to register their redirection URI:

O public clients.
O confidential clients utilizing the implicit grant type.

The authorization server shocould require the client to provide
Complete redirection uri (the client may use the "State" Request
Parameter to achieve per-request customization). The authorization
Server may allow the client to register multiple redirection Uris.
If requiring the registration of the complete redirection URI is not
Possible, the authorization server shocould require the registration
The URI scheme, authority, and path (allowing the client
Dynamically change only the query component of the redirection URI
When requesting authorization ).

3.1.2.3. dynamic configuration

If multiple redirection Uris have been registered, If only part
The redirection Uri has been registered, or if no redirection Uri has
Been registered, the client must include a redirection URI with
Authorization request using the "redirect_uri" request parameter.

When a redirection URI is wrongly ded in an authorization request,
Authorization server must compare and match the value already ed
Against at least one of the registered redirection Uris (or Uri
Components) as defined in [rfc3986] section 6, if any redirection
Uris were registered. If the client registry fails the full
Redirection Uri, the authorization server must compare the two Uris
Using simple string comparison as defined in [rfc3986] section 6.2.1.

If the authorization server allows the client to dynamically change
The query component of the redirection Uri, the client must ensure
That manipulation of the query component by an attacker cannot lead
To an abuse of the redirection endpoint as described in

Note: if not only the URL is registered, but also the redirect_uri is defined in the URL, the two values will be compared. If the URL is not registered, the redirect_uri parameter is required. I think this is the best way. Otherwise, you may find trouble for yourself. The main problem is security. hackers may replace your redirect_uri parameter.

Verification: what are the consequences of not passing redirect_url.

Token 3. In exchange for access token, note that the following URL should be sent in the Web application background using a tool similar to HTTP client. The actual situation is to pass the following parameters into the API method provided by the SDK. The SDK encapsulates all details.
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
(Client_id = your_client_id & client_secret = your_client_secret can be added to the header in Basic Mode)
Note: client_id = your_client_id & client_secret = your_client_secret is the request string:
"After the application information is saved successfully, the app key and secret key of the application are obtained. You can use these two keys to start related technical development.
The app key is the unique identifier of an application. The open platform uses the app key to identify the application.
Appsecret is the key assigned to an application. developers need to properly store the key, which is used to ensure the reliability of the application source and prevent forgery. "
Callback Return Value
{"Access_token": "slav32hkkg", "expires_in": 3600, "refresh_token": "8xloxbtzp8 "}

4.1.4. Use the obtained oauth2.0 access_token to call the API

All JSON return values of Weibo should be provided to the SDK for parsing. No application is required.

II. If the application itself is an application on Weibo, the password will not be disclosed to a third party. You can use the following simplest method to obtain the access token.

Resource owner password credentials)
Basic Process:

Callback 1. Call
Https://api.weibo.com/oauth2/access_token?
Client_id = your_client_id & client_secret = your_client_secret
& Amp; grant_type = PASSWORD & redirect_uri = your_registered_redirect_uri & username = user_name & pasword = Password
Returned value {"access_token": "slav32hkkg", "expires_in": 3600, "refresh_token": "8xloxbtzp8 "}
Token 2. Use the obtained oauth2.0 access token to call the API

 

 

3. Access Token

The access token of Sina Weibo oauth1.0 does not expire. Only the access token that is valid when the user manually revokes the authorization or Sina revokes the access permission of your app. However, the expiration time of oauth2.0 is usually one day.

Refresh token is a type of access grants. When obtaining the access token, the authentication server returns the corresponding refresh token. If the access token expires, you can use the refresh token to refresh it.

Basic Process:

1. When you call the API to return an access token that has expired, you can call oau2/ access_token and pass in refresh_token:

https://api.weibo.com/oauth2/access_token/?Client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=refresh_token&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&refresh_token=…

Returned value {"access_token": "slav32hkkg", "expires_in": 3600, "refresh_token": "8xloxbtzp8 "}

2. Use the obtained oauth2.0 access token to call the API

Note: The refresh token must be applied for separately.

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.