Android integrated Sina Weibo third-party login method _android

Source: Internet
Author: User
Tags getmessage string format

This article describes the Android integrated Sina Weibo Third-party login method. Share to everyone for your reference. The implementation methods are as follows:

1. Download microblogging SDK, import Micro Bo jar package two Android-support-v4.jar and Weibosdkcore.jar two packages

2. Import the COM from the SDK in the demo_src of Sina Weibo into the project

3. With the constants in the demo, the main parameters are set, the parameters inside to change to their own parameters.

4. Write code, the main steps are as follows:

Copy Code code as follows:
Initializing a Microblog object
Mweiboauth = new Weiboauth (this, Constants.app_key, Constants.redirect_url, Constants.scope);
Sinaauthorwebview.loadurl ("https://open.weibo.cn/oauth2/authorize?scope=email&redirect_uri=http:// www.sina.com&state=flashmemoAndroid&forcelogin=true&display=mobile&client_id=2529326755 ");
Get code
Mweiboauth.authorize (New Authlistener (), Weiboauth.obtain_auth_code);
Step two: Get Token by Code
Fetchtokenasync (Mcode, Weibo_demo_app_secret);

5. The main core code is as follows:

Copy Code code as follows:
/**
* Micro-Blog Authentication authorization callback class.
*/
Class Authlistener implements Weiboauthlistener {


@Override
public void OnComplete (Bundle values) {
if (null = values) {
Get code failed
Return
}
String code = values.getstring ("code");
if (Textutils.isempty (code)) {
Get code failed
Return
}
Get code succeeded
Mcode = code;
Get code success, step two: Get Token by code
Fetchtokenasync (Mcode, Weibo_demo_app_secret);
}

@Override
public void OnCancel () {
LOG.E ("Loginactivity", "Sinaauth Cancel");
If you cancel the authorization, you can transfer to the login page and so on
}

@Override
public void Onweiboexception (Weiboexception e) {
LOG.E ("Loginactivity", "Sinaauth exception:" + E.getmessage ());
}
}

/**
* The Handler updates the UI with a callback corresponding to {@link Requestlistener}.
*/
Private Handler Mhandler = new Handler () {
public void Handlemessage (msg) {


Switch (msg.what) {
Case Msg_fetch_token_success:
Show Token
String date = new
SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss"). Format (
New Java.util.Date (Maccesstoken.getexpirestime ()));
String format =
GetString (r.string.weibosdk_demo_token_to_string_format_1);


Get Tocken Success
Break


Case msg_fetch_token_failed:
Toast.maketext (Wbauthcodeactivity.this,
R.string.weibosdk_demo_toast_obtain_token_failed,
Toast.length_short). Show ();
Get Tocken Failed
Break
Default
Break
}
};
};


/**
* Get Token asynchronously.
*
* @param authcode
* Authorization code, the code is one-time, can only be obtained once Token
* @param Appsecret
* App_secret of the application, be sure to take good care of your own app_secret,
* Do not expose directly to the program, here only as a demo demo.
*/
public void Fetchtokenasync (string authcode, String appsecret) {
/*
* linkedhashmap<string, string> requestparams = new
* linkedhashmap<string, string> ();
* Requestparams.put (wbconstants.auth_params_client_id,
* Constants.app_key);
* Requestparams.put (Wbconstants.auth_params_client_secret,
* Appsecretconstants.app_secret);
* Requestparams.put (Wbconstants.auth_params_grant_type,
* "Authorization_code");
* Requestparams.put (Wbconstants.auth_params_code, Authcode);
* Requestparams.put (Wbconstants.auth_params_redirect_url,
* Constants.redirect_url);
*/
Weiboparameters requestparams = new Weiboparameters ();
Requestparams.add (wbconstants.auth_params_client_id, Constants.app_key);
Requestparams.add (Wbconstants.auth_params_client_secret, Appsecret);
Requestparams.add (Wbconstants.auth_params_grant_type,
"Authorization_code");
Requestparams.add (Wbconstants.auth_params_code, Authcode);
Requestparams.add (Wbconstants.auth_params_redirect_url,
Constants.redirect_url);

/**
* Please note: {@link Requestlistener} The callback is run in the background thread, so you need to use Handler to match the update
* UI.
*/
Asyncweiborunner.request (Oauth2_access_token_url, Requestparams,
"POST", new Requestlistener () {
@Override
public void OnComplete (String response) {
LOGUTIL.D (TAG, "Get token Response:" + Response);

Oauth2accesstoken token = Oauth2accesstoken
. Parseaccesstoken (response);
if (token!= null && token.issessionvalid ()) {

LOGUTIL.D (TAG, "success!" + token.tostring ());

Maccesstoken = token;
Get token success, you can make the appropriate processing

Notify UI Changes
Mhandler.obtainmessage (msg_fetch_token_success)
. Sendtotarget ();

@Override
public void Oncomplete4binary (
Bytearrayoutputstream Responseos) {
LOGUTIL.E (TAG, "oncomplete4binary ...");
Mhandler.obtainmessage (msg_fetch_token_failed)
. Sendtotarget ();
}

@Override
public void Onioexception (IOException e) {
LOGUTIL.E (TAG, "onioexception:" + e.getmessage ());
Mhandler.obtainmessage (msg_fetch_token_failed)
. Sendtotarget ();
}

@Override
public void OnError (Weiboexception e) {
LOGUTIL.E (TAG, "weiboexception:" + e.getmessage ());
Mhandler.obtainmessage (msg_fetch_token_failed)
. Sendtotarget ();
}
});
}


The main is to rewrite complete corresponding method can be. Handler is mainly to notify the interface to make changes
Parameters for the parameter invocation problem of the UI handler
Copy Code code as follows:
/** get Token Success or failure message * *
private static final int msg_fetch_token_success = 1;
private static final int msg_fetch_token_failed = 2;

I hope this article will help you with your Android program.

Related Article

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.