Exemplo n.º 1
0
 /**
  * Initializes RESTful connection
  *
  * @param ctx Context needed for assessing shared resources
  */
 public RestClient(Context ctx) {
   mContext = ctx;
   mBaseUrl = mContext.getString(R.string.base_url);
   mConsumer =
       new CommonsHttpOAuthConsumer(
           mContext.getString(R.string.consumer_key),
           mContext.getString(R.string.consumer_secret));
   SharedPreferences settings = mContext.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
   mConsumer.setTokenWithSecret(
       settings.getString("accessToken", null), settings.getString("accessSecret", null));
 }
Exemplo n.º 2
0
 // verifier parametro oauth_verifier della query
 public void retrieveAccessToken(String verifier, String token, String tokenSecret)
     throws ThoundsOAuthParameterExcepion, ThoundsConnectionException {
   try {
     consumer.setTokenWithSecret(token, tokenSecret);
     provider.retrieveAccessToken(consumer, verifier);
   } catch (OAuthMessageSignerException e) {
     throw new ThoundsOAuthParameterExcepion();
   } catch (OAuthNotAuthorizedException e) {
     throw new ThoundsOAuthParameterExcepion();
   } catch (OAuthExpectationFailedException e) {
     throw new ThoundsOAuthParameterExcepion();
   } catch (OAuthCommunicationException e) {
     throw new ThoundsConnectionException();
   }
   isAuthenticated = true;
 }
Exemplo n.º 3
0
 public HttpResponse signRequest(String token, String tokenSecret, HttpPost post) {
   httpOauthConsumer = new CommonsHttpOAuthConsumer(APP_KEY, APP_SECRET);
   httpOauthConsumer.setTokenWithSecret(token, tokenSecret);
   HttpResponse response = null;
   try {
     httpOauthConsumer.sign(post);
   } catch (OAuthMessageSignerException e) {
     e.printStackTrace();
   } catch (OAuthExpectationFailedException e) {
     e.printStackTrace();
   } catch (OAuthCommunicationException e) {
     e.printStackTrace();
   }
   try {
     response = new DefaultHttpClient().execute(post);
   } catch (ClientProtocolException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   return response;
 }
Exemplo n.º 4
0
 /** Unsets access token and secret. */
 public void logout() {
   isAuthenticated = false;
   consumer.setTokenWithSecret(null, null);
 }
Exemplo n.º 5
0
 /**
  * Sets access token and secret.
  *
  * @param token the access token.
  * @param tokenSecret the access token secret.
  */
 public void setAccessToken(String token, String tokenSecret) {
   consumer.setTokenWithSecret(token, tokenSecret);
   isAuthenticated = true;
 }
  private void dealing(List<NameValuePair> nvps) {
    String url = "http://api.t.sina.com.cn/blocks/exists.json";
    // 新建一个POST
    HttpPost post = new HttpPost(url);

    // 将参数加到post中
    try {
      post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    // 关闭Expect:100-Continue握手
    // 100-Continue握手需谨慎使用,因为遇到不支持HTTP/1.1协议的服务器或者代理时会引起问题
    post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
    // 生成CommonsHttpOAuthConsumer对象
    CommonsHttpOAuthConsumer choc =
        new CommonsHttpOAuthConsumer(Constant.consumerKey, Constant.consumerSecret);
    choc.setTokenWithSecret(Constant.userKey, Constant.userSecret);
    // 对post进行签名
    try {
      choc.sign(post);
    } catch (OAuthMessageSignerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (OAuthExpectationFailedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (OAuthCommunicationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    // 发送post用来获得HttpResponse
    HttpClient hc = new DefaultHttpClient();
    HttpResponse rp = null;
    try {
      rp = hc.execute(post);
    } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    if (200 == rp.getStatusLine().getStatusCode()) {
      try {
        Log.v("getStatusLine", "OK");
        // 将HttpResponse中的内容读到buffer中
        InputStream is = rp.getEntity().getContent();
        Reader reader = new BufferedReader(new InputStreamReader(is), 4000);
        StringBuilder buffer = new StringBuilder((int) rp.getEntity().getContentLength());
        try {
          char[] tmp = new char[1024];
          int l;
          while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
          }
        } finally {
          reader.close();
        }
        // 将buffer转为json可以支持的String类型
        String string = buffer.toString();
        // 销毁rp
        rp.getEntity().consumeContent();
        // 新建JSONObject来处理其中的数据
        try {
          JSONObject data = new JSONObject(string);
          Log.v("result", data.getString("result"));
          // result结果为true则该用户已被加黑名单,false则该用户未被加黑名单

        } catch (JSONException e) {
          e.printStackTrace();
        }

      } catch (IllegalStateException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
Exemplo n.º 7
0
 public boolean authentify(Token accessToken) {
   this.accessToken = accessToken;
   httpOauthConsumer.setTokenWithSecret(accessToken.getToken(), accessToken.getSecret());
   this.authentified = true;
   return true;
 }