【问题标题】:DocuSign / Ruby - unsupported_grant_type when trying to obtain tokenDocuSign / Ruby - 尝试获取令牌时不支持_grant_type
【发布时间】:2021-07-15 02:16:50
【问题描述】:

我们将不再使用 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。

【问题讨论】:

  • 欢迎来到 StackOverflow!请检查(接受)您每个问题的最佳答案。谢谢。

标签: ruby jwt docusignapi


【解决方案1】:

嗯,

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

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

【讨论】:

  • 现在它工作得很好。谢谢你的提示,删除.to_json 是票。
  • 太棒了!很高兴为您提供帮助并感谢您的检查!
猜你喜欢
  • 2021-01-29
  • 2017-07-29
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 2021-01-07
  • 2019-10-19
  • 2014-09-07
相关资源
最近更新 更多