Google Safe Browsing v4 API java

Вот код, чтобы узнать, какой URL безопасен для просмотра. Я использовал для этого Google API. Проблема, с которой я столкнулся, заключается в том, что я не могу заставить объект класса SafeBrowsing попасть по заданному URL-адресу. Так что любезно посмотрите, есть ли у кого-нибудь решение.

 public static void main(final String[] args) {
    try {
        final String baseURL = "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=xxx";
        final URL url = new URL(baseURL);

        // Get a URLConnection object, to write to POST method
        final HttpURLConnection connect = (HttpURLConnection) url.openConnection();
        connect.setRequestMethod("POST");
        connect.setRequestProperty("Content-Type", "application/json");
        // Specify connection settings
        connect.setDoInput(true);
        connect.setDoOutput(true);
        final ClientInfo clientInfo = new Ggooggle().getClientInfo();
        final ThreatInfo threatInfo = new Ggooggle().getThreatInfo();
        final FindThreatMatchesRequest request = new FindThreatMatchesRequest();
        request.setClient(clientInfo);
        request.setThreatInfo(threatInfo);

person rajat    schedule 06.10.2017    source источник


Ответы (1)


Попробуй это.

public final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
public static final JacksonFactory GOOGLE_JSON_FACTORY = JacksonFactory.getDefaultInstance();

Safebrowsing.Builder safebrowsingBuilder = new Safebrowsing.Builder(httpTransport, GOOGLE_JSON_FACTORY, null);
Safebrowsing safebrowsing = safebrowsingBuilder.build();
FindThreatMatchesResponse findThreatMatchesResponse = safebrowsing.threatMatches().find(findThreatMatchesRequest).setKey(GOOGLE_API_KEY).execute();

List<ThreatMatch> threatMatches = findThreatMatchesResponse.getMatches();

if (threatMatches != null && threatMatches.size() > 0) {
    for (ThreatMatch threatMatch : threatMatches) {
        threatUrls.add(threatMatch.getThreat().getUrl());
    }
}

Полные примеры кодов: https://github.com/kalinchih/java_google_safebrowsing_v4

person kalin    schedule 28.02.2018
comment
Помогло бы добавить, как строится findThreatMatchesRequest. - person alexod; 28.02.2018
comment
Я добавил полные примеры кодов в github.com/kalinchih/java_google_safebrowsing_v4 - person kalin; 28.02.2018