Home
Welcome to OAuth2 for Servoy This module provides OAuth APIs based on the Google Client OAuth project.
This module relies on the Google Client OAuth libraries for Java Before starting, download the libs and place them in your beans folder
svyOAuth is an object-oriented API for authenticating per the OAuth2 specification.
Example
// get an authorized HTTP client
var client = scopes.svyOAuth.createAuthorizationRequest()
.setAuthServerURL(myAuthURI)
.setTokenServerURL(myTokenURI
.setClientSecret(clientSecret)
.setClientID(clientID)
.addScope(myScope)
.execute('me'); // arbitrary user name
// execute an HTTP GET to your service endpoint
var res = client.get('http://my-web-service/endpoint').execute();
// get the response body as string
var str = res.getContent();
The API consists of a few key classes.
Class | Summary |
---|---|
AuthorizationRequest | Represents an authorization request to an OAuth server |
AuthorizedClient | Represents an authorized HTTP client session |
HttpRequest | An HTTP method ready to be executed |
HttpResponse | Represents the response returned by an HttpRequest |
A class that builds and executes an authorization request to the service.
Return Type | Method | Description |
---|---|---|
AuthorizationRequest | setClientID | Adds the OAuth client ID to the request |
AuthorizationRequest | setClientSecret | Adds the OAuth client secret to the request |
AuthorizationRequest | setAuthServerURL | Sets the URL of the Authorization server for the authorization flow |
AuthorizationRequest | setTokenServerURL | Sets the URL of the Token server for the authorization flow |
AuthorizationRequest | addScope | Identifies a protected resource that will be accessed |
AuthorizedClient | execute | Executes the authorization request and returns a client |
Params
Type | Name | Summary | Required |
---|---|---|---|
String | clientID | The client ID needed by the auth server | Required |
returns
example
var client = scopes.svyOAuth.createAuthorizationRequest();
client.setClientID('abc123');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | clientSecret | The client secret needed by the auth server | Required |
returns
example
var client = scopes.svyOAuth.createAuthorizationRequest();
client.setClientSecret('my-secret');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | tokenServerURL | The URL of the service's token server | Required |
returns
example
var client = scopes.svyOAuth.createAuthorizationRequest();
client.setTokenServerURL('http://my-service/token');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | authServerURL | The URL of the service's auth server | Required |
returns
example
var client = scopes.svyOAuth.createAuthorizationRequest();
client.setAuthServerURL('http://my-service/auth');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | scope | The name of a scope that will be authorized | Required |
returns
example
var client = scopes.svyOAuth.createAuthorizationRequest();
client.addScope('access-to-some-protected-resource');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | userName | The user name of the service. In some cases this may be an arbitrary value as the user is already identified by the clientID | Required |
returns
example
var client = scopes.svyOAuth.createAuthorizationRequest();
client.execute('user-abc');
A class that represents a client session which is already authorized and ready to execute HTTP methods
Return Type | Method | Description |
---|---|---|
HttpRequest | get | Creates an HTTP GET request |
HttpRequest | post | Creates an HTTP POSTrequest |
HttpRequest | put | Creates an HTTP PUT request |
HttpRequest | remove | Creates an HTTP DELETE request |
Params
Type | Name | Summary | Required |
---|---|---|---|
String | url | The URL target for the GET request | Required |
returns
example
var request = client.get('http://my-service');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | url | The URL target for the POST request | Required |
returns
example
var request = client.post('http://my-service/param/value');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | url | The URL target for the PUT request | Required |
returns
example
var request = client.put('http://my-service/param/value');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | url | The URL target for the DELETE request | Required |
returns
example
var request = client.remove('http://my-service/param/value');
A class that represents an HTTP request, ready to be executed
Return Type | Method | Description |
---|---|---|
HttpRequest | setURL | Sets the URL of the request |
HttpRequest | setContent | Sets the body content of the request |
HttpRequest | addHeader | Adds an HTTP request header |
HttpResponse | execute | Executes the HTTP method and returns a response |
Params
Type | Name | Summary | Required |
---|---|---|---|
String | url | The URL target for the HTTP request | Required |
returns
example
request.setURL('http://my-service/param/value');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | json | The body content as a serialized javascript object | Required |
returns
example
request.setContent('{"name":"value"}');
Params
Type | Name | Summary | Required |
---|---|---|---|
String | name | The name of the HTTP header | Required |
String | value | The value of the HTTP header | Required |
returns
example
request.addHeader('Cache-Control', 'no-cache');
Params
None
returns
example
var response = request.execute();
A class that represents an HTTP response after a request
Return Type | Method | Description |
---|---|---|
[Number | getStatusCode | gets the HTTP status code (200==OK) |
[String | getStatusMessage | gets the HTTP status message |
[String | getContent | gets the response body content as string |
Params
None
returns
Number The HTTP Status code
example
var response = request.execute();
var code = response.getStatusCode();
Params
None
returns
String The HTTP Status message
example
var response = request.execute();
var msg= response.getStatusMessage();
Params
None
returns
String The HTTP response body as string. In most cases, this is serialized javascript.
example
var response = request.execute();
var content = response.getContent();