我一直在为此研究一个工作示例,但没有找到任何示例。
我参考了以下链接 Stackoverflow Link和Google Official Docs
从这些文档中,我确实了解到我需要实现这一点
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.model.CacheInvalidationRule;
import com.google.api.services.compute.model.Operation;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
public class ComputeExample {
public static void main(String args[]) throws IOException, GeneralSecurityException {
// Project ID for this request.
String project = "my-project"; // TODO: Update placeholder value.
// Name of the UrlMap scoping this request.
String urlMap = "my-url-map"; // TODO: Update placeholder value.
// TODO: Assign values to desired fields of `requestBody`:
CacheInvalidationRule requestBody = new CacheInvalidationRule();
Compute computeService = createComputeService();
Compute.UrlMaps.InvalidateCache request =
computeService.urlMaps().invalidateCache(project, urlMap, requestBody);
Operation response = request.execute();
// TODO: Change code below to process the `response` object:
System.out.println(response);
}
public static Compute createComputeService() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential =
credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
}
return new Compute.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Google-ComputeSample/0.1")
.build();
}
}
但是,如果您看到这个例子,它只有占位符值。
如果我想刷新名为https://mywebsite.com/homepage.html的页面的缓存, 我应该在上面的代码中哪里输入这些信息?
我在这里添加了吗
credential.createScoped(Arrays.asList("https://mywebsite.com/homepage.html"));
或者我应该在 UrlMaps 中添加它吗?这非常令人困惑。