/** * 从SharedPreferences读取accessstoken * * @param context * @return Oauth2AccessToken */ public static Oauth2AccessToken readAccessToken(Context context) { Oauth2AccessToken token = new Oauth2AccessToken(); SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND); token.setToken(pref.getString("token", "")); token.setExpiresTime(pref.getLong("expiresTime", 0)); return token; }
/** * 保存accesstoken到SharedPreferences * * @param context Activity 上下文环墄1�7 * @param token Oauth2AccessToken */ public static void keepAccessToken(Context context, Oauth2AccessToken token) { SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND); Editor editor = pref.edit(); editor.putString("token", token.getToken()); editor.putLong("expiresTime", token.getExpiresTime()); editor.commit(); }
public void share() { accessToken = AccessTokenKeeper.readAccessToken(context); if (!accessToken.isSessionValid()) { Toasts.toast(context, "第一次使用,需要进行授权"); weibo = Weibo.getInstance("1587758551", URL_STRING); weibo.authorize(context, new AuthDialogListener()); } else { shareXinLang(); } }
/* static public WeiboFunction getInstance(){ if(weiboFunction==null){ weiboFunction = new WeiboFunction(); } return weiboFunction; } */ public static boolean isBound() { if (!Util.isEmpty(Login.sinaWeiboToken) && !Util.isEmpty(Login.sinaExpiresIN)) { access_token = Login.sinaWeiboToken; expires_in = Login.sinaExpiresIN; accessToken = new Oauth2AccessToken(access_token, expires_in); if (!accessToken.isSessionValid()) { accessToken = null; } } return accessToken != null; }
public static void authorize(Context context) { if (accessToken != null) return; if (!Util.isEmpty(Login.sinaWeiboToken) && !Util.isEmpty(Login.sinaExpiresIN)) { access_token = Login.sinaWeiboToken; expires_in = Login.sinaExpiresIN; accessToken = new Oauth2AccessToken(access_token, expires_in); if (!accessToken.isSessionValid()) { accessToken = null; } } WeiboParameters parameters = new WeiboParameters(); parameters.add("forcelogin", "true"); com.weibo.sdk.android.util.Utility.isWifi(context); weibo.startDialog( context, parameters, new WeiboAuthListener() { // weibo.authorize(context, new WeiboAuthListener() { @Override public void onComplete(Bundle bundle) { access_token = bundle.getString("access_token"); expires_in = bundle.getString("expires_in"); accessToken = new Oauth2AccessToken(access_token, expires_in); try { SendToken(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onWeiboException(WeiboException e) { clearToken(); } @Override public void onError(WeiboDialogError weiboDialogError) { clearToken(); } @Override public void onCancel() { clearToken(); } }); }
/** * 构造函数,使用各个API接口提供的服务前必须先获取Oauth2AccessToken * * @param accesssToken Oauth2AccessToken */ public SinaWeiboAPI(Oauth2AccessToken oauth2AccessToken) { this.oAuth2accessToken = oauth2AccessToken; if (oAuth2accessToken != null) { accessToken = oAuth2accessToken.getToken(); } }
/** * 构造函数,使用各个API接口提供的服务前必须先获取Oauth2AccessToken * * @param accesssToken Oauth2AccessToken */ public WeiboAPI(Oauth2AccessToken oauth2AccessToken) { this.mOAuth2accessToken = oauth2AccessToken; if (mOAuth2accessToken != null) { mAccessToken = mOAuth2accessToken.getToken(); } }