“用于测试网址移动设备适用性的实验性 API”

Search Console 网址测试工具 API 示例

示例代码通常是学习如何使用 API 的最简单方法。如需获取 Search Console 网址测试工具 API 示例的链接,请在下方选择编程语言。

这些示例使用 Google API 客户端库

如果某个库的示例页面尚未包含 Search Console 网址测试工具 API 的示例,您仍然可以使用该库,或许可以调整为其他 Google API 提供的示例。

Go

此版本的 Search Console 网址 Testing Tools API 没有专门的 Go 示例。

但是,您可以修改其他某个 Go 示例

Java

package codesamples;

import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;

/** Example of Java client calling Search Console URL Testing Tools API */
public final class MftExample {
  public static void main(String[] args) {
    try {
      HttpTransport httpTransport = new NetHttpTransport();
      HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
      GenericUrl url = new GenericUrl(
          "https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run"
          + "?key={YOUR_API_KEY}");
      JSONObject json = new JSONObject();
      json.put("url", "https://www.google.com/");
      String requestBody = json.toString();
      HttpRequest request = requestFactory.buildPostRequest(
          url, ByteArrayContent.fromString("application/json", requestBody));
      System.out.println("request was: " + requestBody);
      HttpResponse httpResponse = request.setReadTimeout(70000).execute();

      String content = httpResponse.parseAsString();
      System.out.println(content);
    } catch (IOException | JSONException ex) {
      ex.printStackTrace();
    }
  }
}

JavaScript

<!DOCTYPE html>
<html>
<head>
<script>
function listMobileFriendlyIssues(responseJson) {
  var resultEl = document.getElementById('result');
  if (responseJson.mobileFriendlyIssues) {
    resultEl.innerHTML += "Mobile friendly issues:<br>";
    responseJson.mobileFriendlyIssues.forEach(function(issue) {
        resultEl.innerHTML += "<li>" + issue.rule + "</li>"});
  } else {
    resultEl.innerHTML += "No mobile friendliness issues found!";
  }
}
function runInspection() {
  var apiUrl = 'https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key={YOUR_API_KEY}';
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("POST", apiUrl);
  xmlhttp.setRequestHeader("Content-Type", "application/json");
  xmlhttp.send(
      '{"url": "' + document.getElementById("url").value + '", "requestScreenshot": true}');
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4) {
      var responseJson = JSON.parse(this.responseText);
      if (this.status == 200) {
        listMobileFriendlyIssues(responseJson);
        document.getElementById('image').src =
            "data:" + responseJson.screenshot.mimeType + ';base64,' + responseJson.screenshot.data;
      } else {
        document.getElementById('result').innerHTML += "An error occured!<br>Error code: "
            + responseJson.error.code + "<br>Error message: " + responseJson.error.message;
      }
    }
  };
};
</script>
</head>
<body>
<br>URL: <input type="text" id="url" size="40">
<br><br><button onclick="runInspection();">Fetch</button>
<br><br>Result:
<div id="result" style="border: solid thin black; height:5em; overflow:auto;"></div>
Screenshot:
<img id="image" style="border: solid thin black;"/>
</body>
</html>

PHP

此版本的 Search Console 网址 Testing Tools API 没有专门针对 PHP 的示例。

但是,您可以修改其他某个 PHP 示例

Python

"""Example of Python client calling Search Console URL Testing Tools API."""
import urllib
import urllib2

api_key = '{YOUR_API_KEY}'
request_url = 'http://www.google.com/'
service_url = 'https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run'
params = {
    'url': request_url,
    'key': api_key,
}
data = urllib.urlencode(params)
content = urllib2.urlopen(url=service_url, data=data).read()
print(content)

Ruby

此版本的 Search Console 网址 Testing Tools API 没有专门的 Ruby 示例。

但是,您可以修改其他某个 Ruby 示例