python - 谷歌数据泄露防护 : "ValueError: Protocol message Value has no "stringValue"field.“

标签 python google-cloud-platform google-cloud-dlp

我有一种方法可以为 Google 的 DLP inspect API 构建多个项目的表,该表可以采用 ContentItemtablevalues

以下是请求的构造方式:

def redact_text(text_list):
    dlp = google.cloud.dlp.DlpServiceClient()
    project = 'my-project'
    parent = dlp.project_path(project)
    items = build_item_table(text_list)

    info_types = [{'name': 'EMAIL_ADDRESS'}, {'name': 'PHONE_NUMBER'}]
    inspect_config = {
        'min_likelihood': "LIKELIHOOD_UNSPECIFIED",
        'include_quote': True,
        'info_types': info_types
    }
    response = dlp.inspect_content(parent, inspect_config, items)

    return response


def build_item_table(text_list):
    rows = []
    for item in text_list:
        row = {"values": [{"stringValue": item}]}
        rows.append(row)
    table = {"table": {"headers": [{"name": "something"}], "rows": rows}}
    return table

当我运行此命令时,我收到错误ValueError:协议(protocol)消息值没有“stringValue”字段。即使this exampledocs另有说明。

我构建请求的方式有问题吗?

编辑:这是 build_item_table 的输出

{
    'table':
    {
        'headers': 
        [
            {'name': 'value'}
        ], 
        'rows': 
        [
            {
                'values': 
                [
                    {
                        'stringValue': 'My name is Jenny and my number is (555) 867-5309, you can also email me at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f6f9f2faf6fefbd7f0faf6fefbb9f4f8fa" rel="noreferrer noopener nofollow">[email protected]</a>, another email you can reach me at is <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5a525e56537f5a525e5653115c5052" rel="noreferrer noopener nofollow">[email protected]</a>.  '
                    }
                ]
            }, 
            {
                'values': 
                [
                    {
                        'stringValue': 'Jimbob Doe (555) 111-1233, that one place down the road <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="097a66646c566c64686065497068616666276a6664" rel="noreferrer noopener nofollow">[email protected]</a>'
                    }
                ]
            }
        ]
    }
}

最佳答案

尝试 string_value .... python 使用字段名称,而不是类型名称。

关于python - 谷歌数据泄露防护 : "ValueError: Protocol message Value has no "stringValue"field.“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50277963/

相关文章:

Python:以可能不存在的对象的属性为条件

firebase - Firestore查询多个父文档的所有子集合

java - 无法找到或加载主类 com.example.dlp.Inspect

java - 如何通过 google Drive api 获取/更改文件敏感度级别(标签)?

python - 使用默认值python设置字典的缺失值

python - Python 3.2 中字符串的 'or' 运算符出现问题

kubernetes - 在 GKE : apiserver was unable to write a JSON response: http2: stream closed 中扩展 HPA 时出错

c - 为什么 Google 的 TrueTime API 难以复制?

python - 如何为 python 配置谷歌云平台数据丢失预防客户端库以在 SSL 代理后面工作?

python - ParentalKey 未链接到 Wagtail CMS 中的所有子类