DocuSign / Ruby - unsupported_grant_type 尝试获取令牌时
DocuSign / Ruby - unsupported_grant_type when trying to obtain token
我们将不再使用 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。
嗯,
scope
声明只需 signature
(隐含 impersonation
,因为您使用的是 JWT 授权流程。)
- 对于
aud
声明,config.host
是什么?对于开发者系统应该是 account-d.docusign.com
(Do not include https://
)
- 您的主要错误是您以 JSON 格式发送数据哈希。错了,必须以url形式发送。尝试
RestClient.post(uri, data, auth_headers)
相反。 (不要将数据转换为 json。)
我们将不再使用 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。
嗯,
scope
声明只需signature
(隐含impersonation
,因为您使用的是 JWT 授权流程。)- 对于
aud
声明,config.host
是什么?对于开发者系统应该是account-d.docusign.com
(Do not includehttps://
) - 您的主要错误是您以 JSON 格式发送数据哈希。错了,必须以url形式发送。尝试
RestClient.post(uri, data, auth_headers)
相反。 (不要将数据转换为 json。)