Google Indexing API 请求批处理 在 Java 中



我有谷歌索引API问题。我是初级开发人员。T.T

我正在使用Java源代码中的索引API。当前 API 调用已成功为一个站点(无批处理)的请求编制索引。但我不能做请求批处理。

https://developers.google.com/api-client-library/java/google-api-java-client/batch

不要遵循首页上的示例源。页面太差,无法解释。

https://productforums.google.com/forum/#!topic/webmasters/L6yVB7iq1os;context-place=topicsearch/indexing$20api$20request$20batch

上面页面上的示例来源答案也是相同的。

上面的源找不到插入方法。你可以帮我吗?

以下是我的消息来源。


import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.batch.BatchRequest;
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.http.*;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Lists;
import com.google.api.services.indexing.v3.Indexing;
import com.google.api.services.indexing.v3.model.UrlNotification;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
public class Main {
    public static List<HttpResponse> addedCalendarsUsingBatch = Lists.newArrayList();
    public static final String appName = "appName";
    private static com.google.api.services.calendar.Calendar client;
    public static void main(String[] args) {
        try {
            Main main = new Main();
            main.accessToken();
        } catch (Exception e) {
        }
    }
    public void accessToken() {
        try {
            HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            String scopes = "https://www.googleapis.com/auth/indexing";
            String endPoint = "https://indexing.googleapis.com/v3/urlNotifications:publish";
            JsonFactory jsonFactory = new JacksonFactory();
            // service_account_file.json is the private key that you created for your service account.
            File file = new File("/path/key.json");
            InputStream in = new FileInputStream(file);
            GoogleCredential credentials =
                    GoogleCredential.fromStream(in, httpTransport, jsonFactory)
                            .createScoped(Collections.singleton(scopes));
            GenericUrl genericUrl = new GenericUrl(endPoint);
            HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
            // Define content here. The structure of the content is described in the next step.
            String content = "{"
                    + ""url": "indexing URL Address","
                    + ""type": "URL_UPDATED","
                    + "}";
            HttpRequest request =
                    requestFactory
                            .buildPostRequest(genericUrl, ByteArrayContent.fromString("application/json", content));
            credentials.initialize(request);
            HttpResponse response = request.execute();
            /**
             * Success so far. Problems from below
             * Batch start
             *
             * */
            String batchEndpoint = "https://indexing.googleapis.com/batch";
            GenericUrl batchGenericUrl = new GenericUrl(batchEndpoint);
            BatchRequest batch = new BatchRequest(httpTransport, httpRequestInitializer);
            batch.setBatchUrl(new GenericUrl(batchEndpoint));
            // Google Sample batch source
            // https://productforums.google.com/forum/#!topic/webmasters/L6yVB7iq1os;context-place=topicsearch/indexing$20api$20request$20batch
            Indexing client = Indexing.builder(transport, jsonFactory, credential).setApplicationName("BatchExample/1.0").build();
            BatchRequest batch = client.batch();
            UrlNotification entry1 = new UrlNotification().setUrl("http://foo.com/");
            client.urlNotifications().insert(entry1).queue(batch, callback);
            UrlNotification entry2 = new UrlNotification().setUrl("http://foo.com/page2");
            client.urlNotifications().insert(entry2).queue(batch, callback);
            batch.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

您需要使用client.urlNotifications().publish(unf).queue(batch, callback);而不是插入。工作代码示例供您参考。

private static void requestBatchIndex(List<String> urls) throws Exception {
    String scopes = "https://www.googleapis.com/auth/indexing";
    String endPoint = "https://indexing.googleapis.com/v3/urlNotifications:publish";
    JsonFactory jsonFactory = new JacksonFactory();
    HttpTransport httpTransport = new NetHttpTransport();
    // service_account_file.json is the private key that you created for
    // your service account.
    InputStream in = FileUtils.openInputStream(new File("src/main/resources/service_account_file.json"));
    GoogleCredential credentials = GoogleCredential.fromStream(in, httpTransport, jsonFactory).createScoped(Collections.singleton(scopes));
    JsonBatchCallback<PublishUrlNotificationResponse> callback = new JsonBatchCallback<PublishUrlNotificationResponse>() {
        public void onSuccess(PublishUrlNotificationResponse res, HttpHeaders responseHeaders) {
            try {
                System.out.println(res.toPrettyString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
            System.out.println("Error Message: " + e.getMessage());
        }
    };
    IndexingRequestInitializer iri = new IndexingRequestInitializer();
    Indexing client = new Indexing(httpTransport, jsonFactory, credentials);
    BatchRequest batch = client.batch();
    batch.setBatchUrl(new GenericUrl("https://indexing.googleapis.com/batch"));
    for (String url : urls) {
        UrlNotification unf = new UrlNotification();
        unf.setUrl(url);
        unf.setType("URL_UPDATED");
        client.urlNotifications().publish(unf).queue(batch, callback);
    }
    batch.execute();
}

最新更新