Java源码示例:com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential
示例1
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException {
try {
AppIdentityCredential credential = new AppIdentityCredential(Arrays.asList(STORAGE_SCOPE));
// Set up and execute Google Cloud Storage request.
String bucketName = req.getRequestURI();
if (bucketName.equals("/")) {
resp.sendError(
HTTP_NOT_FOUND, "No bucket specified - append /bucket-name to the URL and retry.");
return;
}
// Remove any trailing slashes, if found.
// [START snippet]
String cleanBucketName = bucketName.replaceAll("/$", "");
String uri = GCS_URI + cleanBucketName;
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential);
GenericUrl url = new GenericUrl(uri);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
// [END snippet]
// Display the output XML.
resp.setContentType("text/xml");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(resp.getOutputStream()));
String formattedContent = content.replaceAll("(<ListBucketResult)", XSL + "$1");
writer.append(formattedContent);
writer.flush();
resp.setStatus(HTTP_OK);
} catch (Throwable e) {
resp.sendError(HTTP_NOT_FOUND, e.getMessage());
}
}
示例2
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try {
AppIdentityCredential credential = new AppIdentityCredential(Arrays.asList(STORAGE_SCOPE));
// Set up and execute Google Cloud Storage request.
String bucketName = req.getRequestURI();
if (bucketName.equals("/")) {
resp.sendError(404, "No bucket specified - append /bucket-name to the URL and retry.");
return;
}
// Remove any trailing slashes, if found.
//[START snippet]
String cleanBucketName = bucketName.replaceAll("/$", "");
String URI = GCS_URI + cleanBucketName;
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
//[END snippet]
// Display the output XML.
resp.setContentType("text/xml");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(resp.getOutputStream()));
String formattedContent = content.replaceAll("(<ListBucketResult)", XSL + "$1");
writer.append(formattedContent);
writer.flush();
resp.setStatus(200);
} catch (Throwable e) {
resp.sendError(404, e.getMessage());
}
}
示例3
OauthRawGcsService(OAuthURLFetchService urlfetch, ImmutableSet<HTTPHeader> headers) {
this.urlfetch = checkNotNull(urlfetch, "Null urlfetch");
this.headers = checkNotNull(headers, "Null headers");
AppIdentityCredential cred = new AppIdentityCredential(OAUTH_SCOPES);
storage = new Storage.Builder(new UrlFetchTransport(), new JacksonFactory(), cred)
.setApplicationName(SystemProperty.applicationId.get()).build();
}