public void authorize(String url)
     throws OAuthMessageSignerException, OAuthNotAuthorizedException,
         OAuthExpectationFailedException, OAuthCommunicationException {
   String verifier = Uri.parse(url).getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
   httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier);
   accessToken = new Token(httpOauthConsumer.getToken(), httpOauthConsumer.getTokenSecret());
   authentified = true;
 }
Exemple #2
0
  public void getAccessToken()
      throws OAuthMessageSignerException, OAuthNotAuthorizedException,
          OAuthExpectationFailedException, OAuthCommunicationException {

    provider.setOAuth10a(true);
    provider.retrieveAccessToken(consumer, verifier);
    this.access_token = consumer.getToken();
    this.access_token_secret = consumer.getTokenSecret();
  }
  @Override
  public HttpResponse executeAuthenticatedHttpRequest(HttpUriRequest request)
      throws ThoundsConnectionException {
    try {
      consumer.sign(request);
      HttpResponse response;
      synchronized (httpclient) {
        response = httpclient.execute(request);
      }
      return response;
    } 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();
    } catch (IOException e) {
      throw new ThoundsConnectionException();
    }

    return null;
  }
Exemple #4
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));
 }
Exemple #5
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;
 }
Exemple #6
0
 /**
  * 获得AccessToken
  *
  * @param intent
  * @return UserInfo
  */
 public User getAccessToken(Intent intent) {
   Uri uri = intent.getData();
   // 处理获取返回的oauth_verifier参数
   String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
   try {
     httpOauthprovider.setOAuth10a(true);
     httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier);
   } catch (OAuthMessageSignerException ex) {
     ex.printStackTrace();
   } catch (OAuthNotAuthorizedException ex) {
     ex.printStackTrace();
   } catch (OAuthExpectationFailedException ex) {
     ex.printStackTrace();
   } catch (OAuthCommunicationException ex) {
     ex.printStackTrace();
   }
   SortedSet<String> user_id = httpOauthprovider.getResponseParameters().get("user_id");
   String userToken = httpOauthConsumer.getToken();
   String userSecret = httpOauthConsumer.getTokenSecret();
   User user = new User(user_id.first(), userToken, userSecret);
   return user;
 }
 // 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;
 }
Exemple #8
0
 /**
  * Execute a GET request on an object
  *
  * @param path Relative path to resource
  */
 protected JSONObject getObject(String path) {
   HttpGet request = new HttpGet(mBaseUrl + path);
   DefaultHttpClient httpclient = new DefaultHttpClient();
   ResponseHandler<String> handler = new BasicResponseHandler();
   try {
     mConsumer.sign(request);
     String result = httpclient.execute(request, handler);
     if (result != "") {
       JSONObject json = new JSONObject(result);
       return json;
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
   return null;
 }
 /**
  * Returns the access token secret.
  *
  * @return the access token secret.
  */
 public String getTokenSecret() {
   return consumer.getTokenSecret();
 }
 /** Unsets access token and secret. */
 public void logout() {
   isAuthenticated = false;
   consumer.setTokenWithSecret(null, null);
 }
 /**
  * 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;
 }
 // verifier parametro oauth_verifier della query
 public void retrieveAccessToken(String verifier)
     throws ThoundsOAuthParameterExcepion, ThoundsConnectionException {
   retrieveAccessToken(verifier, consumer.getToken(), consumer.getTokenSecret());
 }
  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();
      }
    }
  }
Exemple #14
0
 public Token getAccessToken() {
   accessToken = new Token(httpOauthConsumer.getToken(), httpOauthConsumer.getTokenSecret());
   return accessToken;
 }
Exemple #15
0
 public boolean authentify(Token accessToken) {
   this.accessToken = accessToken;
   httpOauthConsumer.setTokenWithSecret(accessToken.getToken(), accessToken.getSecret());
   this.authentified = true;
   return true;
 }