Apache Http Client Oauth2



Using Apache HttpClient which acts as browser to get authorization code. Get through OAuth 2.0 of Box.com without browser. boxjavaclient. Client Credentials. More on OAuth 2.0. You can find more details on OAuth2 and Grant Types here: OAuth2 Grant Type. In this guide, I want to address how to access OAuth2 protected resources in.

  1. Python Oauth2 Client Example
  2. Spring Security Oauth2 Client
  3. Apache Shiro Oauth2
  4. Apache Httpclient Oauth2
  5. C# Oauth2 Client Example
Skip to end of metadataGo to start of metadata

Getting OAuth 2.0 access token in your Java application

This example shows how to receive an access token from Facebook. It should work similarly with other OAuth 2.0 compatible implementations. See OAuth Wiki for the list of current OAuth 2.0 server implementations.

Apache Http Client Oauth2

You can also run a sample client app available at: Client Tutorial or go through the following steps:

Add Oltu client to your classpath:

add Maven dependency:

Build OAuth End User Authorization Request

Create the End User Authorization Request by providing end-user authorization URI at the Authorization Server (e.g. Facebook), your application's client id and a redirect URI, in order to receive the authorization code.

If the token Provider is not in the OAuthProviderType Enum you can manually set the correct endpoints. The Enum is merely a convenience to easily set the OAuth endpoints. The Code example below show how you can manually set the authorization endpoint.

The above code will produce an OAuth request where all the parameters are encoded in the URL query. You can obtain the generated URL by calling this method:

Python Oauth2 Client Example

For example, in a Java Servlet, you would execute the following code:

The user is redirected to Facebook (the authorization page, to be exact), which asks the user which permission they would like to grant to your application. The user simply needs to click Allow.

Get Authorization Code from redirect URI

After the user grants permission for your client application, then Facebook redirects the user to: http://www.example.com/redirect (recall step Step 2), with request parameter similar to: code=2.89e3QEvryHUOHPe9YMqpeA__.3600.1285585200-1556050396|5CUsytnAALwWALAUUM8KHlJVNpQ

Spring Security Oauth2 Client

Apache Http Client Oauth2Oauth2 authentication c# client

Exchange OAuth code for an access token

If the token Provider is not in the OAuthProviderType Enum you can manually set the correct endpoints. The Enum is merely a convenience to easily set the OAuth endpoints. The Code example below show how you can manually set the token endpoint.

Now you can store the accessToken and an optional refreshToken and retrieve user data.

Below is an example to get a Facebook profile with the retrieved accessToken.

Below is an example to post a picture to Facebook with the retrieved accessToken.

For the fully working code see OAuth 2.0 client demo included in the package!

Apache Shiro Oauth2

Enjoy!

Additional features

Provide custom response readers

Oltu is extensible and you can provide your own custom response classes that can handle responses from providers that introduce modifications to the core OAuth 2.0 specification. For example, you providers can read access tokens from
application/x-www-form-urlencoded instead of json encoded body.

Just create your own class that extends:

Apache Httpclient Oauth2

Oauth2 authentication c# client

and pass it as one of the

parameters.

C# Oauth2 Client Example

Use your own HTTP client

OAuthClient can use different java http clients with customized configurations like timeouts, connection pools, etc. in order communicate with authorization servers and receive access tokens.
Oltu provides an exemplar implementation of the URLConnection client and Apache's HttpClient 4.

You can easily write your own HTTP client by extending: