Angular App 中的 Maps API OAuth“签名不匹配”

问题描述

我想在我的 Angular 应用程序中使用 Here Maps API,但我在获取访问令牌时遇到问题。我正在尝试通过 OAuth 1.0a 进行身份验证,如 documentation

中所示
  @Injectable()
  export class HereAuthService {
    private readonly TOKEN_ENDPOINT_URL: string;
    private readonly oauth: OAuth;

    constructor(private readonly http: HttpClient) {
      const { accessKeyId,accessKeySecret,tokenEndpointUrl } = environment.mapAPI.auth;

      this.TOKEN_ENDPOINT_URL = tokenEndpointUrl;

      this.oauth = new OAuth({
        consumer: {
          key: accessKeyId,secret: accessKeySecret,},signature_method: 'HMAC-SHA256',hash_function: (base_string: string,key: string) =>
          CryptoJS.HmacSHA256(base_string,key).toString(CryptoJS.enc.Base64),});
    }

    async getToken(): Promise<string> {
      const requestData = {
        url: this.TOKEN_ENDPOINT_URL,method: 'POST',data: { grant_type: 'client_credentials' },};
      const auth: OAuth.Authorization = this.oauth.authorize(requestData);
      const authHeader: OAuth.Header = this.oauth.toHeader(auth);

      let headers = new HttpHeaders();
      headers = headers
        .append('Authorization',authHeader.Authorization)
        .append('Content-Type','application/x-www-form-urlencoded');

      const options = { headers };
      const { access_token } = await this.http.post<OAuthToken>(requestData.url,requestData.data,options).toPromise();

      return access_token;
    }
  }

当我调用 getToken() 时,我收到错误 Signature mismatch. Authorization signature or client credential is wrong 但如果我复制 authHeader.Authorization 的结果并在邮递员中使用它,它会起作用。

我在 Angular 中做错了什么?

解决方法

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

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

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