函数页面下,点击执行能正常返回
但根据文档或使用 SDK 构造的 Authorization 均无法通过验证 (403)
使用 SDK 构造方法如下:
private void getAuthorization(HttpHeaders headers) throws InvalidKeyException {
Map<String, String> headersMap = getHeaders();
String sign = composeStringToSign(HttpMethod.GET, CanonicalizedResource, headersMap, null);
String signature = signString(sign, accessKeySecret);
// Authorization requisites
String prefix = "FC";
String authorization = prefix + " " + accessKeyID + ":" + signature;
headersMap.forEach(headers::set);
headers.set("Authorization", authorization);
}
获取 headers
private Map<String, String> getHeaders() {
Map<String, String> headersMap = new HashMap<>();
headersMap.put("Accept", "*/*");
headersMap.put("Accept-Encoding", "gzip, deflate, br");
headersMap.put("Accept-Language", "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7");
headersMap.put("Cache-Control", "no-cache");
headersMap.put("Connection", "keep-alive");
headersMap.put("Host", "1370308484343651.cn-beijing.fc.aliyuncs.com");
headersMap.put("Origin", "https://fc.console.aliyun.com");
headersMap.put("Referer", "https://fc.console.aliyun.com/fc/service/cn-beijing/java-FC-demo/function/helloFC/code");
headersMap.put("Sec-Fetch-Dest", "empty");
headersMap.put("Sec-Fetch-Mode", "cors");
headersMap.put("Sec-Fetch-Site", "cross-site");
headersMap.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
headersMap.put("Content-Type", "text/plain");
headersMap.put("x-fc-account-id", userId);
headersMap.put("x-fc-account-date", gmtDate);
headersMap.put("X-Fc-Invocation-Code-Version", "Latest");
headersMap.put("X-Fc-Log-Type", "Tail");
headersMap.put("Date", gmtDate);
return headersMap;
}
发送请求:
@Test
public void should_get_hello_from_aliyun_by_sdk_method() throws InvalidKeyException {
String helloFCUrl = host + CanonicalizedResource;
HttpHeaders headers = new HttpHeaders();
getAuthorization(headers);
HttpEntity<String> headerEntity = new HttpEntity<>(headers);
ResponseEntity<String> response
= restTemplate.exchange(helloFCUrl, HttpMethod.GET, headerEntity, String.class);
String body = response.getBody();
System.out.println(body);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
}
使用的一些字段:
private static final String HMAC_SHA256 = "HmacSHA256";
private static final RestTemplate restTemplate = new RestTemplate();
private static final String userId = "1370308484343651";
private static final String host = "https://" + userId + ".cn-beijing.fc.aliyuncs.com";
private static final String CanonicalizedResource = "/2016-08-15/proxy/java-FC-demo/helloFC/";
// accessKey ID 和 Secret 检查过数次,没有错误
private static final String accessKeyID = "***";
private static final String accessKeySecret = "***";
protected final static String HEADER_SEPARATOR = "\n";
public final static String authQueryKeyExpires = "x-fc-expires";
希望有简单的 Demo 演示请求函数计算
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。