Back to project page Music-Share-Android.
The source code is released under:
GNU General Public License
If you think the Android project Music-Share-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.weibo.sdk.android; /*w ww . j a va2 s. co m*/ import com.weibo.sdk.android.util.Utility; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.os.Bundle; import android.util.Log; import android.webkit.CookieSyncManager; /** * * @author luopeng (luopeng@staff.sina.com.cn) */ public class Weibo { public String app_key = "";//??????appkey public String redirecturl = "";// ??????url public String oauthUrl = ""; private String scope = null; public Oauth2AccessToken accessToken = null;//AccessToken?? public String tag = ""; public static final String KEY_TOKEN = "access_token"; public static final String KEY_EXPIRES = "expires_in"; public static final String KEY_REFRESHTOKEN = "refresh_token"; public static boolean isWifi=false;//????????wifi /** * Weibo????? * @param appKey ???App Key * @param redirectUrl ???Redirect Url * @param authUrl ?????????????? * @author Xavier */ public Weibo(String appKey,String redirectUrl,String authUrl){ app_key = appKey; redirecturl = redirectUrl; oauthUrl = authUrl; } /** * Weibo????? ??Scope * @param appKey ???App Key * @param redirectUrl ???Redirect Url * @param authUrl ?????????????? * @param scope ???Scope???? * @author Xavier */ public Weibo(String appKey,String redirectUrl,String authUrl,String scope){ app_key = appKey; redirecturl = redirectUrl; oauthUrl = authUrl; this.scope = scope; } /** * ?????????appkey???????url * @param appKey ??????appkey * @param redirectUrl ????????? */ public void setupConsumerConfig(String appKey,String redirectUrl) { app_key = appKey; redirecturl = redirectUrl; } /** * ?Weibo????Oauth2AccessToken * @param token * @author Xavier */ public void setupAccessToken(Oauth2AccessToken token){ this.accessToken = token; } /** * * ????????? * @param activity ????????Context?? * @param listener WeiboAuthListener ?????????????? */ public void authorize(Context context, WeiboAuthListener listener) { isWifi=Utility.isWifi(context); startAuthDialog(context, listener); } public void startAuthDialog(Context context, final WeiboAuthListener listener) { WeiboParameters params = new WeiboParameters(); // CookieSyncManager.createInstance(context); startDialog(context, params, new WeiboAuthListener() { @Override public void onComplete(Bundle values) { // ensure any cookies set by the dialog are saved Log.d("Weibo-authorize","onComplete"); CookieSyncManager.getInstance().sync(); if (null == accessToken) { accessToken = new Oauth2AccessToken(); } accessToken.setToken(values.getString(KEY_TOKEN)); accessToken.setExpiresIn(values.getString(KEY_EXPIRES)); accessToken.setRefreshToken(values.getString(KEY_REFRESHTOKEN)); if (accessToken.isSessionValid()) { Log.d("Weibo-authorize", "Login Success! access_token=" + accessToken.getToken() + " expires=" + accessToken.getExpiresTime() + " refresh_token=" + accessToken.getRefreshToken()); listener.onComplete(values); } else { Log.d("Weibo-authorize", "Failed to receive access token"); listener.onWeiboException(new WeiboException("Failed to receive access token.")); } } @Override public void onError(WeiboDialogError error) { Log.d("Weibo-authorize", "Login failed: " + error); listener.onError(error); } @Override public void onWeiboException(WeiboException error) { Log.d("Weibo-authorize", "Login failed: " + error); listener.onWeiboException(error); } @Override public void onCancel() { Log.d("Weibo-authorize", "Login canceled"); listener.onCancel(); } }); } public void startDialog(Context context, WeiboParameters parameters, final WeiboAuthListener listener) { parameters.add("client_id", app_key); parameters.add("response_type", "token"); parameters.add("redirect_uri", redirecturl); parameters.add("display", "mobile"); if(scope!=null){ parameters.add("scope",scope); } if (accessToken != null && accessToken.isSessionValid()) { parameters.add(KEY_TOKEN, accessToken.getToken()); } String url = oauthUrl + "?" + Utility.encodeUrl(parameters); url= url.replaceAll("html+", "html"); if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) { Utility.showAlert(context, "Error", "Application requires permission to access the Internet"); } else { new WeiboDialog(context, url, listener,redirecturl).show(); } } }