import ballerina/http;
import ballerina/log;

// Defines the HTTP client to call the OAuth2 secured APIs.
// The client is enriched with the `Authorization: Bearer <token>` header by
// passing the `http:ClientCredentialsGrantConfig` for the `auth` configuration
// of the client.
http:Client securedEP = check new("https://localhost:9090", {
    auth: {
        tokenUrl: "https://localhost:9090/oauth2/token",
        clientId: "s6BhdRkqt3",
        clientSecret: "7Fjfp0ZBr1KtDRbnfVdmIw",
        scopes: ["hello"],
        clientConfig: {
            secureSocket: {
                trustStore: {
                    path: "../resources/ballerinaTruststore.p12",
                    password: "ballerina"
                }
            }
        }
    },
    secureSocket: {
        trustStore: {
            path: "../resources/ballerinaTruststore.p12",
            password: "ballerina"
        }
    }
});

public function main() {
    // Send a `GET` request to the specified endpoint.
    var response = securedEP->get("/foo/bar");
    if (response is http:Response) {
        log:print(response.statusCode.toString());
    } else if (response is http:ClientError) {
        log:printError("Failed to call the endpoint.", err = response);
    }
}

Secured Client with OAuth2 Client Credentials Grant Type

A client, which is secured with OAuth2 client credentials grant type can be used to connect to a secured service.
The client is enriched with the Authorization: Bearer <token> header by passing the http:OAuth2ClientCredentialsGrantConfig for the auth configuration of the client.

For more information on the underlying module, see the OAuth2 module.

import ballerina/http;
import ballerina/log;
http:Client securedEP = check new("https://localhost:9090", {
    auth: {
        tokenUrl: "https://localhost:9090/oauth2/token",
        clientId: "s6BhdRkqt3",
        clientSecret: "7Fjfp0ZBr1KtDRbnfVdmIw",
        scopes: ["hello"],
        clientConfig: {
            secureSocket: {
                trustStore: {
                    path: "../resources/ballerinaTruststore.p12",
                    password: "ballerina"
                }
            }
        }
    },
    secureSocket: {
        trustStore: {
            path: "../resources/ballerinaTruststore.p12",
            password: "ballerina"
        }
    }
});

Defines the HTTP client to call the OAuth2 secured APIs. The client is enriched with the Authorization: Bearer <token> header by passing the http:ClientCredentialsGrantConfig for the auth configuration of the client.

public function main() {
    var response = securedEP->get("/foo/bar");
    if (response is http:Response) {
        log:print(response.statusCode.toString());
    } else if (response is http:ClientError) {
        log:printError("Failed to call the endpoint.", err = response);
    }
}

Send a GET request to the specified endpoint.

# Before testing this sample, first start a sample service secured with OAuth2.
# To run this sample, navigate to the directory that contains the `.bal` file,
# and execute the `bal run` command below.
# (You may need to change the keystore path. A sample keystore file is
# available in the distribution.
# The file path is <ballerina.home>/examples/resources/ballerinaKeystore.p12)
bal run secured_client_with_oauth2_password_grant_type.bal
time = 2021-01-20 20:04:13,261 level = INFO  module = "" message = "200"