google-auth-library-python icon indicating copy to clipboard operation
google-auth-library-python copied to clipboard

'encode' error when using secure_authorized_channel with google.auth.transport.requests.Request

Open salrashid123 opened this issue 3 years ago • 1 comments

Manually setting an secure_authorized_channel with a google.auth.transport.requests.Request() object results in the error shown above.

The snippet below attemtps to create a secure_authorized_channel as shown in the docs here but when its actually executed (for pubsub grpc here), you see the encode exception.

Environment details

  • OS: linux
  • Python version: Python 3.10.5 (virtualenv)
  • pip version: pip 22.0.2
  • google-auth version: Version: 2.11.0
  • google-auth-pubsub version Version: 2.13.6

Steps to reproduce

import google.auth.credentials
import google.auth.jwt
import google.auth.transport.grpc
import grpc

import google.auth.transport.requests
from google.auth.transport.requests import AuthorizedSession

from google.cloud import pubsub_v1
from google.pubsub_v1.services.publisher import transports

project_id = "project"

credentials, _ = google.auth.default()
request = google.auth.transport.requests.Request()
channel = google.auth.transport.grpc.secure_authorized_channel(
    credentials,  pubsub_v1.PublisherClient.SERVICE_ADDRESS, request,
    ssl_credentials=grpc.ssl_channel_credentials())

transport = transports.PublisherGrpcTransport(channel=channel)
publisher =pubsub_v1.PublisherClient(transport=transport)

topic_name = 'projects/{project_id}/topics/{topic}'.format(
    project_id=project_id,
    topic='topic1', 
)    

topic = publisher.topic_path(project_id, 'topic1')
data = b'The rain in Wales falls mainly on the snails.'

for n in range(1, 2):
    future = publisher.publish(topic, data)
    print(future.result())
print("Published messages.")

gives

$ python3 main.py 
Traceback (most recent call last):
  File "/home/srashid/Desktop/sdk_custom_header/python/main.py", line 40, in <module>
    channel = google.auth.transport.grpc.secure_authorized_channel(
  File "/home/srashid/Desktop/sdk_custom_header/python/env/lib/python3.10/site-packages/google/auth/transport/grpc.py", line 286, in secure_authorized_channel
    return grpc.secure_channel(target, composite_credentials, **kwargs)
  File "/home/srashid/Desktop/sdk_custom_header/python/env/lib/python3.10/site-packages/grpc/__init__.py", line 2004, in secure_channel
    return _channel.Channel(target, () if options is None else options,
  File "/home/srashid/Desktop/sdk_custom_header/python/env/lib/python3.10/site-packages/grpc/_channel.py", line 1479, in __init__
    _common.encode(target), _augment_options(core_options, compression),
  File "/home/srashid/Desktop/sdk_custom_header/python/env/lib/python3.10/site-packages/grpc/_common.py", line 72, in encode
    return s.encode('utf8')
AttributeError: 'Request' object has no attribute 'encode'

Note, i'm eventually just trying set a custom metadata header for the pubsub grpc request through an interceptor and it seems you need to dive into the low level plumbing for this. If there's a better way to do that, LMK.

salrashid123 avatar Aug 31 '22 13:08 salrashid123

@clundin25

ok, this is a doc bug here

which shows the parameters to use as

secure_authorized_channel(credentials, request, target, ssl_credentials=None, client_cert_callback=None, **kwargs)

but the snippet mixed the positional arugments up

channel = google.auth.transport.grpc.secure_authorized_channel(
    credentials, regular_endpoint, request,
    ssl_credentials=grpc.ssl_channel_credentials())

i.,e the the posision of requests should be the 2nd positional arg.

suggest just showing the name vs posisional

channel = google.auth.transport.grpc.secure_authorized_channel(
    credentials=credentials,  request=request, target=pubsub_v1.PublisherClient.SERVICE_ADDRESS, 
    ssl_credentials=grpc.ssl_channel_credentials())

salrashid123 avatar Aug 31 '22 21:08 salrashid123