AWS API网关调用不适用于Android

AWS API网关调用不适用于Android,android,amazon-web-services,retrofit2,http-status-code-403,aws-api-gateway,Android,Amazon Web Services,Retrofit2,Http Status Code 403,Aws Api Gateway,我在AWS上设置了一个自定义API,用于向服务器传递消息。然后,服务器以简单的方式响应该消息。API调用可以通过浏览器和邮递员工作,但不能通过移动客户端工作。我已经删除了API上的授权,部署了它,并对API调用进行了三次检查,但我仍然收到403“禁止”错误。我正在使用改型调用API。这是我的客户代码: API调用: import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.Headers; import re

我在AWS上设置了一个自定义API,用于向服务器传递消息。然后,服务器以简单的方式响应该消息。API调用可以通过浏览器和邮递员工作,但不能通过移动客户端工作。我已经删除了API上的授权,部署了它,并对API调用进行了三次检查,但我仍然收到403“禁止”错误。我正在使用改型调用API。这是我的客户代码:

API调用:

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public interface MyAPI {
    @Headers({
            "content-type: application/json",
            "Accept: application/json"
    })
    @POST("/chat/new")
    Call<ChatMessageBody> postMessage(@Body ChatMessageBody message);
}
理论上,由于设置,我呼叫的请求主体应该是这样的:

{
     "messageBody" : "This is my message"
}
我是否错过了从移动设置调用API的一个重要方面?我无法通过浏览器和Postman调用API端点并让它工作,这对我来说毫无意义,但我似乎无法通过Android这样做

编辑: 成功调用的响应正文(通过邮递员或API网关测试)

来自403错误的响应正文

{"message":"Forbidden"}
邮递员根据您想要的客户机和您的呼叫生成代码。以下是Postman为此调用生成的代码(关于OkHTTP。我不使用此代码,但它可以深入了解Postman工作而我的客户不工作的原因):

以下是Android环境中我的请求/响应的堆栈跟踪:

03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp: --> POST [MY_API_ENDPOINT] http/1.1
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp: Content-Type: application/json
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp: Content-Length: 36
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp: Accept: application/json
03-27 12:32:27.836 29985-31080/com.dev.example.debug D/OkHttp: {"messageBody":"This is my message"}
03-27 12:32:27.836 29985-31080/com.dev.example.debug D/OkHttp: --> END POST (36-byte body)
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: <-- 403  [MY_API_ENDPOINT] (239ms)
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: content-type: application/json
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: content-length: 24
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: date: Mon, 27 Mar 2017 16:32:28 GMT
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: x-amzn-requestid: [REQUEST_ID]
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: x-amzn-errortype: ForbiddenException
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: x-cache: Error from cloudfront
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: via: 1.1 [CLOUDFRONT_URL] (CloudFront)
03-27 12:32:28.077 29985-31080/com.dev.example.debug D/OkHttp: x-amz-cf-id: [CF_ID]
03-27 12:32:28.077 29985-31080/com.dev.example.debug D/OkHttp: {"message":"Forbidden"}
03-27 12:32:28.077 29985-31080/com.dev.example.debug D/OkHttp: <-- END HTTP (24-byte body)
03-27 12:32:28.079 29985-29985/com.dev.example.debug E/MyAPI: Your request did not receive a successful response.
03-27 12:32:28.079 29985-29985/com.dev.example.debug E/MyAPI: Response Error Code: 403
03-27 12:32:28.079 29985-29985/com.dev.example.debug E/MyAPI: Response Body: {"message":"Forbidden"}
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp:-->发布[MY_API_ENDPOINT]http/1.1
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp:Content-Type:application/json
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp:Content-Length:36
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp:Accept:application/json
03-27 12:32:27.836 29985-31080/com.dev.example.debug D/OkHttp:{“messageBody”:“这是我的消息”}
03-27 12:32:27.836 29985-31080/com.dev.example.debug D/OkHttp:-->END POST(36字节正文)

03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp:该请求中没有授权,因此返回的响应被禁止访问该路由

此外,内容类型也会影响到,如果您将请求发送到错误的路径,postman可以方便地在幕后处理此问题,
应用程序/x-www-form-urlencoded

403(权限被拒绝)也会发生

我不熟悉改装,但您的注释中只列出了
/chat/new
。如何在改造中配置根路径


你确定要包括完整的路径包括阶段吗?

我最终解决了这个问题,放弃了改造,设计了一个与AWS接口的OKHttp客户端。我的请求现在已被正确地签署为AWS,并且我不再收到403个错误。

为那些更喜欢改型而不是AWS sdk的人创建了一个。它仍然使用
aws android sdk apigateway core
tho中的
AWS4Signer
,因此您必须在项目中保留此依赖关系。

我关闭了所讨论方法的授权,并且我的api不需要密钥,那么我需要采取哪些步骤来配置移动客户端的授权?我检查过的所有资源都表明,除非您设置了授权或API密钥,否则您应该能够自由调用它。发布API调用的完整响应正文?我刚刚编辑了我的原始帖子,以包含成功和失败API调用的响应正文。根路径在运行时使用生成器类添加。我已经检查过了,我使用的是正确的终点。我帖子中的堆栈跟踪显示了[my_API_ENDPOINT],其中url是API端点,包括阶段和资源。Hy John,我遇到了您在问题中提到的相同问题。我无法解决这个问题。如果您能在这个问题上帮助我,我将不胜感激。您能描述一下您的API网关的结构吗?例如,您发送的是什么类型的请求,是否使用CORS等。Hy@John Riley,您可以参考我的同事发布的链接。发布的问题之一是我们在应用程序中的实现方式。@vishalsharma我没有使用改型来获得我的解决方案。我建议你学习OkHttp并用它来实现你的客户端。如果你能分享一些关于如何实现的链接或代码,那就太好了,因为我原以为用改型作为普通api来实现aws api会很容易,但事实并非如此,我必须在本周末之前发布版本,对此毫无头绪。对不起,要求太高了。
{"message":"Forbidden"}
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\t\"messageBody\" : \"test\"\n}");
Request request = new Request.Builder()
  .url("https://qj2wkyi91c.execute-api.us-east-1.amazonaws.com/prototype_mvp/chat/new")
  .post(body)
  .addHeader("content-type", "application/json")
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "dccf1f20-f20e-fd53-dd24-a9f4ae7b5c69")
  .build();

Response response = client.newCall(request).execute();
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp: --> POST [MY_API_ENDPOINT] http/1.1
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp: Content-Type: application/json
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp: Content-Length: 36
03-27 12:32:27.835 29985-31080/com.dev.example.debug D/OkHttp: Accept: application/json
03-27 12:32:27.836 29985-31080/com.dev.example.debug D/OkHttp: {"messageBody":"This is my message"}
03-27 12:32:27.836 29985-31080/com.dev.example.debug D/OkHttp: --> END POST (36-byte body)
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: <-- 403  [MY_API_ENDPOINT] (239ms)
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: content-type: application/json
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: content-length: 24
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: date: Mon, 27 Mar 2017 16:32:28 GMT
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: x-amzn-requestid: [REQUEST_ID]
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: x-amzn-errortype: ForbiddenException
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: x-cache: Error from cloudfront
03-27 12:32:28.076 29985-31080/com.dev.example.debug D/OkHttp: via: 1.1 [CLOUDFRONT_URL] (CloudFront)
03-27 12:32:28.077 29985-31080/com.dev.example.debug D/OkHttp: x-amz-cf-id: [CF_ID]
03-27 12:32:28.077 29985-31080/com.dev.example.debug D/OkHttp: {"message":"Forbidden"}
03-27 12:32:28.077 29985-31080/com.dev.example.debug D/OkHttp: <-- END HTTP (24-byte body)
03-27 12:32:28.079 29985-29985/com.dev.example.debug E/MyAPI: Your request did not receive a successful response.
03-27 12:32:28.079 29985-29985/com.dev.example.debug E/MyAPI: Response Error Code: 403
03-27 12:32:28.079 29985-29985/com.dev.example.debug E/MyAPI: Response Body: {"message":"Forbidden"}