Sean edited this page May 3, 2017 · 11 revisions

Welcome to OAuth2 for Servoy This module provides OAuth APIs based on the Google Client OAuth project.

Dependencies

This module relies on the Google Client OAuth libraries for Java Before starting, download the libs and place them in your beans folder

API Documentation

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();

Class Summary

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

AuthorizationRequest

A class that builds and executes an authorization request to the service.

Method Summary

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

Method Detail

setClientID


Params

Type Name Summary Required
String clientID The client ID needed by the auth server Required

returns

AuthorizationRequest

example

var client = scopes.svyOAuth.createAuthorizationRequest();
client.setClientID('abc123');

setClientSecret


Params

Type Name Summary Required
String clientSecret The client secret needed by the auth server Required

returns

AuthorizationRequest

example

var client = scopes.svyOAuth.createAuthorizationRequest();
client.setClientSecret('my-secret');

setTokenServerURL


Params

Type Name Summary Required
String tokenServerURL The URL of the service's token server Required

returns

AuthorizationRequest

example

var client = scopes.svyOAuth.createAuthorizationRequest();
client.setTokenServerURL('http://my-service/token');

setAuthServerURL


Params

Type Name Summary Required
String authServerURL The URL of the service's auth server Required

returns

AuthorizationRequest

example

var client = scopes.svyOAuth.createAuthorizationRequest();
client.setAuthServerURL('http://my-service/auth');

addScope


Params

Type Name Summary Required
String scope The name of a scope that will be authorized Required

returns

AuthorizationRequest

example

var client = scopes.svyOAuth.createAuthorizationRequest();
client.addScope('access-to-some-protected-resource');

execute


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

AuthorizedClient

example

var client = scopes.svyOAuth.createAuthorizationRequest();
client.execute('user-abc');

AuthorizedClient

A class that represents a client session which is already authorized and ready to execute HTTP methods

Method Summary

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

Method Detail

get


Params

Type Name Summary Required
String url The URL target for the GET request Required

returns

HttpRequest

example

var request = client.get('http://my-service');

post


Params

Type Name Summary Required
String url The URL target for the POST request Required

returns

HttpRequest

example

var request = client.post('http://my-service/param/value');

put


Params

Type Name Summary Required
String url The URL target for the PUT request Required

returns

HttpRequest

example

var request = client.put('http://my-service/param/value');

remove


Params

Type Name Summary Required
String url The URL target for the DELETE request Required

returns

HttpRequest

example

var request = client.remove('http://my-service/param/value');

HttpRequest

A class that represents an HTTP request, ready to be executed

Method Summary

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

Method Detail

setURL


Params

Type Name Summary Required
String url The URL target for the HTTP request Required

returns

HttpRequest

example

request.setURL('http://my-service/param/value');

setContent


Params

Type Name Summary Required
String json The body content as a serialized javascript object Required

returns

HttpRequest

example

request.setContent('{"name":"value"}');

addHeader


Params

Type Name Summary Required
String name The name of the HTTP header Required
String value The value of the HTTP header Required

returns

HttpRequest

example

request.addHeader('Cache-Control', 'no-cache');

execute


Params
None

returns

HttpResponse

example

var response = request.execute();

HttpResponse

A class that represents an HTTP response after a request

Method Summary

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

Method Detail

getStatusCode


Params
None

returns

Number The HTTP Status code

example

var response = request.execute();
var code = response.getStatusCode();

getStatusMessage


Params
None

returns

String The HTTP Status message

example

var response = request.execute();
var msg= response.getStatusMessage();

getContent


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();
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.