Third-party login for Sina Weibo

Source: Internet
Author: User

Because the project needs, so learn the next micro-blog of the third-party authorized login, in this record, for review.

1. Developers register and refine app information

1.1 Enter http://open.weibo.com/, login with Weibo account, perfect developer information (select individual or company)

1.2 Enter http://open.weibo.com/development, click Create App, select Mobile app, perfect information (just test, some information can be filled in first, but the Android package name, and signature need to fill in), Get Appkey, in the " Application Information "Fill in the authorization callback page: https://api.weibo.com/oauth2/default.html Cancellation Authorization callback page: https://api.weibo.com/oauth2/default.html, in the" Interface Management "Request Weibo write permission (if you need to share to Weibo, but the app needs to be approved first)

Get Android Signature Click the Signature tool to download APK, fill in the package name to generate a signature


Remember to fill in the authorization callback page


2. Get Accesstoken and openId, user information, share to Weibo 2.1 on GitHub (HTTPS://GITHUB.COM/MOBILERESEARCH/WEIBO_ANDROID_SDK Download the entire sample project as well as the corresponding APK installation package, find WEIBOSDK this project in Demo-src, import into Eclipse, select your project, Alt+enter, click Add, bring WEIBOSDK into your project



Found in Demo-src, Weibosdkdemo project under the Accesstolenkeeper.java and Constants.java these two classes, the test to the folder you want to put, modify App_key (the above application), redirect_ URL, configuring Androidmanifest.xml, adding permissions

<uses-permission android:name= "Android.permission.INTERNET"/>
    <uses-permission android:name= " Android.permission.ACCESS_WIFI_STATE "/>
    <uses-permission android:name=" android.permission.ACCESS_ Network_state "/>
<uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>



Next, start our Weibo login:

Mainactivity.java

public class Mainactivity extends Activity {/** Weibo Web authorization class, provide login and other functions * * Private Weiboauth Mweiboauth;
	/** encapsulates "Access_token", "expires_in", "Refresh_token", and provides their management functions */private Oauth2accesstoken Maccesstoken;
	/** Note: Ssohandler is valid only when the SDK supports SSO */private Ssohandler Mssohandler;
	/** Weibo user Information interface */private USERSAPI Musersapi;

	/** API/Private STATUSESAPI MSTATUSESAPI used to obtain operations such as microblogging information flow;

	Private TextView Mshowinfo;
		@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

		Setcontentview (R.layout.activity_main); 1.4 Version: New parameters are required here, the global context of the incoming application can be obtained through the activity's Getapplicationcontext method//Create a microblog instance Mweiboauth = new Weiboauth (This, Con Stants.

		App_key, Constants.redirect_url, Constants.scope);

	Initview ();
	} public void Initview () {mshowinfo = (TextView) This.findviewbyid (r.id.show_info);
	} public void Weiboauth (view view) {Startsinaweiboauth ();
		public void Sharetoweibo (view view) {if (Maccesstoken! = null) {	Instantiation of Statusapi Mstatusesapi = new Statusesapi (Maccesstoken);
			String content = "";
			Please note: This interface does not support the release of multi-graph, that is, the parameter pic_id is not available (only through the BD cooperative access, not individual application) String Photourl = "";
		Mstatusesapi.uploadurltext (content, photourl, NULL, NULL, NULL, Mlistener);
		} else {Toast ("Oauth2accesstoken is null, authorize");
	}} public void GetUserInfo (view view) {userinfo ();
		}//SSO authorization private void Startsinaweiboauth () {Mssohandler = new Ssohandler (mainactivity.this, Mweiboauth);
	Mssohandler.authorize (New Authlistener ());  } private void UserInfo () {if (Maccesstoken! = null) {Musersapi = new Usersapi (maccesstoken);//Get User Information interface long
			UID = Long.parselong (Maccesstoken.getuid ());
		Musersapi.show (UID, mlistener);
		} else {Toast ("Oauth2accesstoken is null, authorize"); }}/** * Weibo authentication authorization callback class. 1. When SSO is granted, the callback will not be executed until {@link Ssohandler#authorizecallback} is called in {@link #onActivityResult}. 2. When a non-SSO * is authorized, the callback is executed when the authorization is completed. When the authorization is successful, please save the Access_tokeN, expires_in, UID and other information into the * sharedpreferences. */class Authlistener implements Weiboauthlistener {@Override public void OnComplete (Bundle values) {//from Bundl
			E in parsing Token maccesstoken = Oauth2accesstoken.parseaccesstoken (values); if (Maccesstoken.issessionvalid ()) {//Save Token to Sharedpreferences Accesstokenkeeper.writeaccesstoken (MainActiv
				Ity.this, Maccesstoken);
			Mshowinfo.settext (Maccesstoken.tostring ()); } else {///In the following cases, you will receive Code://1. When the package name and signature of the application you are not registering on the platform;//2. When you register an application package name with an incorrect signature;//3. When you register the package name and signature on the platform with the
				The package name and signature of the app you are currently testing does not match.
				String code = values.getstring ("code"); if (!
				Textutils.isempty (code)) {code = "auth_failed" + "\nobtained The Code:" + code;
			} mshowinfo.settext (code);
		}} @Override public void OnCancel () {Toast ("weibos_auth_canceled");
		} @Override public void Onweiboexception (Weiboexception e) {Toast ("Auth exception:" + E.getmessage ()); }} Private Requestlistener Mlistener = new Requestlistener () {@Override public void OnComplete (String response) {if (!
				Textutils.isempty (response)) {//Call User#parse to parse the JSON string into the user object user = User.parse (response); Mshowinfo.settext ("name=" + User.Name + ", gender=" + User.gender + ", avatar_large=" + User.avatar_large + "...)

		.");
			}
		} @Override public void Onweiboexception (Weiboexception arg0) {//TODO auto-generated method stub Mshowinfo.settext
		(Arg0.tostring ());

	}
	};
	 /** * This function is called when SSO authorization Activity exits. * * @see {@link Activity#onactivityresult} */@Override protected void Onactivityresult (int requestcode, int result
		Code, Intent data) {Super.onactivityresult (Requestcode, ResultCode, data);
		if (Mssohandler! = null) {Mssohandler.authorizecallback (Requestcode, ResultCode, data);
	}
		;
	} private void Toast (String msg) {Toast.maketext (Mainactivity.this, MSG, Toast.length_short). Show (); }
}


SOURCE attached

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.