diff --git a/cloud/cpp/ConnectByOAuth2.cc b/cloud/cpp/ConnectByOAuth2.cc index 92ca8db..ba25700 100644 --- a/cloud/cpp/ConnectByOAuth2.cc +++ b/cloud/cpp/ConnectByOAuth2.cc @@ -17,21 +17,32 @@ * specific language governing permissions and limitations * under the License. */ -#include #include #include using namespace pulsar; int main() { - ClientConfiguration config; - std::string oauthParams = R"({ + // C++ client provides two ways to construct an AuthOauth2 object + // 1. Create by ParamMap (std::map), this way is more simple and efficient + ParamMap params; + params["issuer_url"] = "http://cloud/oauth/token"; + params["private_key"] = "/resources/authentication/token/cpp_credentials_file.json"; + params["audience"] = "https://cloud.auth0.com/api/v2/"; + params["scope"] = "api://pulsar-cluster-1/.default"; // scope is optional + + Client client1("pulsar+ssl://streamnative.cloud:6651", + ClientConfiguration().setAuth(AuthOauth2::create(params))); + client1.close(); + + // 2. Create by JSON string, it will be parsed to a ParamMap internally + std::string paramsJson = R"({ "issuer_url": "https://cloud/oauth/token", "private_key": "/resources/authentication/token/cpp_credentials_file.json", - "audience": "https://cloud.auth0.com/api/v2/"})"; - - config.setAuth(pulsar::AuthOauth2::create(params)); + "audience": "https://cloud.auth0.com/api/v2/", + "scope": "api://pulsar-cluster-1/.default"})"; // scope is optional - Client client("pulsar+ssl://streamnative.cloud:6651", config); - client.close(); + Client client2("pulsar+ssl://streamnative.cloud:6651", + ClientConfiguration().setAuth(AuthOauth2::create(paramsJson))); + client2.close(); } diff --git a/cloud/cpp/README.md b/cloud/cpp/README.md index e56df8b..62b9b1e 100644 --- a/cloud/cpp/README.md +++ b/cloud/cpp/README.md @@ -6,6 +6,8 @@ Produce message to and consume message from a Pulsar cluster using [Apache pulsa - CPP Client: 2.6.1+ +Since 2.8.1.10, C++ client has supported configuring `scope` field for OAuth2 authentication. + ## Linux Since 2.1.0 release, Pulsar ships pre-built RPM and Debian packages. You can download and install those packages directly. diff --git a/cloud/python/README.md b/cloud/python/README.md index 6c8675e..3724afc 100644 --- a/cloud/python/README.md +++ b/cloud/python/README.md @@ -77,6 +77,24 @@ sudo python setup.py install Produce message 'message 9 from oauth2 producer' to the pulsar service successfully. ``` +> *NOTE* +> +> Since 2.8.1.10, Python client has supported configuring `scope` field for OAuth2 authentication. +> +> For example, +> +> ```shell +> python3 OAuth2Producer.py \ +> -su "pulsar+ssl://streamnative.cloud:6651 \ +> -t my-topic -n 10 --auth-params '{ +> "issuer_url": "https://auth.streamnative.cloud", +> "private_key": "/path/to/private.key", +> "audience": "urn:sn:pulsar:test-organization-name:test-pulsar-instance-name"}', +> "scope": "api://pulsar-cluster-1/.default" +> }' +> ``` + + ## Connect to the Pulsar cluster with Token authentication plugin In this example, the Python producer publishes data to the `my-topic` in your Pulsar cluster. The consumer receives the message from the `my-topic` and `acknowledges` each received message.