Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
HTTP client - OAuth2 password grant type
The http:Client
can connect to a service that is secured with the OAuth2 password grant type by adding the Authorization: Bearer <token>
header to each request. The required configurations for this grant type can be specified in the auth
field of the client configuration. Use this grant type when you need to exchange the user's credentials for an access token.
import ballerina/http;
import ballerina/oauth2;
import ballerina/io;
type Album readonly & record {
string title;
string artist;
};
public function main() returns error? {
// Defines the HTTP client to call the OAuth2 secured APIs.
http:Client albumClient = check new ("localhost:9090",
auth = {
tokenUrl: "https://localhost:9445/oauth2/token",
username: "admin",
password: "admin",
clientId: "FlfJYKBD2c925h4lkycqNZlC2l4a",
clientSecret: "PJz0UhTJMrHOo68QQNpvnqAY_3Aa",
scopes: "admin",
refreshConfig: oauth2:INFER_REFRESH_CONFIG,
clientConfig: {
secureSocket: {
cert: "../resource/path/to/public.crt"
}
}
},
secureSocket = {
cert: "../resource/path/to/public.crt"
}
);
Album[] payload = check albumClient->/albums;
io:println(payload);
}
Prerequisites
- • Run the HTTP service given in the OAuth2 service example.
Run the client program by executing the command below.
$ bal run http_client_oauth2_password_grant_type.bal[{"title":"Blue Train","artist":"John Coltrane"},{"title":"Jeru","artist":"Gerry Mulligan"}]
Related links
PreviousOAuth2 client credentials grant type
NextOAuth2 refresh token grant type