Android uses Sina Weibo SSO authorization

Source: Internet
Author: User
Tags date auth getmessage valid

Sina Weibo SSO authorization, very early do, just have no time to organize blog, today overtime, evening leisure time will think of finishing. Because the whole July is very busy, many overtime. Some time ago to the Tencent Micro-BO SSO certification organized. I want to write something before I flip it in July. OK, no more nonsense, the following is to say about the Sina microblogging SSO authentication content.

Sina Weibo is relatively simple and easy to use. Because I have talked about the process of SSO authentication in Tencent Weibo, this is mainly to look at the mainactivity in the Sina Weibo demo, this class tells us how to carry on the authorization of Sina Weibo, follow this example to do can. The following is the source code for this class

Package Com.weibo.sdk.android.demo;

Import Java.text.SimpleDateFormat;

Import android.app.Activity;

Import android.content.Intent;

Import Android.os.Bundle;

Import Android.util.Log;

Import Android.view.Menu;

Import Android.view.View;

Import Android.view.View.OnClickListener;

Import Android.widget.Button;

Import Android.widget.TextView;

Import Android.widget.Toast;

Import Com.weibo.sdk.android.Oauth2AccessToken;

Import Com.weibo.sdk.android.Weibo;

Import Com.weibo.sdk.android.WeiboAuthListener;

Import Com.weibo.sdk.android.WeiboDialogError;

Import com.weibo.sdk.android.WeiboException;

Import Com.weibo.sdk.android.keep.AccessTokenKeeper;

Import Com.weibo.sdk.android.sso.SsoHandler;

Import com.weibo.sdk.android.util.Utility;

/**

*

* @author Liyan (liyan9@staff.sina.com.cn) This is the Sina SSO certified developer, very good developer

*/

public class Mainactivity extends activity {

Private Weibo Mweibo;

private static final String Consumer_key = "966056985";//Replace with developer's appkey, such as "1646212860";

private static final String Redirect_url = "http://www.sina.com";

Private Button authbtn, apibtn, ssobtn, cancelbtn;

Private TextView Mtext;

public static Oauth2accesstoken Accesstoken;

public static final String TAG = "SINASDK";

/**

* Ssohandler is only valid when the SDK supports SSO

*/

Ssohandler Mssohandler;

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

Mweibo = Weibo.getinstance (Consumer_key, Redirect_url);

AUTHBTN = (Button) Findviewbyid (R.id.auth);

Authbtn.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View v) {

Mweibo.authorize (Mainactivity.this, New Authdialoglistener ());

}

});

ssobtn = (Button) Findviewbyid (R.id.sso);//button that triggers SSO

try {

Class SSO = Class.forName ("Com.weibo.sdk.android.sso.SsoHandler");

Ssobtn.setvisibility (view.visible);

catch (ClassNotFoundException e) {

LOG.I (TAG, "Com.weibo.sdk.android.sso.SsoHandler not Found");

}

Ssobtn.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View v) {

/**

* The following two annotated code is valid only if the SDK supports SSO

*/

Mssohandler = new Ssohandler (mainactivity.this, Mweibo);

Mssohandler.authorize (New Authdialoglistener ());

}

});

CANCELBTN = (Button) Findviewbyid (r.id.apicancel);

Cancelbtn.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View v) {

Accesstokenkeeper.clear (Mainactivity.this);

Authbtn.setvisibility (view.visible);

Ssobtn.setvisibility (view.visible);

Cancelbtn.setvisibility (view.invisible);

Mtext.settext ("");

}

});

Mtext = (TextView) Findviewbyid (r.id.show);

Mainactivity.accesstoken = Accesstokenkeeper.readaccesstoken (this);

if (MainActivity.accessToken.isSessionValid ()) {

Weibo.iswifi = Utility.iswifi (this);

try {

Class SSO = Class.forName ("Com.weibo.sdk.android.api.WeiboAPI");//If support WEIBOAPI, Show API function Demo Entry button

Apibtn.setvisibility (view.visible);

catch (ClassNotFoundException e) {

E.printstacktrace ();

LOG.I (TAG, "Com.weibo.sdk.android.api.WeiboAPI not Found");

}

Authbtn.setvisibility (view.invisible);

Ssobtn.setvisibility (view.invisible);

Cancelbtn.setvisibility (view.visible);

String date = new Java.text.SimpleDateFormat ("Yyyy/mm/dd hh:mm:ss")

. Format (new Java.util.Date (Mainactivity.accesstoken

. Getexpirestime ());

Mtext.settext ("Access_token is still in validity, no need to log in again: Naccess_token:"

+ MainActivity.accessToken.getToken () + "n valid:" + date);

} else {

Mtext.settext ("Before using SSO, check to see if Sina Weibo client is installed on the phone, only 3.0.0 and above microblogging client version support SSO, if not installed, automatically to Oauth2.0 for authentication");

}

}

@Override

public boolean Oncreateoptionsmenu (Menu menu) {

Getmenuinflater (). Inflate (R.menu.activity_main, menu);

return true;

}

Class Authdialoglistener implements Weiboauthlistener {

@Override

public void OnComplete (Bundle values) {

String token = values.getstring ("Access_token");

String expires_in = values.getstring ("expires_in");

Mainactivity.accesstoken = new Oauth2accesstoken (token, expires_in);

if (MainActivity.accessToken.isSessionValid ()) {

String date = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss")

. Format (new Java.util.Date (Mainactivity.accesstoken

. Getexpirestime ());

Mtext.settext ("Certified success: RN Access_token:" + token + "RN")

+ "expires_in:" + expires_in + "RN is valid for:" + date);

try {

Class SSO = Class

. forname ("Com.weibo.sdk.android.api.WeiboAPI")//If WEIBOAPI is supported, Show API function Demo Entry button

Apibtn.setvisibility (view.visible);

catch (ClassNotFoundException e) {

E.printstacktrace ();

LOG.I (TAG, "Com.weibo.sdk.android.api.WeiboAPI not Found");

}

Cancelbtn.setvisibility (view.visible);

Accesstokenkeeper.keepaccesstoken (Mainactivity.this,

Accesstoken);

Toast.maketext (Mainactivity.this, "Certified successful", Toast.length_short)

. Show ();

}

}

@Override

public void OnError (Weibodialogerror e) {

Toast.maketext (Getapplicationcontext (),

"Auth Error:" + e.getmessage (), Toast.length_long). Show ();

}

@Override

public void OnCancel () {

Toast.maketext (Getapplicationcontext (), "Auth Cancel",

Toast.length_long). Show ();

}

@Override

public void Onweiboexception (Weiboexception e) {

Toast.maketext (Getapplicationcontext (),

"Auth Exception:" + E.getmessage (), Toast.length_long)

. Show ();

}

}

@Override

protected void Onactivityresult (int requestcode, int resultcode, Intent data) {

Super.onactivityresult (Requestcode, ResultCode, data);

/**

* The following two annotated code is valid only if the SDK supports SSO

*/

if (Mssohandler!= null) {

Mssohandler.authorizecallback (Requestcode, ResultCode, data);

}

}

}

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.