我最后一小时面临OAuth2 JWT令牌验证异常(所以没有人可以访问我的应用程序):
java.security.SignatureException:签名长度不正确:得到256但是期望128.我正在使用google-http-client 1.20.0和Java 1.7.0.到目前为止相同的配置 - 任何想法?
Stacktrace
java.security.SignatureException: Signature length not correct: got 256 but was expecting 128
at sun.security.rsa.RSASignature.engineVerify(Unknown Source) ~[na:1.7.0_45]
at java.security.Signature$Delegate.engineVerify(Unknown Source) ~[na:1.7.0_45]
at java.security.Signature.verify(Unknown Source) ~[na:1.7.0_45]
at com.google.api.client.util.SecurityUtils.verify(SecurityUtils.java:164) ~[google-http-client-1.20.0.jar:1.20.0]
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建POST包含multipart-form-data该要求的请求NT Credentials.身份验证请求导致POST重新发送,我得到一个不可重复的实体异常.
我尝试包装生成的MultipartContent实体BufferedHttpEntity但是它会抛出NullPointerExceptions?
final GenericUrl sau = new GenericUrl(baseURI.resolve("Record"));
final MultipartContent c = new MultipartContent().setMediaType(MULTIPART_FORM_DATA).setBoundary("__END_OF_PART__");
final MultipartContent.Part p0 = new MultipartContent.Part(new HttpHeaders().set("Content-Disposition", format("form-data; name=\"%s\"", "RecordRecordType")), ByteArrayContent.fromString(null, "C_APP_BOX"));
final MultipartContent.Part p1 = new MultipartContent.Part(new HttpHeaders().set("Content-Disposition", format("form-data; name=\"%s\"", "RecordTitle")), ByteArrayContent.fromString(null, "JAVA_TEST"));
c.addPart(p0);
c.addPart(p1);
Run Code Online (Sandbox Code Playgroud)
文件ByteArrayContent说
AbstractInputStreamContent的具体实现,它根据字节数组的内容生成可重复的输入流.
使所有部件可重复并不能解决问题.因为这段代码
System.out.println("c.retrySupported() = " + c.retrySupported());输出c.retrySupported() = true.
我找到了以下文档:
1.1.4.1.可重复实体实体可以是可重复的,这意味着其内容可以被多次读取.这仅适用于自包含实体(如ByteArrayEntity或StringEntity)
我现在通过提取字符串内容将我转换MultipartContent为ByteArrayContent具有multi/part-form …
我的应用程序在GAE中运行。此应用程序对我的CloudML进行REST调用。
这是该代码
GoogleCredential credential = GoogleCredential.getApplicationDefault()
.createScoped(Collections.singleton(CLOUDML_SCOPE));
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
credential);
GenericUrl url = new GenericUrl(cloudMLRestUrl);
JacksonFactory jacksonFactory = new JacksonFactory();
JsonHttpContent jsonHttpContent = new JsonHttpContent(jacksonFactory, getPayLoad());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
jsonHttpContent.setWrapperKey("instances");
jsonHttpContent.writeTo(baos);
LOG.info("Executing request... " + baos.toString());
HttpRequest request = requestFactory.buildPostRequest(url, jsonHttpContent);
HttpResponse response = request.execute();
Run Code Online (Sandbox Code Playgroud)
上面的代码通常会导致ReadTimeout异常。
java.net.SocketTimeoutException: Read timed out at
java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_121] at
java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
~[na:1.8.0_121] at
java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_121]
at
Run Code Online (Sandbox Code Playgroud)
似乎我们可以添加具有自定义超时的HttpRequestInitializer,但是在创建HttpRequestFactory时我们需要传递GoogleCredential
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(GoogleCredential);
因此,我不能使用自定义HTTPRequestInitializer。如何增加使用GoogleCredential HTTPRequestInitializer创建的HttpRequestFactory的readTimeout?
java google-app-engine google-authentication google-http-client
从google-http-java-client*docs中不清楚如何发布一个包含文件字段的表单.
例如,我正在尝试使用Google Cloud Print API打印文档:
HttpRequestFactory httpRequestFactory = getHttpRequestFactory();
Map<String, Object> parameters = Maps.newHashMap();
parameters.put("printerId", printRequest.getPrinterId());
parameters.put("title", printRequest.getTitle());
parameters.put("contentType", printRequest.getContentType());
parameters.put("ticket", new Gson().toJson(printRequest.getOptions()));
MultipartContent content = new MultipartContent();
content.addPart(new MultipartContent.Part(new UrlEncodedContent(parameters)));
content.addPart(new MultipartContent.Part(
new FileContent(printRequest.getContentType(), printRequest.getFile())));
try {
HttpResponse response = httpRequestFactory.buildPostRequest(
SubmitUrl, content).execute();
System.out.println(IOUtils.toString(response.getContent()));
} catch (IOException e) {
String message = String.format();
System.out.println("Error submitting print job: " + e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用.API返回错误"此请求所需的打印机ID".在我看来,请求没有正确形成.
我究竟做错了什么?
*我专门使用google-http-java-client,因为它可以为我自动刷新OAuth令牌等.请不要回复涉及使用其他HTTP客户端的解决方案.
我创建了一个简单的 Jersey 客户端,它能够成功执行带有有效负载的 POST 请求。但现在它等待来自 http 端点的响应:
public void callEndpoint(String endpoint, String payload) {
try {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource webResource = client.resource(getBaseURI(endpoint));
log.debug("Sending payload [" + payload + "] to URL - [" + getBaseURI(endpoint) + "]");
// POST method - Is this blocking?
// Is it possible to not wait for response here
ClientResponse response = webResource.accept("application/json")
.type("application/json")
.post(ClientResponse.class, payload);
if (response.getStatus() != 200) {
log.error("The endpoint [" + getBaseURI(endpoint) + "] …Run Code Online (Sandbox Code Playgroud) HttpTransport我正在运行多个并发 API 调用,我想知道Google Api 客户端是否存在非阻塞实现。有人知道吗?
java nio nonblocking google-api-java-client google-http-client
需要帮助治疗间歇性头痛.代码调用com.google.api.client.http.HttpRequest#executeAsync()基本上具有以下逻辑,
@Beta
public Future<HttpResponse> executeAsync(Executor executor) {
FutureTask<HttpResponse> future = new FutureTask<HttpResponse>(new Callable<HttpResponse>() {
public HttpResponse call() throws Exception {
return execute();
}
});
executor.execute(future);
return future;
}
@Beta
public Future<HttpResponse> executeAsync() {
return executeAsync(Executors.newSingleThreadExecutor());
}
Run Code Online (Sandbox Code Playgroud)
该调用java.util.concurrent.RejectedExecutionException有时会进入,并且从日志看来,当这种情况发生时,它总是伴随着垃圾收集.下面是发生这种情况时的日志模式示例,
2017-09-26 11:04:56.039186 2017-09-26T11:04:56.012+0000: [GC pause (G1 Evacuation Pause) (young) 213M->50M(300M), 0.0262262 secs]
2017-09-26 11:04:56.048210 java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@71a0a39 rejected from java.util.concurrent.ThreadPoolExecutor@36c306aa[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
2017-09-26 11:04:56.048212 at …Run Code Online (Sandbox Code Playgroud) java multithreading garbage-collection jvm google-http-client
我使用Google HTTP Client Library for Java来制作简单的JSON请求并解析响应.当我不通过代理时,它运作良好.但现在我想允许我的用户在我的应用程序中使用代理(带身份验证)功能.我查看了HttpTransport,HttpRequestFactory和HttpRequestInitializer类没有任何成功.
到目前为止,我只略微修改了这些示例(主要是它删除了不必要的代码).那么我在代码中添加了代理设置?
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
static final JsonFactory JSON_FACTORY = new JacksonFactory();
<T> T get(String url, Class<T> type) throws IOException {
HttpRequestFactory requestFactory =
HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
request.setParser(new JsonObjectParser(JSON_FACTORY));
}
});
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(url));
return request.execute().parseAs(type);
}
Run Code Online (Sandbox Code Playgroud) 我想借助Google HTTP Client v1.14.1在Box API中调用这个特定的方法http://developers.box.com/docs/#files-upload-a-file.目前我认为没办法这样做.
如果我使用http://hc.apache.org/httpclient-3.x/methods/multipartpost.html,我会添加2项StringPart和1项FilePart.
在Google HTTP Client库中,我只看到MultipartContent和Part类似乎无法处理纯名称/值对,如上面引用的StringPart.
以下是Apache HTTP Client示例的摘录:
HttpPost httppost = new HttpPost("http://localhost:8080" +
"/servlets-examples/servlet/RequestInfoExample");
FileBody bin = new FileBody(new File(args[0]));
StringBody comment = new StringBody("A binary file of some kind");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);
Run Code Online (Sandbox Code Playgroud)
我想完成类似的事情,但使用谷歌HTTP客户端.欢迎大家提出意见!
java ×7
google-api ×2
box-api ×1
http-proxy ×1
jersey ×1
jvm ×1
jwt ×1
nio ×1
nonblocking ×1
post ×1