如何在React JS中访问证书文件?

问题描述 投票:0回答:1

我有一个 React 本机应用程序,它利用 Apple Music API 来显示用户播放列表。

我有一个密钥文件,我需要用它来获取我的开发者令牌,我正在尝试找到一种读取该文件的方法。由于它位于我的本地,我无法使用 fetch 来获取密钥文件,因为它会从我的 Web 应用程序所在的本地主机调用它。 T

fs/promises readFile 方法不能在 React 中使用,因为它与当前版本的 React 不兼容。

这是我当前的代码:

` 私有异步 readPrivateKey(): Promise { 尝试 { const 响应 = 等待 fetch(private_key_path);

  if (response.ok) {
    const privateKeyContent = await response.text();
    return privateKeyContent;
  } else {
    console.error(`Failed to read private key: ${response.statusText}`);
    throw new Error('Failed to read private key');
  }
} catch (error) {
  console.error(`Failed to read private key: ${error}`);
  throw error;
}

}`

它使用以下路径获取密钥:“http://localhost:5173/key/key.p8”,这导致了我的问题。

任何想法/想法将不胜感激。

reactjs typescript react-native certificate
1个回答
0
投票

fetch
方法无法读取本地文件,这种情况下你需要使用RNFS模块。 链接:https://www.npmjs.com/package/react-native-fs

伪代码示例

import RNFS from 'react-native-fs';

const readPrivateKey = async () => {
  const filePath = '/key/key.p8';
  const privateKeyContent = await RNFS.readFile(filePath);

  return privateKeyContent;
};
© www.soinside.com 2019 - 2024. All rights reserved.