Exemplo n.º 1
0
  public KaixinAccessToken getAccessToken(
      String requestToken, String requestTokenSecret, String verifier) throws WeiboException {
    List<PostParameter> params = new ArrayList<PostParameter>();
    params.add(new PostParameter(OUATH_TOKEN, requestToken));
    params.add(new PostParameter(OUATH_TOKEN_VERIFIER, verifier));

    // 公用参数
    String timestamp = WeiboUtil.generateTimeStamp();
    String onceMd5 = Util.md5(WeiboUtil.generateNonce());
    params.add(new PostParameter("oauth_consumer_key", getApiKey()));
    params.add(new PostParameter("oauth_signature_method", "HMAC-SHA1"));
    params.add(new PostParameter("oauth_timestamp", timestamp));
    params.add(new PostParameter("oauth_nonce", onceMd5));
    // 签名
    String signature =
        makeSign(params, KX_ACCESS_TOKEN_URL, GET_METHOD, getApiSecret(), requestTokenSecret);
    params.add(new PostParameter("oauth_signature", signature));

    String url = KX_ACCESS_TOKEN_URL + "?" + WeiboUtil.encodeParameters(params, "&", false);

    try {
      String result = Util.urlGet(url);
      Bundle bundle = WeiboUtil.parseToken(result);
      String token = bundle.getString(OUATH_TOKEN);
      String tokenSecret = bundle.getString(OUATH_TOKEN_SECRET);
      if (token != null && tokenSecret != null) {
        SharedPreferences pref =
            context.getSharedPreferences(Weibo.PREF_WEIBO, Context.MODE_PRIVATE);
        pref.edit()
            .putString(PREF_ACCESSTOKEN, token)
            .putString(PREF_ACCESSTOKEN_SECRET, tokenSecret)
            .commit();
        return new KaixinAccessToken(this, token, tokenSecret);
      } else {
        throw new WeiboException("未获取到授权。");
      }
    } catch (Exception e) {
      throw new WeiboException(e);
    }
  }
Exemplo n.º 2
0
  @Override
  public RequestToken getOAuthRequestToken(String callbackUrl) throws WeiboException {
    List<PostParameter> params = new ArrayList<PostParameter>();

    // 设置回调
    params.add(new PostParameter("oauth_callback", callbackUrl));
    // 设置权限范围
    params.add(new PostParameter("scope", "basic create_records"));

    // 公用参数
    String timestamp = WeiboUtil.generateTimeStamp();
    String onceMd5 = Util.md5(WeiboUtil.generateNonce());
    params.add(new PostParameter("oauth_consumer_key", getApiKey()));
    params.add(new PostParameter("oauth_signature_method", "HMAC-SHA1"));
    params.add(new PostParameter("oauth_timestamp", timestamp));
    params.add(new PostParameter("oauth_nonce", onceMd5));
    // 签名
    String signature = makeSign(params, KX_REQUEST_TOKEN_URL, GET_METHOD, getApiSecret(), null);
    params.add(new PostParameter("oauth_signature", signature));

    // 请求url
    String url = KX_REQUEST_TOKEN_URL + "?" + WeiboUtil.encodeParameters(params, "&", false);
    try {
      String result = Util.urlGet(url);
      Bundle token = WeiboUtil.parseToken(result);
      String tokenKey = token.getString(OUATH_TOKEN);
      String tokenSecret = token.getString(OUATH_TOKEN_SECRET);
      if (tokenKey != null && tokenSecret != null) {
        return new KaixinRequestToken(this, callbackUrl, tokenKey, tokenSecret);
      } else {
        throw new WeiboException("未获取到授权。");
      }
    } catch (Exception e) {
      throw new WeiboException(e);
    }
  }
Exemplo n.º 3
0
  public boolean uploadStatus(KaixinAccessToken accessToken, String status, File file)
      throws WeiboException {
    String upload = "http://api.kaixin001.com/records/add.json";
    List<PostParameter> params = new ArrayList<PostParameter>();
    status = status.length() > 140 ? status.substring(0, 140) : status;
    params.add(new PostParameter("content", status));
    params.add(new PostParameter("oauth_token", accessToken.getToken()));
    params.add(new PostParameter("oauth_version", OAUTH_VERSION));

    // 公用参数
    String timestamp = WeiboUtil.generateTimeStamp();
    String onceMd5 = Util.md5(WeiboUtil.generateNonce());
    params.add(new PostParameter("oauth_consumer_key", getApiKey()));
    params.add(new PostParameter("oauth_signature_method", "HMAC-SHA1"));
    params.add(new PostParameter("oauth_timestamp", timestamp));
    params.add(new PostParameter("oauth_nonce", onceMd5));
    // 签名
    String signature =
        makeSign(params, upload, POST_METHOD, getApiSecret(), accessToken.getTokenSecret());
    params.add(new PostParameter("oauth_signature", signature));

    if (file != null) {
      params.add(new PostParameter("pic", file));
    }

    try {
      String s = WeiboUtil.multipartPost(upload, params);
      JSONObject json = new JSONObject(s);
      if (!Util.isEmpty(json.getString("rid"))) { // 分享成功
        return true;
      }
    } catch (Exception e) {
      throw new WeiboException("未获取到授权。", e);
    }
    return false;
  }