Development of Weibo open platform (i) Get Access_token

Source: Internet
Author: User
Tags oauth

Access to micro-blogging open platform development because of job needs. Make this record easy to check.

First, prepare.

1. Weibo account. Registration is easy.

2. Weibo account becomes a developer.

Login to the Weibo open platform to login to your registered account, then go to the Admin Center to complete the developer basic information and identity authentication.

At this point, you need to look at the documentation for the Weibo open platform to learn some of the normative terms of this open platform.

Each link takes a look and, by the way, a look at the microblog API documentation, you'll find that there's an introduction to some of the interface calls that need to get access_token. This article is to achieve this goal.

Second, create the application.

For example, we want to develop a website, and then in this site to connect Weibo open platform, call open platform interface. Then we need to create an app after we sign in to the Weibo open platform.

Once you've created your app, you'll get app key and app Secret. And then we're holding a message that calls Sina Weibo's auth2.0 certification to get Access_token.

After we get the Access_token, we can invoke the interface we need, such as sending a tweet and so on.

1. Create a reference place. Open platform-click on the navigation menu "Micro link" above. The concept of a micro link here is also a noun for this open platform. Specific meanings can be seen in related documents.

2. What types of apps are created?

Feel free to look at your situation. When testing the creation of anything can be, try, rest assured. I'm creating a Web page reference, and I don't need to fill in too much information when I find it easy to go in.

Such as:

After the creation is complete, you can see in my app:

Click in to see information about the app. For a moment we need to use the app key and app secret, and we need to configure the app authorization callback address.

Third, download the Micro-blog development SDK.

There are some examples in the development SDK that can be used as a reference. I downloaded the Java version of the weibo4j, after downloading, import development tools. Such as:

The code is more, don't panic, first find examples package under the Oauth2 package under the Oauth4code class. We need to run this class.

This class completes the OAuth operation, which includes two requests

First time:
https://api.weibo.com/oauth2/authorize?client_id=1234567890&redirect_uri=https://api.weibo.com/ oauth2/default.html&response_type=code
Second time: Https://api.weibo.com/oauth2/access_token

Note that there is a client_id in the first request and this parameter needs to be configured in Config.properties, which corresponds to the app key of the app we created above.

Similarly, there is a client_sercret in this configuration file, which corresponds to the app Sercret of the app we created above, and it's copied over and put in.

There is also a parameter Redirect_uri, more critical. When we run the Oauth4code class, the address is called back after the first request, and a code parameter is brought in.

We need to copy this parameter, type it into the console, then enter, the second request is initiated, and the request returns the Access_token we need. So here's the question, how does this address be configured?

1. In the reference created above, find the interface management--"authorization mechanism also does not have a configuration callback address place." Fill in the https://api.weibo.com/oauth2/default.html and you can do it.

2. The above link corresponds to the configuration to Config.properties, as follows:

client_id = 1234567890client_sercret = 890a2ab550cf8d7a0090ff2f19850f3aRedirect_uri = https\://api.weibo.com /oauth2/default.htmlbaseurl=https://api.weibo.com/2/accesstokenurl=https://api.weibo.com/oauth2/ access_tokenauthorizeurl=https://api.weibo.com/oauth2/authorizermurl=https\://rm.api.weibo.com/2/

Third, run the program to get Access_token.

After the second step above is complete, the configuration is configured. Run the following class:

 Public classOauth4code { Public Static voidMain (String [] args)throwsweiboexception, ioexception{oauth oauth=NewOauth (); Barebonesbrowserlaunch.openurl (Oauth.authorize ("Code")); System.out.println (Oauth.authorize ("Code")); System.out.print ("Hit Enter when it's done." [Enter]: "); BufferedReader BR=NewBufferedReader (NewInputStreamReader (system.in)); String Code=Br.readline (); Log.loginfo ("Code:" +code); Try{System.out.println (Oauth.getaccesstokenbycode (code)); } Catch(weiboexception e) {if(401 = =E.getstatuscode ()) {Log.loginfo ("Unable to get the access token."); }Else{e.printstacktrace (); }        }    }}

After running the program, after entering a row of logs in the console, the browser opens a page immediately after the jump and the page becomes the following:

Copy the code parameter value of the browser address bar. Go back to MyEclipse, paste this value, and then enter. The program continues to execute, using this code parameter, the re-initiation of the request after the final success can be obtained Access_token

The entire process effect log:

Iv. summarize the OAuth authorization process in this example.

Two requests

First time:

HTTPS://api.weibo.com/oauth2/authorize?client_id=123050457758183&redirect_uri=http:/ /www.example.com/response&response_type=code

After agreeing to authorize, it redirects:

// HTTP is redirected after consent authorization ://Www.example.com/response&code=CODE

Second time:

Start the request again according to the code above. Get Access_token

Full text reference: http://open.weibo.com/

Some problems are encountered, mainly because the authorization address configuration is incorrect. According to the example configuration in this article, the program can be run successfully. Get Access_token.

Development of Weibo open platform (i) Get 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.