@Override protected String doInBackground() throws Exception { return provider.retrieveRequestToken(consumer, CALLBACK_URL); }
/** * Helper method to concatenate a parameter and its value to a pair that can * be used in an HTTP header. This method percent encodes both parts before * joining them. * * @param name * the OAuth parameter name, e.g. oauth_token * @param value * the OAuth parameter value, e.g. 'hello oauth' * @return a name/value pair, e.g. oauth_token="hello%20oauth" */ public static String toHeaderElement(String name, String value) { return OAuth.percentEncode(name) + "=\"" + OAuth.percentEncode(value) + "\""; }
@Test(expected = OAuthExpectationFailedException.class) public void shouldThrowIfConsumerSecretNotSet() throws Exception { OAuthConsumer consumer = buildConsumer(CONSUMER_KEY, null, null); consumer.setTokenWithSecret(TOKEN, TOKEN_SECRET); consumer.sign(httpGetMock); }
@Test public void shouldRetrieveAccessTokenAndUpdateConsumer() throws Exception { provider.retrieveAccessToken(consumerMock, null); verify(consumerMock).sign((HttpRequest) anyObject()); verify(consumerMock).setTokenWithSecret(TOKEN, TOKEN_SECRET); }
public synchronized void retrieveAccessToken(OAuthConsumer consumer, String oauthVerifier, String... customOAuthParams) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException { if (consumer.getToken() == null || consumer.getTokenSecret() == null) { throw new OAuthExpectationFailedException( "Authorized request token or token secret not set. " + "Did you retrieve an authorized request token before?"); } HttpParameters params = new HttpParameters(); params.putAll(customOAuthParams, true); if (isOAuth10a && oauthVerifier != null) { params.put(OAuth.OAUTH_VERIFIER, oauthVerifier, true); } retrieveToken(consumer, accessTokenEndpointUrl, params); }
@Test public void shouldSignUrl() throws Exception { OAuthConsumer consumer = buildConsumer(CONSUMER_KEY, CONSUMER_SECRET, null); consumer.setTokenWithSecret(TOKEN, TOKEN_SECRET); String result = consumer.sign("http://www.example.com?q=1"); assertNotNull(result); String[] parts = result.split("\\?"); assertEquals("parameters are missing", 2, parts.length); assertEquals("http://www.example.com", parts[0]); HttpParameters params = OAuth.decodeForm(parts[1]); assertAllOAuthParametersExist(params); assertEquals("1", params.getFirst("q")); }
@Test public void shouldSignHttpRequestMessage() throws Exception { OAuthConsumer consumer = buildConsumer(CONSUMER_KEY, CONSUMER_SECRET, null); consumer.setTokenWithSecret(TOKEN, TOKEN_SECRET); consumer.sign(httpGetMock); verify(httpGetMock).setHeader(eq("Authorization"), argThat(new IsCompleteListOfOAuthParameters())); }
@Test public void shouldPercentEncodeOAuthParameters() throws Exception { OAuthConsumer consumer = buildConsumer("1%2", CONSUMER_SECRET, null); consumer.setTokenWithSecret("3 4", TOKEN_SECRET); consumer.sign(httpGetMock); verify(httpGetMock).setHeader(eq("Authorization"), argThat(new HasValuesPercentEncoded())); }
@Before public void prepare() throws Exception { MockitoAnnotations.initMocks(this); // init consumer mock when(consumerMock.getConsumerKey()).thenReturn(CONSUMER_KEY); when(consumerMock.getConsumerSecret()).thenReturn(CONSUMER_SECRET); when(consumerMock.getToken()).thenReturn(TOKEN); when(consumerMock.getTokenSecret()).thenReturn(TOKEN_SECRET); provider = buildProvider(REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL, AUTHORIZE_WEBSITE_URL, true); }
@Test(expected = OAuthExpectationFailedException.class) public void shouldThrowExpectationFailedIfConsumerSecretNotSet() throws Exception { provider = buildProvider(REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL, AUTHORIZE_WEBSITE_URL, true); provider.retrieveRequestToken(new DefaultOAuthConsumer(CONSUMER_KEY, null), REQUEST_TOKEN_ENDPOINT_URL); }
@Override public void run() { try { consumer.sign(httpGetMockWithQueryString); } catch (Exception e) { throw new RuntimeException("sign error", e); } } };
@Test(expected = OAuthExpectationFailedException.class) public void shouldThrowWhenGettingAccessTokenAndRequestTokenNotSet() throws Exception { when(consumerMock.getToken()).thenReturn(null); provider.retrieveAccessToken(consumerMock, null); }
@Test(expected = OAuthExpectationFailedException.class) public void shouldThrowWhenGettingAccessTokenAndRequestTokenSecretNotSet() throws Exception { when(consumerMock.getTokenSecret()).thenReturn(null); provider.retrieveAccessToken(consumerMock, null); }
private OAuthConsumer getConsumer() { OAuthConsumer consumer = new CommonsHttpOAuthConsumer(LinkedinConstants.CONSUMER_KEY, LinkedinConstants.CONSUMER_SECRET); consumer.setTokenWithSecret(token, secret); return consumer; }
public static String addQueryParameters(String url, Map<String, String> params) { String[] kvPairs = new String[params.size() * 2]; int idx = 0; for (String key : params.keySet()) { kvPairs[idx] = key; kvPairs[idx + 1] = params.get(key); idx += 2; } return addQueryParameters(url, kvPairs); }
@Override public String sign(HttpRequest request, HttpParameters requestParams) throws OAuthMessageSignerException { return OAuth.percentEncode(getConsumerSecret()) + '&' + OAuth.percentEncode(getTokenSecret()); } }
@Test(expected = OAuthExpectationFailedException.class) public void shouldThrowIfConsumerKeyNotSet() throws Exception { OAuthConsumer consumer = buildConsumer(null, CONSUMER_SECRET, null); consumer.setTokenWithSecret(TOKEN, TOKEN_SECRET); consumer.sign(httpGetMock); }
@Test(expected = OAuthExpectationFailedException.class) public void shouldThrowExpectationFailedIfConsumerKeyNotSet() throws Exception { provider = buildProvider(REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL, AUTHORIZE_WEBSITE_URL, true); provider.retrieveRequestToken(new DefaultOAuthConsumer(null, CONSUMER_SECRET), REQUEST_TOKEN_ENDPOINT_URL); }
@Override public void run() { try { consumer.sign(httpGetMock); } catch (Exception e) { throw new RuntimeException("sign error", e); } } };