Step 2: Build the Python Client

  1. Create a Python file in any editor. For example, create a file named PubSubAPIClient.py.

  2. To import these modules, add these import statements at the top of the file.

  3. Set a semaphore at the beginning of the program. Because of the way Python gRPC is designed, the program shuts down immediately if no response comes back in the milliseconds between calling an RPC and the end of the program. By setting a semaphore, you cause the client to keep running indefinitely.

  4. Create a global variable to store the replay ID.

  5. Set up the gRPC channel, and generate the stub.

  6. Retrieve your session token. Replace the username, password, and API login URL with values for your Salesforce org. If you’re using a production instance, the API login URL is https://login.salesforce.com/services/Soap/u/55.0/. If it’s a sandbox, the URL is https://test.salesforce.com/services/Soap/u/55.0/. Send a POST request formatted like this to the login URL.

    • When you copy the code snippet, ensure it aligns with the indented block of code above it. In Python, code blocks are delimited by their indentation.
    • If you haven’t set up a range of trusted IP addresses for your org, append a security token to your password. For more information, see Reset Your Security Token and Set Trusted IP Ranges for Your Organization.
    • The code example uses username and password authentication for simplicity, but we recommend that you use OAuth in production apps. For more information, see Supported Authentication.
  7. Run this client by entering this command on the command line.

    When the request returns, you have XML-formatted data in the response content field, res.content. It contains a session ID wrapped within the <sessionId> tags. It also contains the server URL wrapped within the <serverUrl> tags and the org ID within the <organizationId> tags under <userInfo>. Note those values because you use them in the next step.

    After getting the session information, you can delete or comment out the login code you added in the previous step. To comment out a line in Python, prepend it with #.

  8. Store the authorization information, including session ID, instance URL, and org ID, in a tuple called authmetadata. Each element in this tuple is also a tuple. You use this information when subscribing to a channel or publishing events.

    1. Replace the sessionid placeholder value with the session ID that you got from the previous step.

    2. Replace the instanceurl placeholder value with the first part of the server URL that you got from the previous step, without the path portion. For example, https://MyDomainName.my.salesforce.com.

    3. Replace the tenantid placeholder value with the org ID that you got from the previous step. Alternatively, you can get your organization ID value by following the steps in Find your Salesforce Organization ID in Salesforce Help. For more information about the headers, see Include Authorization Headers in RPC Method Calls.

  9. Generate your stub object like this. PubSubStub comes from the pubsub_api_pb2_grpc.py file, which you generated in the previous step.