【问题标题】:TypeError: Can't concat bytes to str indexing APITypeError:无法将字节连接到 str 索引 API
【发布时间】:2019-11-21 16:37:47
【问题描述】:

我正在尝试使用 python 使用谷歌索引 API,但不断收到以下错误:

 'TypeError: Can't concat bytes to str'

这是我目前编写的代码:

SCOPES = [ "https://www.googleapis.com/auth/indexing" ]
ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
JSON_KEY_FILE = "json_key.json"

credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)

http = credentials.authorize(httplib2.Http())

content = {"url" : "https://careers.google.com/jobs/google/technical-writer",
            "type" : "URL_UPDATED"
            }

response, content = http.request(ENDPOINT, method="POST", body=content)

有谁知道我为什么会收到这个错误以及如何解决它?

【问题讨论】:

    标签: python google-api urllib2


    【解决方案1】:

    以下行会导致问题,因为 body 需要 byte,但您提供的是 dict

    response, content = http.request(ENDPOINT, method="POST", body=content)
    

    要解决此问题,您需要将 content 编码为 JSON,最后将其转换为 byte

    response, content = http.request(ENDPOINT, method="POST", body=bytes(json.dumps(content))
    

    【讨论】:

      猜你喜欢
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      相关资源
      最近更新 更多