Android Open Source - doubanbook4android Douban






From Project

Back to project page doubanbook4android.

License

The source code is released under:

Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECI...

If you think the Android project doubanbook4android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.study.doubanbook_for_android.auth;
/*  w w w .  j  a  va2s . c o m*/
import com.study.doubanbook_for_android.utils.UriUtils;

import android.Manifest;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * douban for basic oath
 */
public class Douban {

  private static Douban mDoubanInstance = null;
  public static String URL_OAUTH2_ACCESS_AUTHORIZE = "https://www.douban.com/service/auth2/auth";
  public static String URL_OAUTH2_ACCESS_TOKEN = "https://www.douban.com/service/auth2/token";
  public static String app_key = "09561172f001e8251c023458d005b86f";// ??????appkey
  public static String secret = "f31eaf3a7bf0a4cf";// ??????appkey
  public static String redirecturl = "http://developers.douban.com/";// ??????url
  public static String scope = "book_basic_r,book_basic_w,douban_basic_common";
  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 final String KEY_USERID = "douban_user_id";
  public static final String KEY_GRANT_TYPE = "grant_type";
  public static final String KEY_CLIENT_SECRET = "client_secret";

  public AccessToken accessToken = null;// AccessToken??

  private Douban() {
  }

  /**
   * 
   * @param appKey
   *            ??????appkey
   * @param redirectUrl
   *            ?????????
   * @return Douban???
   */
  public synchronized static Douban getInstance(String appKey,
      String clientSecret, String redirectUrl, String doubanScope) {
    if (mDoubanInstance == null) {
      mDoubanInstance = new Douban();
    }
    app_key = appKey;
    secret = clientSecret;
    redirecturl = redirectUrl;
    scope = doubanScope;
    return mDoubanInstance;
  }

  public synchronized static Douban getInstance() {
    if (mDoubanInstance == null) {
      mDoubanInstance = new Douban();
    }
    return mDoubanInstance;
  }

  /**
   * ?????????appkey???????url
   * 
   * @param appKey
   *            ??????appkey
   * @param redirectUrl
   *            ?????????
   */
  public void setupConsumerConfig(String appKey, String clientSecret,
      String redirectUrl, String doubanScope) {
    app_key = appKey;
    secret = clientSecret;
    redirecturl = redirectUrl;
    scope = doubanScope;
  }

  /**
   * 
   * ??Douban???
   * 
   * @param activity
   *            ????????Context??
   * @param listener
   *            DoubanOAuthListener Douban??????????
   */
  public void authorize(Context context, DoubanOAuthListener listener) {
    startAuthDialog(context, listener);
  }

  public void startAuthDialog(final Context context,
      final DoubanOAuthListener listener) {

    showDialog(context, new DoubanOAuthListener() {
      @Override
      public void onComplete(Token token) {
        // ensure any cookies set by the dialog are saved

        if (null == accessToken) {
          accessToken = new AccessToken();
        }
        accessToken.setToken(token.access_token);
        accessToken.setExpiresIn(String.valueOf(token.expires_in));
        accessToken.setRefreshToken(token.refresh_token);
        accessToken.setDoubanUserId(token.douban_user_id);
        if (accessToken.isSessionValid()) {
          Log.d("Douban-authorize", "Login Success! access_token="
              + accessToken.getToken() + " expires="
              + accessToken.getExpiresTime() + " refresh_token="
              + accessToken.getRefreshToken());
          KeepToken.keepAccessToken(context, accessToken);
          listener.onComplete(token);
        } else {
          Log.d("Douban-authorize", "Failed to receive access token");
          listener.onDoubanException(new DoubanException(
              "Failed to receive access token."));
        }
      }

      @Override
      public void onError(DoubanDialogError error) {
        Log.d("Douban-authorize", "Login failed: " + error);
        listener.onError(error);
      }

      @Override
      public void onDoubanException(DoubanException error) {
        Log.d("Douban-authorize", "Login failed: " + error);
        listener.onDoubanException(error);
      }

      @Override
      public void onCancel() {
        Log.d("Douban-authorize", "Login canceled");
        listener.onCancel();
      }
    });
  }

  public void showDialog(final Context context,
      final DoubanOAuthListener listener) {
    if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
      showAlert(context, "Error",
          "Application requires permission to access the Internet");
      return;
    }
    accessToken = KeepToken.readAccessToken(context);
    DoubanParameters params = new DoubanParameters();
    params.add("client_id", app_key);
    params.add("redirect_uri", redirecturl);

    if (accessToken != null && accessToken.isSessionValid()) {
      params.add(KEY_REFRESHTOKEN, accessToken.getRefreshToken());
      params.add(KEY_GRANT_TYPE, "refresh_token");
      params.add(KEY_CLIENT_SECRET, secret);
      exchangeToken(context, params, listener);
    } else {
      params.add("response_type", "code");
      params.add("scope", scope);
      String authUrl = URL_OAUTH2_ACCESS_AUTHORIZE + "?"
          + UriUtils.encodeUrl(params);
      new DoubanDialog(context, authUrl, listener).show();
    }
  }

  public void exchangeToken(final Context context,
      final DoubanParameters params, final DoubanOAuthListener listener) {
    new Thread() {
      public void run() {
        Token token = DoubanRequest.requestAccessToken(params);
        if (token != null) {
          listener.onComplete(token);
        } else {
          listener.onDoubanException(new DoubanException(
              "request failure !"));
        }
      }
    }.start();
  }

  public void showAlert(Context context, String title, String text) {
    Builder alertBuilder = new Builder(context);
    alertBuilder.setTitle(title);
    alertBuilder.setMessage(text);
    alertBuilder.create().show();
  }

}




Java Source Code List

.UserListAdapter.java
com.study.doubanbook_for_android.activity.BaseActivity.java
com.study.doubanbook_for_android.activity.BaseP2RActivity.java
com.study.doubanbook_for_android.activity.BookDetailActivity.java
com.study.doubanbook_for_android.activity.BookListsActivity.java
com.study.doubanbook_for_android.activity.BookNoteListActivity.java
com.study.doubanbook_for_android.activity.CollectDetailActivity.java
com.study.doubanbook_for_android.activity.NoteAndUserDetailActivity.java
com.study.doubanbook_for_android.activity.SerchInputActivity.java
com.study.doubanbook_for_android.activity.TextActivity1.java
com.study.doubanbook_for_android.activity.UserDetailActivity.java
com.study.doubanbook_for_android.activity.UserListActivity.java
com.study.doubanbook_for_android.activity.UserNoteActivity.java
com.study.doubanbook_for_android.adapter.BookAdapter.java
com.study.doubanbook_for_android.adapter.CommentAdapter.java
com.study.doubanbook_for_android.adapter.UserNoteAdapter.java
com.study.doubanbook_for_android.api.NetUtils.java
com.study.doubanbook_for_android.api.SSLSocketFactoryEx.java
com.study.doubanbook_for_android.api.WrongMsg.java
com.study.doubanbook_for_android.auth.AccessToken.java
com.study.doubanbook_for_android.auth.DoubanDialogError.java
com.study.doubanbook_for_android.auth.DoubanDialog.java
com.study.doubanbook_for_android.auth.DoubanException.java
com.study.doubanbook_for_android.auth.DoubanOAuthListener.java
com.study.doubanbook_for_android.auth.DoubanParameters.java
com.study.doubanbook_for_android.auth.DoubanRequest.java
com.study.doubanbook_for_android.auth.Douban.java
com.study.doubanbook_for_android.auth.KeepToken.java
com.study.doubanbook_for_android.auth.SimpleDoubanOAuthListener.java
com.study.doubanbook_for_android.auth.Token.java
com.study.doubanbook_for_android.business.DoubanBusiness.java
com.study.doubanbook_for_android.callback.AsynCallback.java
com.study.doubanbook_for_android.imagedownloader.FileCache.java
com.study.doubanbook_for_android.imagedownloader.INetImageDownloader.java
com.study.doubanbook_for_android.imagedownloader.ImageCache.java
com.study.doubanbook_for_android.imagedownloader.ImageDownloaderListener.java
com.study.doubanbook_for_android.imagedownloader.ImageDownloader.java
com.study.doubanbook_for_android.model.Annotations.java
com.study.doubanbook_for_android.model.AuthorUser.java
com.study.doubanbook_for_android.model.BookItem.java
com.study.doubanbook_for_android.model.CollectBookMsg.java
com.study.doubanbook_for_android.model.CollectSuccessResult.java
com.study.doubanbook_for_android.model.CommentItem.java
com.study.doubanbook_for_android.model.Comment.java
com.study.doubanbook_for_android.model.CurrenUserCollection.java
com.study.doubanbook_for_android.model.GeneralNoteResult.java
com.study.doubanbook_for_android.model.GeneralResult.java
com.study.doubanbook_for_android.model.GeneralUserResult.java
com.study.doubanbook_for_android.model.ImageItem.java
com.study.doubanbook_for_android.model.Rating.java
com.study.doubanbook_for_android.model.RequestGrantScope.java
com.study.doubanbook_for_android.model.TagItem.java
com.study.doubanbook_for_android.model.URLMananeger.java
com.study.doubanbook_for_android.utils.EncodeUtils.java
com.study.doubanbook_for_android.utils.JsonParser.java
com.study.doubanbook_for_android.utils.JsonUtil.java
com.study.doubanbook_for_android.utils.UriUtils.java