I have the following endpoint that a client sent me:
https://{restapi_id}.execute-api.{region}.amazonaws.com/test/v1/items?type=ABC
Since this request needs to be called with the AWS Authorization process I wanted to use the API Gateway SDK, but I can't find a straightforward example on how to call it. I was able to successfully test the API call using Postman (as exaplained in the AWS docs), but I don't know how to specify it using the SDK. The ENDPOINT should be the full URL? How should I make a call beyond this code? I tried using the GetResourceRequest, but that doesn't seem right. If so, what part of this URL I have is the resource?
This is code where I'm stuck with:
BasicAWSCredentials awsCreds = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
AmazonApiGatewayClient api = new AmazonApiGatewayClient(awsCreds);
api.setRegion(Region.getRegion(Regions.US_EAST_1));
api.setServiceNameIntern("execute-api");
api.setEndpoint(ENDPOINT);
Thanks for the help.
Hi @sanafa, AmazonApiGatewayClient is meant for interacting with the API Gateway service itself (e.g. if you want to deploy an API), and so you will not be able to invoke your client's APIs using the API Gateway client; you will need to use some other mechanism for executing the HTTP request against the endpoint. When using IAM authorization, the Authorization header value required is calculated using the AWS Signature Version 4 process. The class that the Java SDK uses for this is AWS4Signer which you could probably adapt to your needs if needed.
Hi @dagnir, thanks a lof for the clarification. I wanted to used the SDK because of the Authorization header requirement, and to avoid to manually code the signing process.
I'll take a look at the AWS4Signer you mentioned.
Thanks!
A follow up question:
I've been reading this post:
http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-call-api.html
Here the example is for just use it with the browser (or Postman if it's signed),
Isn't a way to call it within the AWS SDK?
Thanks.
There's no way to call an API deployed to API Gateway directly using the Java SDK. However, it's now possible to generate SDKs for API fronted by API Gateway. If your client can generate a Java SDK for his API, then you should be able to make calls to the API from Java in a similar way to the normal AWS service clients.
Please see this blog post for more information: https://aws.amazon.com/blogs/developer/api-gateway-java-sdk/
Closing this issue. Please feel free to re-open if you have further issues.
You will need to use the official generated SDK or a generic client
Hello Team,
I want to call a service hosted on AWS API Gateway using Java. For that want to do some different kind of authentication using inputs AccessKeyId, SecretAccessKey, SessionToken,Expiration and region. I have these inputs. Could you please guide me how can i authenticate service using above inputs using java/AWS SDK? I am using java 1.6 version.
I have tried to implement one way but getting Error 500/ connection timeout. Below is my code snippet and i am not using session token in my code. Your help be appreciated.
String newurl = "https://XXXXX.aws.XXX.com/v0/authentication/css/apps/rest/apps/presales-portal/searchers/presales-portal?q=truck&type=all-documents&search=fulltext&facet=true";
String key = "XrRgU5DYRKHQd0+AcUKeS+LqsAlGaC374LLuDiaJ"; String dateStamp = "2019-03-22 12:28:12+00:00"; String regionName = "eu-west-1"; String serviceName = "execute-api"; String awsAccessKeyID = "ASIAZAC6LSMZL566LQU7"; URL url; try { url = new URL(newurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
byte getSignatureKey[] = CommonMethod.getSignatureKey(key, dateStamp, regionName, serviceName);
conn.setRequestProperty ("Authorization", "AWS "+awsAccessKeyID+":"+getSignatureKey);
if (conn.getResponseCode() == 200) {
InputStreamReader inputStream = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(inputStream);
String output;
while ((output = br.readLine()) != null) {
completeResponse += output;
}
System.out.println("completeResponse :"+completeResponse);
br.close();
inputStream.close();
if (conn != null)
{
conn.disconnect();
}
}else{
System.out.println("Eror code 500");
}
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("getFullTextSearchResponse Exception 1 : "+e);
} catch (IOException e) {
e.printStackTrace();
System.out.println("getFullTextSearchResponse Exception 2 : "+e);
}
Most helpful comment
Hi @sanafa,
AmazonApiGatewayClientis meant for interacting with the API Gateway service itself (e.g. if you want to deploy an API), and so you will not be able to invoke your client's APIs using the API Gateway client; you will need to use some other mechanism for executing the HTTP request against the endpoint. When using IAM authorization, theAuthorizationheader value required is calculated using the AWS Signature Version 4 process. The class that the Java SDK uses for this isAWS4Signerwhich you could probably adapt to your needs if needed.