查询 safebrowsing api 时没有返回任何内容

问题描述 投票:0回答:2

我正在尝试从 google api 获取信息,但响应正文似乎为空。它只是将

{}
输出到控制台。当我使用文档获取请求的有效负载信息时,不确定我哪里出错了:https://developers.google.com/safe-browsing/v4/lookup-api

package main

import (
    "fmt"

    "encoding/json"
    "io/ioutil"
    "net/http"
    "strings"
)

type payload struct {
    Client     client     `json:"client"`
    ThreatInfo threatInfo `json:"threatInfo"`
}

type client struct {
    ClientId      string `json:"clientId"`
    ClientVersion string `json:"clientVersion"`
}

type threatInfo struct {
    ThreatTypes      []string `json:"threatTypes"`
    PlatformTypes    []string `json:"platformTypes"`
    ThreatEntryTypes []string `json:"threatEntryTypes"`
    ThreatEntries    []entry  `json:"threatEntries"`
}

type entry struct {
    URL string `json:"url"`
}

func checkURLs(urls []string) {

    // populate entries
    var entries = []entry{}
    for _, url := range urls {
        entries = append(entries, entry{URL: url})
    }

    data := payload {
        Client: client{
            ClientId:      "myapp",
            ClientVersion: "0.0.1",
        },
        ThreatInfo: threatInfo{
            ThreatTypes:   []string{"MALWARE", "SOCIAL_ENGINEERING", "POTENTIALLY_HARMFUL_APPLICATION"},
            PlatformTypes: []string{"ANY_PLATFORM"},
            ThreatEntryTypes: []string{"URL"},
            ThreatEntries: entries,
        },
    }

    jsonBytes, _ := json.Marshal(data)

    key := "*"
    api := fmt.Sprintf("https://safebrowsing.googleapis.com/v4/threatMatches:find?key=%s", key)
    req, _ := http.NewRequest("POST", api, strings.NewReader(string(jsonBytes)))
    req.Header.Add("Content-Type", "application/json")
    res, _ := http.DefaultClient.Do(req)

    defer res.Body.Close()

    body, err := ioutil.ReadAll(res.Body)
    fmt.Println(res) // 200 OK 
    fmt.Println(err) // nil
    fmt.Println(string(body)) // {}
}

func main() {
    checkURLs([]string{"http://www.urltocheck1.org/", "http://www.urltocheck2.org/"})
}

编辑

我在谷歌上找到了一个 go 包来完成大部分繁重的工作,但仍然是一个空洞的回应。我应该补充一点,我设法获得了一些do包含恶意软件的网址,并且是通过谷歌透明度报告网址搜索检测到的:https://transparencyreport.google.com/safe-browsing/search

既然应该有结果,为什么对我来说却是空的?

package main

import (
    "fmt"
    "github.com/google/safebrowsing"
)

func checkURLs(urls []string) {
    sb, err := safebrowsing.NewSafeBrowser(safebrowsing.Config{
        ID: "myapp",
        Version: "0.0.1",
        APIKey: "*",
    })

    if err != nil {
        fmt.Println(err)
        return
    }

    threats, err := sb.LookupURLs(urls)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println(threats)
}

func main() {
    checkURLs([]string{"http://www.urltocheck1.org/", "http://www.urltocheck2.org/"})
}
go httprequest httpresponse safe-browsing-api
2个回答
0
投票

我认为文档中已经说明了这一点

注意:如果没有匹配项(即,如果在请求中指定的任何列表中都找不到请求中指定的 URL),则 HTTP POST 响应仅在响应正文中返回一个空对象。


-1
投票

同样的问题,解决了吗?

© www.soinside.com 2019 - 2024. All rights reserved.