Android integrated sina weibo third party login methods

  • 2020-06-15 10:10:37
  • OfStack

An example of this article describes how Android integrates third party login with sina weibo. Share to everybody for everybody reference. Specific implementation methods are as follows:

1. Download sdk for Microblog and import two packages of jar for microblog: ES6en-ES7en-v4.jar and ES10en.jar

2. Import the com in demo_src in sina weibo into the project

3. constants in demo is mainly used for parameter setting. Change the parameters inside to your own parameters.

4. To write code, the main steps are as follows:

//  Initializes the weibo 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");
// To obtain code
mWeiboAuth.authorize(new AuthListener(), WeiboAuth.OBTAIN_AUTH_CODE);
// The first 2 Step: Code To obtain Token
fetchTokenAsync(mCode, WEIBO_DEMO_APP_SECRET);

5. The main core codes are as follows:

/**
* Weibo authentication authorization callback class.
*/
class AuthListener implements WeiboAuthListener {
@Override
public void onComplete(Bundle values) {
if (null == values) {
// To obtain code failure
return;
}
String code = values.getString("code");
if (TextUtils.isEmpty(code)) {
// To obtain code failure  
return;
}
// To obtain code successful
mCode = code;
// To obtain code Successful, the first 2 Step: Code To obtain Token
fetchTokenAsync(mCode, WEIBO_DEMO_APP_SECRET);
} @Override
public void onCancel() {
Log.e("LoginActivity", "sinaAuth cancel");
// If you unauthorize, you can tune to the login page and so on
} @Override
public void onWeiboException(WeiboException e) {
Log.e("LoginActivity", "sinaAuth exception :" + e.getMessage());
}
} /**
* the Handler Cooperate with {@link RequestListener} The corresponding callback is updated UI .
*/
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_FETCH_TOKEN_SUCCESS:
// According to 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);
// To obtain tocken successful
break;
case MSG_FETCH_TOKEN_FAILED:
// Toast.makeText(WBAuthCodeActivity.this,
// R.string.weibosdk_demo_toast_obtain_token_failed,
// Toast.LENGTH_SHORT).show();
// To obtain tocken failure
break;
default:
break;
}
};
};
/**
* Asynchronous access Token .
*
* @param authCode
*            authorization Code the Code is 1 Subsexual, can only be acquired 1 time Token
* @param appSecret
*            Application-specific APP_SECRET Be sure to take good care of yours APP_SECRET .
*            Do not directly expose the program, only as 1 a DEMO To demonstrate.
*/
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 corresponding callback is run in the background thread, Therefore, it needs to be used Handler To match the updates.
* 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;
// To obtain token Successful, can make corresponding processing // notice UI change
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 method is to rewrite the corresponding method of complete. handler is mainly about notification interface changes
The argument to UI Handler calls the argument to the problem
/**  To obtain  Token  A success or failure message  */
private static final int MSG_FETCH_TOKEN_SUCCESS = 1;
private static final int MSG_FETCH_TOKEN_FAILED = 2;

Hopefully, this article has been helpful in your Android programming.


Related articles: