oauth.signpost.exception.OAuthExpectationFailedException Java Examples

The following examples show how to use oauth.signpost.exception.OAuthExpectationFailedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TwitterSearchServiceImpl.java    From albert with MIT License 6 votes vote down vote up
@Override
public Map<String, Object> check(TwitterSearchHistory tsh) throws RuntimeException, OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, ClientProtocolException, IOException{
	Map<String, Object> map =new HashMap<String, Object>();
	//检查是否已经搜索过此内容或用户
	TwitterSearchHistoryExample example = new TwitterSearchHistoryExample();
	example.createCriteria().andSearchKeyEqualTo(tsh.getSearchKey())
							.andSearchTypeEqualTo(tsh.getSearchType());
	List<TwitterSearchHistory> list =twitterSearchHistoryMapper.selectByExample(example);
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
	if(null!=list&&list.size()>0){
		tsh =list.get(0);
	}
	if(null!=list&&list.size()>0&&sdf.format(list.get(0).getSearchDate()).compareTo(sdf.format(new Date()))==0){
		map.put("searchId", tsh.getId());
		map.put("status", Boolean.TRUE);
	}else{
		//今天没有搜索过,则调用推特api查询数据
		map = this.addTwitterPostByKey(tsh);
	}
	return map;
}
 
Example #2
Source File: LtiOauthSigner.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public HttpRequest sign(HttpRequest request, String key, String secret) throws LtiSigningException {
    CommonsHttpOAuthConsumer signer = new CommonsHttpOAuthConsumer(key, secret);
    try {
        String body = getRequestBody(request);
        String bodyHash = new String(Base64.encodeBase64(md.digest(body.getBytes())));

        HttpParameters params = new HttpParameters();
        params.put("oauth_body_hash", URLEncoder.encode(bodyHash, "UTF-8"));
        signer.setAdditionalParameters(params);

        signer.sign(request);
    } catch (OAuthMessageSignerException|OAuthExpectationFailedException|OAuthCommunicationException|IOException e) {
        throw new LtiSigningException("Exception encountered while singing Lti request...", e);
    }
    return request;
}
 
Example #3
Source File: OAuthInteractorTest.java    From Forage with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void retrieveAccessToken_success() throws OAuthCommunicationException, OAuthExpectationFailedException, OAuthNotAuthorizedException, OAuthMessageSignerException {
    Uri uri = mock(Uri.class);
    String verifier = "some verifier";
    when(uri.getQueryParameter(OAuth.OAUTH_VERIFIER)).thenReturn(verifier);

    String userTokenString = "some token";
    when(okHttpOAuthConsumer.getToken()).thenReturn(userTokenString);

    String userTokenSecretString = "some secret token";
    when(okHttpOAuthConsumer.getTokenSecret()).thenReturn(userTokenSecretString);


    TestSubscriber testSubscriber = new TestSubscriber();
    oAuthInteractor.retrieveAccessToken(uri)
            .subscribe(testSubscriber);

    testSubscriber.assertCompleted();

    verify(okHttpOAuthProvider, times(1)).retrieveAccessToken(okHttpOAuthConsumer, verifier);
    verify(userToken, times(1)).set(userTokenString);
    verify(userTokenSecret, times(1)).set(userTokenSecretString);
}
 
Example #4
Source File: OsmConnection.java    From osmapi with GNU Lesser General Public License v3.0 6 votes vote down vote up
private OAuthConsumer createOAuthConsumer() throws OAuthExpectationFailedException
{
	synchronized(oauthLock)
	{
		if(oauth == null)
		{
			throw new OAuthExpectationFailedException(
					"This class has been initialized without a OAuthConsumer. Only API calls " +
					"that do not require authentication can be made.");
		}

		// "clone" the original consumer every time because the consumer is documented to be not
		// thread safe and maybe multiple threads are making calls to this class
		OAuthConsumer consumer = new DefaultOAuthConsumer(
				oauth.getConsumerKey(), oauth.getConsumerSecret());
		consumer.setTokenWithSecret(oauth.getToken(), oauth.getTokenSecret());
		return consumer;
	}
}
 
Example #5
Source File: LtiOauthSigner.java    From basiclti-util-java with Apache License 2.0 6 votes vote down vote up
@Override
public HttpRequest sign(HttpRequest request, String key, String secret) throws LtiSigningException {
    CommonsHttpOAuthConsumer signer = new CommonsHttpOAuthConsumer(key, secret);
    try {
        String body = getRequestBody(request);
        String bodyHash = new String(Base64.encodeBase64(md.digest(body.getBytes())));

        HttpParameters params = new HttpParameters();
        params.put("oauth_body_hash", URLEncoder.encode(bodyHash, "UTF-8"));
        signer.setAdditionalParameters(params);

        signer.sign(request);
    } catch (OAuthMessageSignerException|OAuthExpectationFailedException|OAuthCommunicationException|IOException e) {
        throw new LtiSigningException("Exception encountered while singing Lti request...", e);
    }
    return request;
}
 
Example #6
Source File: OBPRestClient.java    From Hello-OBP-OAuth1.0a-Android with Apache License 2.0 6 votes vote down vote up
public static String getAuthoriseAppUrl(Context context)
		throws OAuthMessageSignerException, OAuthNotAuthorizedException,
		OAuthExpectationFailedException, OAuthCommunicationException {

	/**
	 * You will want to set customProtocol to something unique to avoid
	 * having Android give the user more than one app to pick from to handle
	 * the oauth callback. A good option would be to use something derived
	 * from your package name.
	 */
	String customProtocol = context.getResources().getString(
			R.string.customAppProtocol);

	return provider.retrieveRequestToken(consumer, customProtocol
			+ "://oauth");
}
 
Example #7
Source File: Authenticator.java    From AndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
public static OAuthConsumer createOAuthConsumer(OAuthConsumer oauth) throws OAuthExpectationFailedException {
    if (oauth == null) {
        throw new OAuthExpectationFailedException(
                "This class has been initialized without a OAuthConsumer. Only API calls " +
                        "that do not require authentication can be made.");
    }

    // "clone" the original consumer every time because the consumer is documented to be not
    // thread safe and maybe multiple threads are making calls to this class
    OAuthConsumer consumer = new DefaultOAuthConsumer(
            oauth.getConsumerKey(), oauth.getConsumerSecret());
    consumer.setTokenWithSecret(oauth.getToken(), oauth.getTokenSecret());
    return consumer;
}
 
Example #8
Source File: OAuthInteractorTest.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void retrieveRequestToken_success() throws OAuthCommunicationException, OAuthExpectationFailedException, OAuthNotAuthorizedException, OAuthMessageSignerException {
    String token = "some token";
    when(okHttpOAuthProvider.retrieveRequestToken(any(), anyString())).thenReturn(token);

    TestSubscriber<String> testSubscriber = new TestSubscriber<>();
    oAuthInteractor.retrieveRequestToken()
            .subscribe(testSubscriber);

    testSubscriber.assertValue(token);
    verify(okHttpOAuthProvider, times(1)).retrieveRequestToken(okHttpOAuthConsumer, ApiConstants.OAUTH_CALLBACK);
}
 
Example #9
Source File: OAuthInteractorTest.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void retrieveRequestToken_error() throws OAuthCommunicationException, OAuthExpectationFailedException, OAuthNotAuthorizedException, OAuthMessageSignerException {
    Throwable throwable = new RuntimeException();
    when(okHttpOAuthProvider.retrieveRequestToken(any(), anyString())).thenThrow(throwable);

    TestSubscriber<String> testSubscriber = new TestSubscriber<>();
    oAuthInteractor.retrieveRequestToken()
            .subscribe(testSubscriber);

    testSubscriber.assertError(throwable);
    verify(okHttpOAuthProvider, times(1)).retrieveRequestToken(okHttpOAuthConsumer, ApiConstants.OAUTH_CALLBACK);
}
 
Example #10
Source File: OAuthInteractorTest.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void retrieveAccessToken_error() throws OAuthCommunicationException, OAuthExpectationFailedException, OAuthNotAuthorizedException, OAuthMessageSignerException {
    Uri uri = mock(Uri.class);
    when(uri.getQueryParameter(OAuth.OAUTH_VERIFIER)).thenReturn(null);

    TestSubscriber testSubscriber = new TestSubscriber();
    oAuthInteractor.retrieveAccessToken(uri)
            .subscribe(testSubscriber);

    // We don't really care what the error is just that we have one
    // Hacky way to see if there is an error (terminal event) but not success (completion)
    testSubscriber.assertTerminalEvent();
    testSubscriber.assertNotCompleted();
}
 
Example #11
Source File: NotesDaoTest.java    From osmapi with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testReopenNoteAsAnonymousFails()
{
	try
	{
		anonymousDao.reopen(note.id, TEXT);
		fail();
	}
	catch(OsmAuthorizationException e)
	{
		assertTrue(e.getCause() instanceof OAuthExpectationFailedException);
	}
}
 
Example #12
Source File: NotesDaoTest.java    From osmapi with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testCloseNoteAsAnonymousFails()
{
	try
	{
		anonymousDao.close(note.id, TEXT);
		fail();
	}
	catch(OsmAuthorizationException e)
	{
		assertTrue(e.getCause() instanceof OAuthExpectationFailedException);
	}
}
 
Example #13
Source File: OAuth1Utils.java    From shimmer with Apache License 2.0 5 votes vote down vote up
/**
 * Signs an HTTP post request for cases where OAuth 1.0 posts are
 * required instead of GET.
 *
 * @param unsignedUrl     - The unsigned URL
 * @param clientId        - The external provider assigned client id
 * @param clientSecret    - The external provider assigned client secret
 * @param token           - The access token
 * @param tokenSecret     - The 'secret' parameter to be used (Note: token secret != client secret)
 * @param oAuthParameters - Any additional parameters
 * @return The request to be signed and sent to external data provider.
 */
public static HttpRequestBase getSignedRequest(HttpMethod method,
                                               String unsignedUrl,
                                               String clientId,
                                               String clientSecret,
                                               String token,
                                               String tokenSecret,
                                               Map<String, String> oAuthParameters) throws ShimException {

    URL requestUrl = buildSignedUrl(unsignedUrl, clientId, clientSecret, token, tokenSecret, oAuthParameters);
    String[] signedParams = requestUrl.toString().split("\\?")[1].split("&");

    HttpRequestBase postRequest = method == HttpMethod.GET ?
        new HttpGet(unsignedUrl) : new HttpPost(unsignedUrl);
    String oauthHeader = "";
    for (String signedParam : signedParams) {
        String[] parts = signedParam.split("=");
        oauthHeader += parts[0] + "=\"" + parts[1] + "\",";
    }
    oauthHeader = "OAuth " + oauthHeader.substring(0, oauthHeader.length() - 1);
    postRequest.setHeader("Authorization", oauthHeader);
    CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(clientId, clientSecret);
    consumer.setSendEmptyTokens(false);
    if (token != null) {
        consumer.setTokenWithSecret(token, tokenSecret);
    }
    try {
        consumer.sign(postRequest);
        return postRequest;
    } catch (
        OAuthMessageSignerException
            | OAuthExpectationFailedException
            | OAuthCommunicationException e) {
        e.printStackTrace();
        throw new ShimException("Could not sign POST request, cannot continue");
    }
}
 
Example #14
Source File: OAuth.java    From restcommander with Apache License 2.0 5 votes vote down vote up
private Error(OAuthException exception) {
    this.exception = exception;
    if (this.exception instanceof OAuthMessageSignerException) {
        this.type = Type.MESSAGE_SIGNER;
    } else if (this.exception instanceof OAuthNotAuthorizedException) {
        this.type = Type.NOT_AUTHORIZED;
    } else if (this.exception instanceof OAuthExpectationFailedException) {
        this.type = Type.EXPECTATION_FAILED;
    } else if (this.exception instanceof OAuthCommunicationException) {
        this.type = Type.COMMUNICATION;
    } else {
        this.type = Type.OTHER;
    }
    this.details = exception.getMessage();
}
 
Example #15
Source File: WSAsync.java    From restcommander with Apache License 2.0 4 votes vote down vote up
public WSRequest sign(WSRequest request, String method) throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException {
    WSRequestAdapter req = (WSRequestAdapter)wrap(request);
    req.setMethod(method);
    sign(req);
    return request;
}
 
Example #16
Source File: TwitterSearchService.java    From albert with MIT License votes vote down vote up
Map<String, Object> check(TwitterSearchHistory tsh) throws RuntimeException, OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, ClientProtocolException, IOException;