public class OAuth2
extends java.lang.Object
The OAuth2
class contains information required for authorization on a server.
Create signature for authorization request
AccessToken
instance
via AccessTokenRequest.getAccessToken()
.
The following code illustrates typical scheme of using oAuth2 :
Scheme Authorization Code Grant:
// To initialize oAuth it is necessary to provide authorization server Endpoints and client credentials OAuth2 oauth2 = new OAuth2( authorizationEndpoint, tokenEndpoint, client_id ); // creating an OAuth2 object // Create Authorization Request Request r = GTAuthorizationCode.createAuthorizationRequest(oauth2, "http:://oracle.exampleoauth2.com", "", "xyz"); // ... // Modify Request according to server specifications // ... try { // Get Authorization Request IResponse response = r.execute(); if (response.resultCode() == HttpConnection.HTTP_MOVED_TEMP) { // Get Authorization Response String link = response.getField( "Location" ); // ... // At this point you have to direct a user to the provided link. // The user have to log on to the server and grant access to his/her data. // In this case the server answers with URI composed from the redirect_uri parameter, and some 'code' value. // ... } String authorizationCode = ...; // the 'code' value from the above server's response String redirect_uri = ...; // set redirect uri AccessTokenRequest atr = GTAuthorizationCode.createAccessTokenRequest( oauth2, authorizationCode, redirect_uri ); // here you may modify the request according to your scenario and authorization server configuration try { // AccessToken Request AccessToken at = atr.getAccessToken(); oauth2.setToken( at ); // Now it is possible to use oauth2 for sign Requests; } catch (OAuth2Exception oae) { // Error Response } } catch (IOException ex) { } Request request;// // get Signature for particular request String sign = oauth2.getAuthorizationSign(request); // sign particular request request.addParameter(oauth2.getPropertyName(), sign);
Scheme Implicit Grant:
// Create Authorization Request Request r = GTImplicit.createAuthorizationRequest(oauth2, "http:://oracle.exampleoauth2.com", "", "xyz"); // ... // Modify Request according to server specifications // ... try { // Get Authorization Request IResponse response = r.execute(); if (response.resultCode() == HttpConnection.HTTP_MOVED_TEMP) { // Get Authorization Response AccessToken at = GTImplicit.getAccessToken(response); oauth2.setToken( at ); // Now it is possible to use oauth2 for sign Requests; } } catch (IOException ex) { }
Scheme Resource Owner Password Credentials Grant:
// Create Authorization Request AccessTokenRequest atr = GTResourceOwnerPasswordCredentials.createAccessTokenRequest(oauth2, "oracle", "password", ""); // here you may modify the request according to your scenario try { // AccessToken Request AccessToken at = atr.getAccessToken(); oauth2.setToken( at ); // Now it is possible to use oauth2 for sign Requests; }catch (OAuth2Exception oae) { // Error Response } catch (IOException ex) { }
Scheme Client Credentials Grant:
// Create Authorization Request AccessTokenRequest atr = GTClientCredentials.createAccessTokenRequest(oauth2, ""); // here you may modify the request according to your scenario try { // AccessToken Request AccessToken at = atr.getAccessToken(); oauth2.setToken( at ); // Now it is possible to use oauth2 for sign Requests; }catch (OAuth2Exception oae) { // Error Response } catch (IOException ex) { }
Constructor and Description |
---|
OAuth2(java.lang.String authorizationEndpoint,
java.lang.String tokenEndpoint,
java.lang.String client_id)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getAuthorizationEndpoint() |
java.lang.String |
getAuthorizationSign(Request request)
Get AuthorizationSign.
|
java.lang.String |
getClientId() |
java.lang.String |
getPropertyName()
Get Property name for sign
|
protected Signer |
getSigner(AccessToken token)
redefinition of this method allows a user of the API to provide his own RequestSigner
for the token passed as a parameter.
|
java.lang.String |
getTokenEndpoint() |
void |
setToken(AccessToken token)
this method sets
AccessToken for signing all subsequent requests
created by create() method. |
public OAuth2(java.lang.String authorizationEndpoint, java.lang.String tokenEndpoint, java.lang.String client_id)
authorizationEndpoint
- authorization endpoint of some servicetokenEndpoint
- token endpoint of the serviceclient_id
- id of the client (application key)protected Signer getSigner(AccessToken token) throws java.lang.IllegalArgumentException
token
- a tokenjava.lang.IllegalArgumentException
- if the token has unknown token_type
public void setToken(AccessToken token)
AccessToken
for signing all subsequent requests
created by create()
method.token
- a token. Can be null
. In this case new requests will not be signed.public java.lang.String getAuthorizationSign(Request request)
params
- parameters of requestpublic java.lang.String getPropertyName()
public java.lang.String getAuthorizationEndpoint()
public java.lang.String getTokenEndpoint()
public java.lang.String getClientId()
Copyright © 2013, 2014, Oracle and/or its affiliates. All rights reserved.