【发布时间】:2019-06-20 17:54:14
【问题描述】:
我有 Google Indexing API 的问题。 我是初级开发人员。 T.T
我在 Java 源代码中使用索引 API。 当前 API 调用成功地为一个站点(无批次)编制索引请求。 但是我不能做Request Batch。
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();
}
}
}
【问题讨论】:
标签: java api indexing google-cloud-platform