How to connect StreamNative Cloud with Python client?

In StreamNative Cloud console it provides demo for C++, Go and Java clients to connect StreamNative Cloud. However, all of them are compiled language, and I’d like to explore with Python client via REPL.

Although, connect to StreamNative Cloud requires authentication via OAuth2, I don’t find an example from user doc or api doc.

I notice that Python client wraps C++ client. So I read the API doc and follow C++ client example:

#include <iostream>
#include <pulsar/Client.h>

using namespace pulsar;

int main() {
    ClientConfiguration config;
    std::string params = R"({
    "issuer_url": "https://auth.streamnative.cloud/",
    "private_key": "YOUR-KEY-FILE-PATH",
    "audience": "<audience-value>"})";

    config.setAuth(pulsar::AuthOauth2::create(params));

    Client client("pulsar+ssl://<cluster-hostname>:6651", config);
    client.close();
}

writing:

import pulsar

auth = auth = pulsar.AuthenticationOauth2('{"issuer_url": "https://auth.streamnative.cloud/","private_key": "YOUR-KEY-FILE-PATH","audience": "<audience-value>"}')
client = pulsar.Client('pulsar+ssl://<cluster-hostname>:6651', authentication=auth)
producer = client.create_producer('test')

and I got:

2022-07-26 22:50:04.273 INFO  [0x1008a8580] ClientConnection:189 | [<none> -> pulsar+ssl://<cluster-hostname>:6651] Create ClientConnection, timeout=10000
2022-07-26 22:50:06.027 ERROR [0x1008a8580] AuthOauth2:213 | Response failed for getting the well-known configuration https://auth.streamnative.cloud/. response Code 404
2022-07-26 22:50:06.028 INFO  [0x1008a8580] ConnectionPool:96 | Created connection for pulsar+ssl://<cluster-hostname>:6651
2022-07-26 22:50:06.191 INFO  [0x16f8a7000] ClientConnection:375 | [198.18.0.6:64264 -> 198.19.32.75:6651] Connected to broker
2022-07-26 22:50:07.654 ERROR [0x16f8a7000] ClientConnection:473 | [198.18.0.6:64264 -> 198.19.32.75:6651] Failed to establish connection: AuthenticationError
2022-07-26 22:50:07.654 INFO  [0x16f8a7000] ClientConnection:1559 | [198.18.0.6:64264 -> 198.19.32.75:6651] Connection closed
2022-07-26 22:50:07.654 ERROR [0x16f8a7000] ClientImpl:192 | Error Checking/Getting Partition Metadata while creating producer on persistent://public/default/test -- AuthenticationError
2022-07-26 22:50:07.654 INFO  [0x16f8a7000] ClientConnection:263 | [198.18.0.6:64264 -> 198.19.32.75:6651] Destroyed connection
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/lib/python3.9/site-packages/pulsar/__init__.py", line 603, in create_producer
    p._producer = self._client.create_producer(topic, conf)
_pulsar.AuthenticationError: Pulsar error: AuthenticationError

I don’t know it’s a network issue or something I get wrong.

FYI, follow the Go client example I can produce and consume properly.

[fix][auth] Generate correct well-known OpenID configuration URL by gaoran10 · Pull Request #15928 · apache/pulsar · GitHub might fix the bug. Before this PR, the issuer url cannot have a slash suffix.

@BewareMyPower thank you! Remove the slash of issue_url works around the issue for 2.10.1 :slight_smile: