简体   繁体   English

GCP - Python 创建 IoT 设备 PermissionDenied

[英]GCP - Python create IoT device PermissionDenied

I am trying to create IoT devices on the Google Cloud through a Python script.我正在尝试通过 Python 脚本在 Google Cloud 上创建物联网设备。 I have set up the project, IoT registry and authenticated my GCloud and linked the GOOGLE_APPLICATION_CREDENTIALS to the json from the corresponding service account.我已经设置了项目、物联网注册表并验证了我的 GCloud,并将 GOOGLE_APPLICATION_CREDENTIALS 链接到相应服务帐户的 json。 Why I use the command line to create an account, eg gcloud iot devices create dev01 --project=... --region=... --registry=... , it works.为什么我使用命令行创建一个帐户,例如gcloud iot devices create dev01 --project=... --region=... --registry=... ,它有效。 However, my Python script (run through command prompt) does not seem to yield the same results.但是,我的 Python 脚本(通过命令提示符运行)似乎没有产生相同的结果。 I used https://cloud.google.com/iot/docs/samples/device-manager-samples#iot-core-create-rs256-python for the iot_v1 reference.我使用https://cloud.google.com/iot/docs/samples/device-manager-samples#iot-core-create-rs256-python作为 iot_v1 参考。

    # Generate Key
    key = rsa.generate_private_key(backend=default_backend(), public_exponent=65537, key_size=2048)
    # Get Public
    public_key = key.public_key().public_bytes(serialization.Encoding.OpenSSH, serialization.PublicFormat.OpenSSH)
    # Get Private
    pem = key.private_bytes(encoding=serialization.Encoding.PEM,
                            format=serialization.PrivateFormat.TraditionalOpenSSL,
                            encryption_algorithm=serialization.NoEncryption())
    # Decode to UTF-8
    private_key_str = pem.decode('utf-8')
    public_key_str = public_key.decode('utf-8')
    # Write keys
    with open('Key Pairs/'+deviceName+'_private.pem', 'wb') as file:
        file.write(pem)
    with open('Key Pairs/' + deviceName + '_public.pem', 'wb') as file:
        file.write(public_key)

    # Create Device
    client = iot_v1.DeviceManagerClient()
    parent = client.registry_path(PROJECTID, REGION, REGISTRY)
    deviceTemplate = {
        'id': deviceName,
        "credentials": [
            {
                "public_key": {
                    "format": iot_v1.PublicKeyFormat.RSA_X509_PEM,
                    "key": public_key_str,
                }
            }
        ]
    }
    client.create_device(request={'parent': parent, 'device': deviceTemplate})

The error traceback is错误回溯是

File "commissioning.py", line 46, in <module>
    client.create_device(request={'parent': parent, 'device': deviceTemplate})
  File "C:\Users\Niels\AppData\Local\Programs\Python\Python38\lib\site-packages\google\cloud\iot_v1\services\device_manager\client.py", line 728, in create_device
    response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
  File "C:\Users\Niels\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\gapic_v1\method.py", line 145, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Users\Niels\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\grpc_helpers.py", line 59, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission

I guess it is either a problem the way I use iot_v1 or with the permissions of Python. Any help/tips would be much appreciated!我想这要么是我使用 iot_v1 的方式有问题,要么是 Python 的权限有问题。任何帮助/提示将不胜感激!

Make sure that the service account being used to create the client has at least the roles/cloudiot.provisioner Role assigned (if you keep getting the permission errors try adding the roles/cloudiot.admin role to the service account, as it should grant full control of all IoT devices, find more information about all the available permissions here .)确保用于创建客户端的服务帐户至少分配了roles/cloudiot.provisioner角色(如果您不断收到权限错误,请尝试将roles/cloudiot.admin角色添加到服务帐户,因为它应该授予完整权限控制所有物联网设备,在此处找到有关所有可用权限的更多信息。)

Once you are sure that the service account has the correct permissions you can take advantage of the the credentials parameter offered by the iot_v1.DeviceManagerClient() class to make sure you point to the service account key file as explained on the Authentication section of the docs.一旦您确定服务帐户具有正确的权限,您就可以利用iot_v1.DeviceManagerClient() class提供的credentials参数来确保您指向服务帐户密钥文件,如文档的身份验证部分所述.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM