OAuth 1.0 HMAC256 签名不匹配

问题描述

我目前正在尝试授权使用 OAuth1 到 Here 位置服务。这是我遵循的提供的文档以达到这一点: https://developer.here.com/tutorials/how-to-authenticate-with-here-oauth/

似乎一切都符合规范,但我收到此错误

date: Mon,18 Jan 2021 19:36:10 GMT
content-type: application/json; charset=utf-8
content-length: 322
pragma: no-cache
x-error-id: ERROR-8560ecd6-9078-4e0d-85d5-65eb058c2012
x-request-id: REQ-7e15616e-7b1e-4556-9aef-8deeaa97b8a9
cache-control: no-store
x-frame-options: DENY
x-response-time: 3
www-authenticate: Bearer realm="https://account.api.here.com",error="invalid_request",error_description="Signature mismatch. Authorization signature or client credential is wrong."
x-correlation-id: c5fec749-81cf-45d1-8fcd-97cbe3790a0c
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
content-security-policy: default-src 'self'
x-permitted-cross-domain-policies: master-only

Response{protocol=h2,code=401,message=,url=https://account.api.here.com/oauth2/token}

这是我的代码

@RestController
public class HereOAuth {

    @GetMapping("/hereOAuth")
    @ResponseStatus(HttpStatus.CREATED)
    public String hereOAuth() throws Exception {

        String accessKey = "xxxx";
        String psig = encode("xxxxxxxx",accessKey);
        String sig = URLEncoder.encode(psig,StandardCharsets.UTF_8.toString());
        long Now = Instant.Now().getEpochSecond();
        

        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType,"grant_type=client_credentials");
        String authHeader = "OAuth oauth_consumer_key=\"" + accessKey + "\",oauth_nonce=\"" + Now + "\",oauth_signature=\"" + sig + "\",oauth_signature_method=\"HMAC-SHA256\",oauth_timestamp=\"" + Now + "\",oauth_version=\"1.0\"";
        Request request = new Request.Builder()
                .url("https://account.api.here.com/oauth2/token")
                .addHeader("Content-Type","application/x-www-form-urlencoded")
                .addHeader("Authorization",authHeader)
                .method("POST",body)
                .build();
        Response response = client.newCall(request).execute();
        System.out.println(response.headers());
        System.out.println(response.toString());

        return "NA";
    }

    public String encode(String key,String data) {
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"),"HmacSHA256");
            sha256_HMAC.init(secret_key);
            return new String(Hex.encodeHex(sha256_HMAC.doFinal(data.getBytes("UTF-8"))));
        } catch (NoSuchAlgorithmException e) {
            e.printstacktrace();
        } catch (InvalidKeyException e) {
            e.printstacktrace();
        } catch (UnsupportedEncodingException e) {
            e.printstacktrace();
        }
        return null;
    }
}

我尝试使用在线 HMAC 生成生成签名,但没有成功。我在这里错过了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)