The automatic acquisition accesstoken of Sina Micro-blog development

Source: Internet
Author: User

First blog.

Some time ago to study the development of Sina Weibo, found that the official SDK (Java version) to obtain Accesstoken actually need to manually copy the code from the browser to the command line to obtain Accestoken, obviously very unfriendly ah, online also did not find a practical way, only their own research, Using HttpClient to write a method of automatic acquisition, the code is as follows:

Public Accesstoken Getaccesstoken () throws Weiboexception, IOException {Oauth Oauth = new Oauth ();
		String url= "Https://api.weibo.com/oauth2/authorize";
		HttpClient httpclient=new defaulthttpclient ();
		
		HttpPost post=new httppost (URL);
		
		Accesstoken Accesstoken=null;
		Initialize post data list<namevaluepair> params=new arraylist<namevaluepair> ();
		Params.add (New Basicnamevaluepair ("UserId", "Weibo account"));		
		Params.add (New Basicnamevaluepair ("passwd", "password"));
		Params.add (New Basicnamevaluepair ("Action", "Login"));
		Params.add (New Basicnamevaluepair ("Display", "Default"));
		Params.add (New Basicnamevaluepair ("Withofficalflag", "0"));
		Params.add (New Basicnamevaluepair ("Withofficalaccount", ""));
		Params.add (New Basicnamevaluepair ("Scope", ""));
		Params.add (New Basicnamevaluepair ("Ticket", ""));
		Params.add (New Basicnamevaluepair ("Isloginsina", ""));
		Params.add (New Basicnamevaluepair ("Response_type", "Code")); Params.add (New Basicnamevaluepair ("Regcallback", "https%3a%2f%2fapi.weibo.com%2f2%2foauth2%2fauthorize%3fclient_id%3d3616023689%26response_type%3dcode%26display%
		3ddefault%26redirect_uri%3dhttp%3a%2f%2f127.0.0.1%26from%3d%26with_cookie%3d "));
		Params.add (New Basicnamevaluepair ("Redirect_uri", "http://127.0.0.1"));
		Params.add (New Basicnamevaluepair ("client_id", "3616023689"));
		Params.add (New Basicnamevaluepair ("appkey62", "5pph2h"));
		Params.add (New Basicnamevaluepair ("state", ""));
		Params.add (New Basicnamevaluepair ("Verifytoken", "null"));
		
		Params.add (New Basicnamevaluepair ("from", ""));
		Urlencodedformentity entity;
		Entity=new urlencodedformentity (params, "utf-8");
		Post.setentity (entity); Post.addheader ("Referer", "https://api.weibo.com/oauth2/authorize?client_id=3616023689&redirect_uri=http://		
		
		127.0.0.1&response_type=code&state=&scope= ");
		
		HttpResponse Response=httpclient.execute (POST);
		String[] Location=response.getlastheader ("location"). ToString (). Split ("=");
		String Code=null; if (location. length>1) {code=location[1];
		Log.loginfo ("Code:" +code);
			} if (Code!=null) {Accesstoken=oauth.getaccesstokenbycode (code);
		Log.loginfo ("Access_token:" +accesstoken.getaccesstoken ());
	return accesstoken; }
Because Sina Weibo will verify the required code redirect to Redirect_uri, and HttpClient seemingly does not support, so the direct access to the response message code parameters will be an error.

I get the code by getting the location value of the response data, that is, redirecting the address. The format for this location is redirect_uri?code=xxxxxxxx, and here I am http://127.0.0.1?code=xxxxxxxx, capturing the code by intercepting the location, The code value is then passed to the Accesstoken method provided in the SDK to obtain the Accesstoken successfully.

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.