开发者社区> 问答> 正文

函数计算 Authorization 无法通过验证

函数页面下,点击执行能正常返回

但根据文档或使用 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 演示请求函数计算

展开
收起
1370308484343651 2020-05-20 16:57:08 1116 0
1 条回答
写回答
取消 提交回答
  • queries 不能为 null,至少应该为空 Map

    2020-05-20 19:05:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Hologres Serverless之路:揭秘弹性计算组 立即下载
Serverless开发平台: 让研发效能再提升 立即下载
Serverless 引领云上研发新范式 立即下载