-1

我正在尝试从 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 包来完成大部分繁重的工作,但仍然是一个空洞的回应。我应该补充一点,我设法获得了一些确实包含恶意软件的网址,并且通过谷歌的透明度报告网址搜索检测到: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/"})
}
4

1 回答 1

0

我认为这是在文档中说明的

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

于 2021-12-02T04:38:42.837 回答