Sina Weibo oauth2.0 access Method (Java version)

Source: Internet
Author: User
Tags documentation oauth

Refer to:http://chuanwang66.iteye.com/blog/1436438

First, the initial experience

Sina Weibo • Open platform: http://open.weibo.com/
There are several tabs on the top page: "Home", "Web Access", "Application Development", "Development Documentation", "forum", "My Application"

"Development document" has "Platform overview", "Development Strategy" (from how to create application to application through the audit process introduction), "API documentation" (to be learned. , SDK downloads (various development kits, including PHP, Python, Java, etc.)

Second, the first application:
0. OAuth Standard
==>oauth 1.0 and OAuth 2.0
OAuth 1.0 has been settled in the IETF (Internet Engineering Task Force Internet Engineering Tasks Group), the number is RFC5894
This also marks this oauth already officially as Internet Standard protocol.
OAuth 2.0 had already begun to discuss and establish the draft. OAuth2.0 is likely to be the next generation of "User authentication and authorization" standards. Now Baidu Open platform, Tencent Open platform, and most of the open platform are used by the OAuth 2.0 agreement as a support.
OAuth (Open Licensing) is an open standard that allows a third party to apply access to private resources (such as photos, videos, contact lists) that are stored on a Web site without having to provide a user name and password to a third-party application.
==> Certification and authorization process:
The three parties involved in the process of accreditation and authorization include:
1, the service provider, the user uses the service provider to store the protected resources, such as photos, videos, contact list.
2, the user, the owner of the protected resource stored in the service provider.
3, client, to access the service provider resources of Third-party applications, usually the Web site, such as providing photo printing services site. Before the authentication process, the client requests the client identity to the service provider.

The process of authenticating and authorizing using OAuth is as follows:
The user accesses the client's Web site and wants to manipulate the resources that the user holds on the service provider.
The client requests a temporary token from the service provider.
After the service provider verifies the identity of the client, a temporary token is granted.
After the client obtains a temporary token, the user is booted to the service provider's authorization page to request authorization from the user. In this procedure, the temporary token and the client's callback connection are sent to the service provider.
The user enters the user name and password on the service provider's Web page, and then authorizes the client to access the requested resource.
Once the authorization is successful, the service provider directs the user back to the client's Web page.
The client obtains access tokens from the service provider based on the temporary token.
The service provider grants the client access token based on the temporary token and the user's authorization.
The client uses the acquired access token to access the protected resource that is stored on the service provider.

1. OAuth2 Verification Steps

Step One: Modify the configuration information for the config.properties. Open this file and fill in your information as follows:
client_id = (you apply for the application of the Appkey)
Client_sercret = (you apply for the application of the App_secret)
Redirect_uri = (callback address, the same as the online configuration, I am relatively poor, and with the client, so here is empty)
BaseURL = https://api.weibo.com/2/
Accesstokenurl = Https://api.weibo.com/2/oauth2/access_token
Authorizeurl = Https://api.weibo.com/2/oauth2/authorize

The second step opens the file in the example directory Oauth4code.java,code the following figure

Step three: Run this file, the browser will automatically open a Web page, as shown below. Enter your account number and password in it, click "Login and Authorize"

The fourth step: after landing, the browser will automatically jump to the callback page (since I do not own the site, do not fill out the callback page, so the browser will open a Third-party licensing error interface), the address bar in the "Https://api.weibo.com/2/oauth2/authorize "Code=xxxxx" in the xxx copy out, this is you want dongdong. Fill in the Eclipse's console in the following illustration and enter.

Step Fifth: Place the Acquired "Access_token" (Look up in the console output) into the Showuser.java file, do the following, and then run the program to return your personal information.
String Access_token = args[0]; modify Args[0] to the "access_token" just obtained;
String uid = args[1]; Modify ARGS[1] to the ID number of the user you just authorized;

2. First application--Get user information

Follow the steps in "two, 1." To view the information of a user, the following is a sample program with Access_token and UID set:
Java code
Package weibo4j.examples.user;

Import weibo4j. Users;
Import weibo4j. Weibo;
Import Weibo4j.examples.oauth2.Log;
Import Weibo4j.model.User;
Import weibo4j.model.WeiboException;

public class Showuser {

public static void Main (string[] args) {
String Access_token = "2.00MO7YLBSRVMWB3594DFCF0CZASBVD";
Weibo Weibo = new Weibo ();
Weibo.settoken (Access_token);
String uid = "1788217673"; This UID is the UID of cat sister.
Users um = new users ();
try {
User user = Um.showuserbyid (UID);
Log.loginfo (User.tostring ());
SYSTEM.OUT.PRINTLN (user);
catch (Weiboexception e) {
E.printstacktrace ();
}
}
}

String uid = "1788217673"; This UID is the UID of cat sister.
Where does the UID value come from, haha, through the following program:

Java code
Package weibo4j.examples.friendships;

Import weibo4j. friendships;
Import weibo4j. Weibo;
Import Weibo4j.examples.oauth2.Log;
Import Weibo4j.model.User;
Import Weibo4j.model.UserWapper;
Import weibo4j.model.WeiboException;

public class Getfollowers {

public static void Main (string[] args) {
String Access_token = "2.00MO7YLBSRVMWB3594DFCF0CZASBVD";
Weibo Weibo = new Weibo ();
Weibo.settoken (Access_token);
Friendships FM = new friendships ();
String screen_name = "Blue Rapids Island";
try {
Userwapper users = Fm.getfollowersbyname (screen_name);
For (User u:users.getusers ()) {
Log.loginfo (U.tostring ());
}
System.out.println (Users.getnextcursor ());
System.out.println (Users.getpreviouscursor ());
System.out.println (Users.gettotalnumber ());
catch (Weiboexception e) {
E.printstacktrace ();
}
}
}

The above program can get the "screen_name = Blue Rapids Island" user's All "fan (followers)" information. One of the analysis's information is:
Java code
486 INFO [2012-03-02 15:56:58] User [id=1788217673, Screenname= Hutong de cat, name= hutong de cat, province=11, city=1000, location= Beijing, Description= love life, Love sister, Love Beibei, Love 6 elder sister ~, Url=http://blog.sina.com.cn/aquadora, profileimageurl=http://tp2.sinaimg.cn/ 1788217673/50/5599615830/0, Userdomain=aquadora, Gender=f, followerscount=508, friendscount=101, statusesCount=1590 , favouritescount=0, Createdat=fri Aug 00:00:00 CST, Following=true, Verified=false, Verifiedtype=-1, allowAllAct Msg=false, Allowallcomment=true, Followme=true, avatarlarge=http://tp2.sinaimg.cn/1788217673/180/5599615830/0, Onlinestatus=0, Status=null, bifollowerscount=0, remark= Pan Xiao, LANG=ZH-CN, verifiedreason=, weihao=, statusId= 3419140201314017]
Cute cat Sister ^ ^


Three

Call Restrictions: http://open.weibo.com/wiki/Rate-limiting

Four

One example of this little friend: http://blog.csdn.net/zjl234/article/details/7339993

Related Article

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.