Search in sources :

Example 21 with AuthScope

use of org.apache.http.auth.AuthScope in project api-snippets by TwilioDevEd.

the class Example method main.

public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("lookups.twilio.com", 80), new UsernamePasswordCredentials("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "your_auth_token"));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {
        HttpGet httpget = new HttpGet("https://lookups.twilio.com/v1/PhoneNumbers/+16502530000/?AddOns=payfone_tcpa_compliance&AddOns.payfone_tcpa_compliance.RightPartyContactedDate=20160101");
        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 22 with AuthScope

use of org.apache.http.auth.AuthScope in project SeaStar by 13120241790.

the class SyncHttpClient method setBasicAuth.

/**
	 * Sets basic authentication for the request. Uses AuthScope.ANY. This is
	 * the same as setBasicAuth('username','password',AuthScope.ANY)
	 * 
	 * @param username
	 *            Basic Auth username
	 * @param password
	 *            Basic Auth password
	 */
public void setBasicAuth(String username, String password) {
    AuthScope scope = AuthScope.ANY;
    setBasicAuth(username, password, scope);
}
Also used : AuthScope(org.apache.http.auth.AuthScope)

Example 23 with AuthScope

use of org.apache.http.auth.AuthScope in project SeaStar by 13120241790.

the class AsyncHttpClient method setBasicAuth.

/**
     * Sets basic authentication for the request. Uses AuthScope.ANY. This is the same as
     * setBasicAuth('username','password',AuthScope.ANY)
     *
     * @param username Basic Auth username
     * @param password Basic Auth password
     */
public void setBasicAuth(String username, String password) {
    AuthScope scope = AuthScope.ANY;
    setBasicAuth(username, password, scope);
}
Also used : AuthScope(org.apache.http.auth.AuthScope)

Example 24 with AuthScope

use of org.apache.http.auth.AuthScope in project orientdb by orientechnologies.

the class BaseHttpTest method exec.

protected BaseHttpTest exec() throws IOException {
    final HttpHost targetHost = new HttpHost(getHost(), getPort(), getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost), new UsernamePasswordCredentials(getUserName(), getUserPassword()));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    if (keepAlive != null)
        request.addHeader("Connection", keepAlive ? "Keep-Alive" : "Close");
    if (payload != null && request instanceof HttpEntityEnclosingRequestBase)
        ((HttpEntityEnclosingRequestBase) request).setEntity(payload);
    final CloseableHttpClient httpClient = HttpClients.createDefault();
    // DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(retry, false);
    // context.setAttribute(HttpMethodParams.RETRY_HANDLER, retryhandler);
    response = httpClient.execute(targetHost, request, context);
    return this;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 25 with AuthScope

use of org.apache.http.auth.AuthScope in project apex-core by apache.

the class WebServicesClientTest method checkUserCredentials.

public static void checkUserCredentials(String username, String password, AuthScheme authScheme) throws NoSuchFieldException, IllegalAccessException {
    CredentialsProvider provider = getCredentialsProvider();
    String httpScheme = AuthScope.ANY_SCHEME;
    if (authScheme == AuthScheme.BASIC) {
        httpScheme = AuthSchemes.BASIC;
    } else if (authScheme == AuthScheme.DIGEST) {
        httpScheme = AuthSchemes.DIGEST;
    }
    AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, httpScheme);
    Credentials credentials = provider.getCredentials(authScope);
    Assert.assertNotNull("Credentials", credentials);
    Assert.assertTrue("Credentials type is user", UsernamePasswordCredentials.class.isAssignableFrom(credentials.getClass()));
    UsernamePasswordCredentials pwdCredentials = (UsernamePasswordCredentials) credentials;
    Assert.assertEquals("Username", username, pwdCredentials.getUserName());
    Assert.assertEquals("Password", password, pwdCredentials.getPassword());
}
Also used : AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

AuthScope (org.apache.http.auth.AuthScope)95 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)60 CredentialsProvider (org.apache.http.client.CredentialsProvider)42 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)41 HttpHost (org.apache.http.HttpHost)28 Credentials (org.apache.http.auth.Credentials)22 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 Test (org.junit.Test)19 HttpResponse (org.apache.http.HttpResponse)17 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)14 HttpGet (org.apache.http.client.methods.HttpGet)13 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)12 IOException (java.io.IOException)11 BasicScheme (org.apache.http.impl.auth.BasicScheme)11 HttpEntity (org.apache.http.HttpEntity)10 AuthScheme (org.apache.http.auth.AuthScheme)8 NTCredentials (org.apache.http.auth.NTCredentials)7 AuthCache (org.apache.http.client.AuthCache)7 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)7 URL (java.net.URL)6