简体   繁体   English

使用Google Safe Browsing API v4和Python请求发生意外响应

[英]Unexpected response with Google Safe Browsing API v4 and Python requests

I'm trying to implement a small function to verify possible phishing URL's and thought that using Google Safe Browsing API would be a good start. 我正在尝试实现一个小功能来验证可能的网上诱骗网址,并认为使用Google Safe Browsing API将是一个良好的开端。

After reading the API documentation I thought I had a handle on things and cobbled together the following code: 在阅读了API文档之后,我认为我已经掌握了一些东西并拼凑了以下代码:

import requests
import json

url = "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=<REDACTED>"
headers = {'content-type': 'application/json'}

payload = {'client': {'clientId': "mycompany", 'clientVersion': "0.1"},
        'threatInfo': {'threatTypes': ["SOCIAL_ENGINEERING", "MALWARE"],
                       'platformTypes': ["ANY_PLATFORM"],
                       'threatEntryTypes': ["URL"],
                       'threatEntries:': [{'url': "http://www.urltocheck1.org"}]}}

print (json.dumps(payload, indent=4))

r = requests.post(url, headers=headers, json=payload)

If I do a 如果我做了

print (json.dumps(payload, indent=4) print(json.dumps(payload,indent = 4)

it all looks ok. 一切看起来都不错。 However, the reply I get back from Google doesn't agree. 但是,我从Google收到的回复不同意。

{'error': {'message': 'Invalid JSON payload received. {'error':{'message':'收到无效的JSON有效负载。 Unknown name "threat_entries:" at \\'threat_info\\': Cannot find field.', 'status': 'INVALID_ARGUMENT', 'code': 400, 'details': [{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'threat_info', 'description': 'Invalid JSON payload received. \\'threat_info \\'的未知名称“threat_entries:”:无法找到字段。','status':'INVALID_ARGUMENT','code':400,'details':[{'@ type':'type.googleapis.com /google.rpc.BadRequest','fieldViolations':[{'field':'threat_info','description':'收到无效的JSON有效负载。 Unknown name "threat_entries:" at \\'threat_info\\': Cannot find field.'}]}]}} {'X-Frame-Options': 'SAMEORIGIN', 'Transfer-Encoding': 'chunked', 'Cache-Control': 'private', 'Date': 'Tue, 25 Oct 2016 07:55:30 GMT', 'Content-Type': 'application/json; \\'threat_info \\'的未知名称“threat_entries:”:无法找到字段。'}]}]}} {'X-Frame-Options':'SAMEORIGIN','转码':'chunked','缓存 - 控制':'私人','日期':'星期二,2016年10月25日07:55:30 GMT','内容类型':'application / json; charset=UTF-8', 'Alt-Svc': 'quic=":443"; charset = UTF-8','Alt-Svc':'quic =“:443”; ma=2592000; MA = 2592000; v="36,35,34,33,32"', 'X-Content-Type-Options': 'nosniff', 'Content-Encoding': 'gzip', 'X-XSS-Protection': '1; v =“36,35,34,33,32”','X-Content-Type-Options':'nosniff','Content-Encoding':'gzip','X-XSS-Protection':'1; mode=block', 'Server': 'ESF'} application/json; mode = block','Server':'ESF'} application / json; charset=UTF-8 字符集= UTF-8

I can't - as usual - spot my mistake. 我不能 - 像往常一样 - 发现我的错误。 Can someone else spot it and possibly put me on the correct track? 其他人可以发现它并可能把我放在正确的轨道上吗?

Just remove unnecessary colon after threatEntries and it should work just fine. 只需在threatEntries之后删除不必要的冒号,它应该可以正常工作。

Also if you are using requests version 2.4.2 or newer you do not need to insert content-type header to your code, instead you could move your key to the params section: 此外,如果您使用的是2.4.2或更新版本的requests ,则无需在代码中插入content-type标题,而是可以将密钥移至params部分:

import requests
import json

api_key='your_key'
url = "https://safebrowsing.googleapis.com/v4/threatMatches:find"
payload = {'client': {'clientId': "mycompany", 'clientVersion': "0.1"},
        'threatInfo': {'threatTypes': ["SOCIAL_ENGINEERING", "MALWARE"],
                       'platformTypes': ["ANY_PLATFORM"],
                       'threatEntryTypes': ["URL"],
                       'threatEntries': [{'url': "http://www.urltocheck1.org"}]}}
params = {'key': api_key}
r = requests.post(url, params=params, json=payload)
# Print response
print(r) 
print(r.json())

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

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