Looking for a long time on the internet did not find a better Android microblogging client can provide learning ... Some of them are very old ... Friction friction ... Completely keep up with the pace of the times. Rage, Vio Weibo was born.
Duang ...
... Sina Weibo currently uses the authorization mechanism, has completely abandoned OAuth1.0, turns to OAuth2.0. So how do OAuth2.0 certification?
First, we understand that oauth2.0:oauth2.0 is a completely new protocol that is not backwards compatible with previous versions, but the OAuth2.0 's overall architecture is the same as the previous OAuth schema.
OAuth2.0 certification process:
(1) After the user opens the client, the client asks the user to grant authorization
(2) The user agrees to grant the client authorization
(3) The client uses the authorization obtained in the previous step to request a token from the authentication server
(4) After the authentication server authenticates the client, it confirms the error and agrees to issue the token.
(5) The client uses tokens to request resources from the resource server
(6) The resource server confirms that the token is correct and agrees to open the resource to the client
The OAuth2.0 client licensing model includes:
① Authorization Code mode (authorization code)
② simplified mode (implicit)
③ Password Mode (resource owner password credentials
④ client mode (credentials)
Authorization Code mode:
, user access client, client pointing to Server (
parameter: Response_type: Indicates authorization type, mandatory option, where the value is fixed to "code"
client_id: Indicates the ID of the client, required option ( Sina Weibo App_key)
Redirect_uri: Indicates the redirect URI, optional (default URL provided by Sina Weibo)
Scope: Represents the permission range for the request, optionally (reference Sdkdemo)
State: Indicates the current status of the client. Any value can be specified, and the authentication server returns the value intact.
-User chooses whether to authorize
, after obtaining the user, directs the user to Redirect_uri, returns the authorization Code
, which is not visible to the user, and uses the server authorization code to request a access_token to the server. The Br>-> authentication server checks the authorization code and the redirect URI without error, and returns the parameter (
parameter: Access_token: Indicates an access token, required option.
Token_type: Represents the token type, which is case insensitive and must be an option, either bearer type or Mac type.
Expires_in: Indicates the expiration time, in seconds. If this argument is omitted, the expiration time must be set in other ways.
Refresh_token: Represents an update token that is used to obtain the next access token, which can be selected.
Scope: Represents a permission range, which can be omitted if the scope of the client request is consistent.
This is how we use it: the other three types are not introduced here: (interested in: http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html)
Let's take a look at my vio micro-blog certification:
First: Open Sina Weibo open platform (http://open.weibo.com/) perfect personal information creation application ...
(If you are creating a second app, click Micro-Connect, the mobile app is OK)
Android signature needs to get app_signatures.apk input package name in now Sdk_demo.: https://github.com/sinaweibosdk/weibo_android_sdk
It is worth noting that the advanced settings need to be set as follows (of course, if the big God is automatically avoided):
The application process is here, the above is the point of attention. We recommend you to consider the demo, the benefits of a lot.
New vio Weibo project, import the following. so files and Weibosdkcore.jar files in the Weibo SDK
I do not like the project relies on, so between the cuff. If you can accept it, set weibosdk as a dependency directly.
new AuthInfo(this, WeiBoConfig.APP_KEY, WeiBoConfig.REDIRECT_URL, WeiBoConfig.SCOPE); new SsoHandler(this, authInfo); Button btnLogin = (Button) findViewById(R.id.btn_login); btnLogin.setOnClickListener(new OnClickListener() { @Override publicvoidonClick(View v) { ssoHandler.authorize(new AuthListener()); } });
Class Authlistener implements weiboauthlistener{PrivateOauth2accesstoken Accesstoken; @Override Public voidOnCancel () {Log.DTAG,"Authorization Cancellation"); } @Override Public voidOnComplete (Bundle arg0) {Accesstoken=Oauth2accesstoken.Parseaccesstoken (arg0);if(Accesstoken.Issessionvalid ()) {//Determine if Accesstoken is valid Log.DTAG,"UID =" +Accesstoken.Getuid ());Log.DTAG,"token =" +Accesstoken.GetToken ());Log.DTAG,"Refreshtoken =" +Accesstoken.Getrefreshtoken ());Log.DTAG,"Expirestime =" +Accesstoken.Getexpirestime ()); }Else{Log.DTAG,"Accesstoken Invalid");//In the following cases, you will receive Code: //1. When you are not registering the package name and signature of the application on the platform; //2. When you register the application package name and the signature is incorrect; //3. When the package name and signature that you registered on the platform do not match the package name and signature of the app that you are currently testing. StringCode=arg0.GetString ("Code"); Code=Textutils.IsEmpty (Code)? "Unknown Code": Code;Log.DTAG,"Not getting a valid Accesstoken, error code code =" +code); }} @Override Public voidOnweiboexception (Weiboexception arg0) {Log.DTAG,"Exception occurred:" +arg0.GetMessage ()); } }
@Override protectedvoidonActivityResult(intint resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // SSO 授权回调 // 重要:发起 SSO 登陆的 Activity 必须重写 onActivityResult ifnull) { ssoHandler.authorizeCallBack(requestCode, resultCode, data); } }
This is the easiest way to get authorization ... I'm lazy, I call the SDK directly. Do not spray.
Ssohandler.authorize (New Authlistener ()); This way by default calls the microblog client authorization, if the user does not have the microblogging client on the mobile phone, then the practical Web method authorizes.
The SSO licensing chapter of the Vio Weibo (Android) development Diary