本文介绍了为 App Store Connect API 生成令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 Store Connect API 生成 JWT 令牌.我正在尝试使用 jwt ruby​​ gem ruby-jwt.这是我的令牌生成代码,

I need to generate JWT token for Store Connect API. I'm trying use jwt ruby gem ruby-jwt. This is my token generating code,

payload = {
      'iss': my_issuer_id_from_db,
      'exp': generated_unix_timestamp, #Time.now + 20min
      'aud': 'hard_coded_string_from_doc'
  }
  header = {
      'alg': 'ES256',
      'kid': my_key_id_from_db,
      'typ': 'JWT'
  }

private_key = OpenSSL::PKey.read(File.read('/tmp/private_key.pem'))
# private_key - <OpenSSL::PKey::EC:0x000000000XXXXXXX>

@token = JWT.encode(payload, private_key, 'ES256', header)
# encoded_header.encoded_payload.emcoded_signature

我放入请求标头的令牌:

This token I put in to header of my request:

headers = { Authorization: 'Bearer' + @token }

我收到的回复:

        "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"
        }]
}

我认为问题在于令牌(直接带有签名).当我尝试使用 在线工具 解码令牌时,我的有效负载和标头被正确解码.状态:无效签名

I thing the problem is with token(directly with signature). When I try decode token with online tool, my payload and header is decoded properly. Status: Invalid Signature

我做错了什么?任何想法如何正确?

What I do wrong? Any ideas how do it properly?

推荐答案

您传入的授权字符串中缺少空格.您的代码在修改为

You had a missing space in your authorization string passed in. You code worked fine when modified to

headers = { Authorization: 'Bearer' + @token }

这篇关于为 App Store Connect API 生成令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:07