Swan Lake Beta3 is here.
import ballerina/io;

// Defines the gRPC client to call the OAuth2 secured APIs.
// The client metadata is enriched with the `Authorization: Bearer <token>`
// header by passing the [`grpc:OAuth2RefreshTokenGrantConfig`](https://docs.central.ballerina.io/ballerina/grpc/latest/records/OAuth2RefreshTokenGrantConfig) for the `auth`
// configuration of the client.
HelloWorldClient securedEP = check new("https://localhost:9090",
    auth = {
        refreshUrl: "https://localhost:9445/oauth2/token",
        refreshToken: "24f19603-8565-4b5f-a036-88a945e1f272",
        clientId: "FlfJYKBD2c925h4lkycqNZlC2l4a",
        clientSecret: "PJz0UhTJMrHOo68QQNpvnqAY_3Aa",
        scopes: ["admin"],
        clientConfig: {
            secureSocket: {
                cert: "../resource/path/to/public.crt"
            }
        }
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

public function main() returns error? {
    string result = check securedEP->hello();
    io:println(result);
}

Client - OAuth2 Refresh Token Grant Type

A client, which is secured with an OAuth2 refresh token grant type can be used to connect to a secured service.
The client metadata is enriched with the Authorization: Bearer <token> header by passing the grpc:OAuth2RefreshTokenGrantConfig to the auth configuration of the client.

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

syntax = "proto3";
import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";

This is the service definition for the scenario.

service HelloWorld {
	rpc hello(google.protobuf.Empty) returns (google.protobuf.StringValue);
}
# Create a new Protocol Buffers definition file named `grpc_client.proto` and add the service definition to it.
# Run the command below in the Ballerina tools distribution for stub generation.
bal grpc --input grpc_client.proto --output stubs
# Once you run the command, `grpc_client_pb.bal` file is generated inside stubs directory.
# For more information on how to use the Ballerina Protocol Buffers tool, see the [Proto To Ballerina](https://ballerina.io/learn/by-example/proto-to-ballerina.html) example.
import ballerina/io;
HelloWorldClient securedEP = check new("https://localhost:9090",
    auth = {
        refreshUrl: "https://localhost:9445/oauth2/token",
        refreshToken: "24f19603-8565-4b5f-a036-88a945e1f272",
        clientId: "FlfJYKBD2c925h4lkycqNZlC2l4a",
        clientSecret: "PJz0UhTJMrHOo68QQNpvnqAY_3Aa",
        scopes: ["admin"],
        clientConfig: {
            secureSocket: {
                cert: "../resource/path/to/public.crt"
            }
        }
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

Defines the gRPC client to call the OAuth2 secured APIs. The client metadata is enriched with the Authorization: Bearer <token> header by passing the grpc:OAuth2RefreshTokenGrantConfig for the auth configuration of the client.

public function main() returns error? {
    string result = check securedEP->hello();
    io:println(result);
}
# Create a Ballerina package.
# Copy the generated `grpc_secured_pb.bal` stub file to the package.
# For example, if you create a package named `client`, copy the stub file to the `client` package.
# Create a new `grpc_client_oauth2_refresh_token_grant_type.bal` Ballerina file inside the `client` package and add the client implementation.
# Execute the command below to build the 'client' package.
# You may need to change the trusted certificate file path.
`bal build client`
# Run the client using the command below.
# As a prerequisite, start a sample service secured with OAuth2.
bal run client/target/bin/client.jar
Hello, World!
import ballerina/io;

// Defines the gRPC client to call the OAuth2 secured APIs.
// The client metadata is enriched with the `Authorization: Bearer <token>`
// header by passing the [`grpc:OAuth2RefreshTokenGrantConfig`](https://docs.central.ballerina.io/ballerina/grpc/latest/records/OAuth2RefreshTokenGrantConfig) for the `auth`
// configuration of the client.
HelloWorldClient securedEP = check new("https://localhost:9090",
    auth = {
        refreshUrl: "https://localhost:9445/oauth2/token",
        refreshToken: "24f19603-8565-4b5f-a036-88a945e1f272",
        clientId: "FlfJYKBD2c925h4lkycqNZlC2l4a",
        clientSecret: "PJz0UhTJMrHOo68QQNpvnqAY_3Aa",
        scopes: ["admin"],
        clientConfig: {
            secureSocket: {
                cert: "../resource/path/to/public.crt"
            }
        }
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

public function main() returns error? {
    string result = check securedEP->hello();
    io:println(result);
}

Client - OAuth2 Refresh Token Grant Type

A client, which is secured with an OAuth2 refresh token grant type can be used to connect to a secured service.
The client metadata is enriched with the Authorization: Bearer <token> header by passing the grpc:OAuth2RefreshTokenGrantConfig to the auth configuration of the client.

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

syntax = "proto3";
import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";

This is the service definition for the scenario.

service HelloWorld {
	rpc hello(google.protobuf.Empty) returns (google.protobuf.StringValue);
}
# Create a new Protocol Buffers definition file named `grpc_client.proto` and add the service definition to it.
# Run the command below in the Ballerina tools distribution for stub generation.
bal grpc --input grpc_client.proto --output stubs
# Once you run the command, `grpc_client_pb.bal` file is generated inside stubs directory.
# For more information on how to use the Ballerina Protocol Buffers tool, see the [Proto To Ballerina](https://ballerina.io/learn/by-example/proto-to-ballerina.html) example.
import ballerina/io;
HelloWorldClient securedEP = check new("https://localhost:9090",
    auth = {
        refreshUrl: "https://localhost:9445/oauth2/token",
        refreshToken: "24f19603-8565-4b5f-a036-88a945e1f272",
        clientId: "FlfJYKBD2c925h4lkycqNZlC2l4a",
        clientSecret: "PJz0UhTJMrHOo68QQNpvnqAY_3Aa",
        scopes: ["admin"],
        clientConfig: {
            secureSocket: {
                cert: "../resource/path/to/public.crt"
            }
        }
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

Defines the gRPC client to call the OAuth2 secured APIs. The client metadata is enriched with the Authorization: Bearer <token> header by passing the grpc:OAuth2RefreshTokenGrantConfig for the auth configuration of the client.

public function main() returns error? {
    string result = check securedEP->hello();
    io:println(result);
}
# Create a Ballerina package.
# Copy the generated `grpc_secured_pb.bal` stub file to the package.
# For example, if you create a package named `client`, copy the stub file to the `client` package.
# Create a new `grpc_client_oauth2_refresh_token_grant_type.bal` Ballerina file inside the `client` package and add the client implementation.
# Execute the command below to build the 'client' package.
# You may need to change the trusted certificate file path.
`bal build client`
# Run the client using the command below.
# As a prerequisite, start a sample service secured with OAuth2.
bal run client/target/bin/client.jar
Hello, World!
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand