DocuSign / Ruby - 尝试获取令牌时 unsupported_grant_type

时间:2021-04-20 21:19:30

标签: ruby jwt docusignapi

我们不再使用 DocuSign::Esign gem,而是尝试按照 How to get an access token with JWT Grant authentication 说明进行 API 调用。当我们最初使用 DocuSign::Esign gem 设置此应用程序时,该应用程序已获得同意。

我收到以下错误:

<块引用>

{"error"=>"invalid_grant", "error_description"=>"unsupported_grant_type"}

我正在使用 Ruby 并在控制台中运行此代码

config = Padrino.config.docusign
current_time = Time.now.utc

header = { 
  typ: 'JWT', 
  alg: 'RS256'
}

body = {
  iss: config.integrator_key,
  sub: config.user_id,
  iat: current_time.to_i,
  exp: (current_time + 1.hour).to_i,
  aud: config.host,
  scope: 'signature impersonation'
}

key_file = "-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAiOMDM5jdGYTEOC/nFVUTQ3+5U2TCUpEKyUD+mByldDbgvT9q
. . .
jDjfX6L15x8JcY9eiXvCvZNF6Za2dg8cagK+ff5d6KLodmVFD5o=
-----END RSA PRIVATE KEY-----"
private_key = OpenSSL::PKey::RSA.new(key_file)

token = JWT.encode(body, private_key, 'RS256')

uri = 'https://account-d.docusign.com/oauth/token'
data = {
  grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
  assertion: token 
}
auth_headers = {content_type: 'application/x-www-form-urlencoded'}

但是,当我调用 api 时,我收到了 RestClient::Bad 响应错误

irb(main):352:0> begin
irb(main):353:1>   RestClient.post(uri, data.to_json, auth_headers)
irb(main):354:1> rescue RestClient::BadRequest => e
irb(main):355:1>   JSON.parse(e.http_body)
irb(main):356:1> end
=> {"error"=>"invalid_grant", "error_description"=>"unsupported_grant_type"}

我不确定我做错了什么。当我在 https://jwt.io/ 中检查 JWT 时,它会正确解码。我完全按照文档中提供的方式使用 grant_type。

1 个答案:

答案 0 :(得分:1)

嗯,

  1. scope 声明只需为 signatureimpersonation 是隐含的,因为您使用的是 JWT 授权流程。)
  2. 对于 aud 声明,config.host 是什么?对于开发者系统,它应该是 account-d.docusign.com(不要包括 https://
  3. 您的主要错误是您以 JSON 格式发送数据哈希。错了,必须以url形式发送。试试
RestClient.post(uri, data, auth_headers)

相反。 (不要将数据转换为json。)