Google Script Request to App Store Connect 错误

Google Script Request to App Store Connect Error

提问人:Stan 提问时间:11/8/2023 最后编辑:Stan 更新时间:11/10/2023 访问量:57

问:

我在 Google Script 中编写了代码,当我运行它时出现错误:

{
    "errors": [{
        "status": "401",
        "code": "NOT_AUTHORIZED",
        "title": "Authentication credentials are missing or invalid.",
        "detail": "Provide a properly configured and signed bearer token, and make sure that it has not expired. Learn more about Generating Tokens for API Requests https://developer.apple.com/go/?id=api-generating-tokens"
    }]
}

从脚本生成的令牌是用正确的标头形成的,但警告签名无效,我在这里检查了它 - https://jwt.io/ 签名编码时看起来像问题。

代码如下:

const createJwt = ({ privateKey, expiresInMins, data = {} }) => {
  // Sign token using HMAC with SHA-256 algorithm
  const header = {
    alg: 'ES256',
    kid: '**kid**',
    typ: 'JWT'
  };

  const now = Date.now();
  const expires = new Date(now);
  expires.setMinutes(expires.getMinutes() + expiresInMins);

  // iat = issued time, exp = expiration time
  const payload = {
    exp: Math.round(expires.getTime() / 1000),
    iat: Math.round(now / 1000),
  };

  // add user payload
  Object.keys(data).forEach(function (key) {
    payload[key] = data[key];
  });

  const base64Encode = (text, json = true) => {
    const data = json ? JSON.stringify(text) : text;
    return Utilities.base64EncodeWebSafe(data).replace(/=+$/, '');
  };

  const toSign = `${base64Encode(header)}.${base64Encode(payload)}`;
  const signatureBytes = Utilities.computeHmacSha256Signature(toSign, privateKey);
  const signature = base64Encode(signatureBytes, false);
  return `${toSign}.${signature}`;
};

  const generateAccessToken = () => {
  // Your super secret private key
  const privateKey = '**private key**';
  const accessToken = createJwt({
    privateKey,
    expiresInMins: 10,
    data: {
      iss: '**iss**',
      aud: 'appstoreconnect-v1'
    },
  });
  Logger.log(accessToken);
  
  getUsers(accessToken);

};

function getUsers(accessToken){
    url = 'https://api.appstoreconnect.apple.com/v1/users';

    const optionsUsrGet = {
      method: 'GET',
      muteHttpExceptions: true,
      contentType: "application/json",
      headers: {
        'Authorization': "Bearer " + accessToken}
    };

    const responseUsrGet = UrlFetchApp.fetch(url, optionsUsrGet);
    if (responseUsrGet.getResponseCode() !=200 ) {
     Logger.log(responseUsrGet);
     service.reset();
      fail;
    };

    const data = JSON.parse(responseUsrGet);
    Logger.log(data);
}

更新:

通常,.p8 格式的密钥是从 App Store Connect 下载的,如下所示:

-----开始私钥----- MIGTAgEAMBMDByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgnUaBH3umCqbyl6w4 NBE9YxD42J2AFBXjfd8+tvVOQrOgCgYIKoZIzj1OPQehASVSAAS4fh1BhWSzj0PV vgIIllp9BQZKTNopsesddJ5ofmKYO/m7al9Pk1KAvN0vQRJKyHJ8A0sJUc0bljDSTm GWB4LPo -----结束私钥-----

在描述中,密钥应使用 ES256 算法 (ECDSASha256) 进行加密,判断为 事实证明,整个 Google 脚本只能 RsaSha256 或 HmacSha256 该行:const signatureBytes = Utilities.computeRsaSha256Signature(toSign, privatekey) 错误地对数据进行签名,这就是我无法通过 Apple API 连接的原因。

我拿了 RSA PKCS#8 测试密钥:

-----开始私有化钥匙----- MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqPfgaTEWEP3S9w0t gsicURfo+nLW09/0KfOPinhYZ4ouzU+3xC4pSlEp8Ut9FgL0AgqNslNaK34Kq+NZ jO9DAQIDAQABAkAgkuLEHLaqkWhLgNKagSajeobLS3rPT0Agm0f7k55FXVt743hw Ngkp98bMNrzy9AQ1mJGbQZGrpr4c8ZAx3aRNAiEAoxK/MgGeeLui385KJ7ZOYktj hLBNAB69fKwTZFsUNh0CIQEJQRpFCcydunv2bENcN/oBTRw39E8GNv2pIcNxZkcb NQIgbYSzn3Py6AasNj6nEtCfB+i1p3F35TK/87DlPSrmAgkCIQDJLhFoj1gbwRbH /bDRPrtlRUDDx44wHoEhSDRdy77eiQIgE6z/k6I+ChN1LLttwX0galITxmAYrOBh BVl433tgTTQ= -----结束私钥-----

在这种情况下,所有内容都已签名,但密钥不是来自 App Store Connect。

JWT 与来自 AppStore Connect 的密钥: eyJhbGciOiJFUzI1NiIsImtpZCI6Ik8xREszMUIyUkQiLCJ0eXAiOiJKV1QifQ.eyJleHAiOjE2OTk2MTA2MzEsImlhdCI6MTY5OTYxMDAzMSwiaXNzIjoiM2NvcDMyMTYtZD kyMS00YTU0LWEyZTUtN2o5MTc4Yjg5YWMzIiwiYXVkIjoiYXBwc3RvcmVjb25uZWN0LXYxIn0.F1wiu601YWLL3ZXPTu8f_m-ggTqeVavihye_ToesaJs

需要通过 API 从 App Store Connect 获取用户列表

谷歌应用脚本 jwt 应用商店连接 http-status-code-401

评论

0赞 Tanaike 11/9/2023
关于,我们能认为 ur 是一个有效值吗?而且,您能否提供 API 的官方文档来了解 API 的规范?The generated token from the script is formed with the correct headersaccessToken
0赞 Stan 11/9/2023
你好!我正在使用本手册 - labnol.org/code/json-web-token-201128 我认为签名编码部分存在问题。我正在从 App Store Connect 以 .p8 格式下载密钥。密钥以“-----BEGIN PRIVATE KEY-----”开头。这是 App Store Connect JWT 代币说明 - developer.apple.com/documentation/appstoreconnectapi/...
0赞 Stan 11/9/2023
我也试过这个 - nima-poulad-40744.medium.com/......但是当我使用我的密钥时,收到错误 - “无效的参数:密钥”
0赞 Tanaike 11/9/2023
感谢您的回复。我必须为我糟糕的英语水平道歉。不幸的是,我无法理解你对我的问题的回答。我能问你吗?About The generated token from the script is formed with the correct headers, can we consider that your accessToken is a valid value?
0赞 Stan 11/9/2023
感谢您的帮助!AccesToken是什么意思?如果你是关于我从 App Store 获得的令牌,它是正确的,当我在 Postman 中使用它时,它的工作很完美。

答: 暂无答案