新浪微博SSO授權,很早就做好了,只是一直沒有時間整理博客,今天加班,晚上閒暇之時便想到整理一下。由於整個七月份很忙,加班很多。前段時間把騰訊微博的SSO認證整理好了。想在七月份翻篇之前再寫點東西。好了,不多說廢話了,下面就說說關於新浪微博SSO認證的內容。
新浪微博比較簡單,而且很方便使用。由於在騰訊微博中我有講到SSO認證的過程,這裡主要是看看新浪微博demo中的MainActivity,這個類中告訴我們如何進行新浪微博的授權,按照這個范例來做就可以的。下面是這個類的源碼
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 ([email protected]) 這位是新浪SSO認證的開發者,很優秀的開發者
*/
public class MainActivity extends Activity {
private Weibo mWeibo;
private static final String CONSUMER_KEY = "966056985";// 替換為開發者的appkey,例如"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 僅當sdk支持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);// 觸發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) {
/**
* 下面兩個注釋掉的代碼,僅當sdk支持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");// 如果支持weiboapi的話,顯示api功能演示入口按鈕
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 仍在有效期內,無需再次登錄: naccess_token:"
+ MainActivity.accessToken.getToken() + "n有效期:" + date);
} else {
mText.setText("使用SSO登錄前,請檢查手機上是否已經安裝新浪微博客戶端,目前僅3.0.0及以上微博客戶端版本支持SSO;如果未安裝,將自動轉為Oauth2.0進行認證");
}
}
@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("認證成功: rn access_token: " + token + "rn"
+ "expires_in: " + expires_in + "rn有效期:" + date);
try {
Class sso = Class
.forName("com.weibo.sdk.android.api.WeiboAPI");// 如果支持weiboapi的話,顯示api功能演示入口按鈕
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, "認證成功", 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);
/**
* 下面兩個注釋掉的代碼,僅當sdk支持sso時有效,
*/
if (mSsoHandler != null) {