本文介绍了为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