我正在尝试将post请求发送到sendGrid,以便为子用户生成API密钥。
这就是我的代码现在的样子
body = JSON.parse('{
"name":"My API Key",
"scopes": [
"mail.send",
"alerts.create",
"alerts.read"
]
}')
header = {'On-Behalf-Of' => 'my@email.com'}
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
response = sg.client.api_keys.post(request_body: body, request_header: header)
此代码生成API,但在主帐户而不是子用户帐户上。header是生成api密钥的驱动程序,我似乎可以在网上找到任何源代码,知道如何通过header向sendgrid发送正确的语法。
如果你能帮忙,我将非常感激。谢谢!
最佳答案
我最近不得不这么做。在实例化客户机时,而不是在发出请求时,需要设置On-Behalf-Of
头:
` `
@send_grid = API.new(api_key: @api_key, request_headers: {
'On-Behalf-Of' => @username
})
` `
然后,当您使用
@send_grid
发出请求时,它将代表子用户发送——并且api密钥不会显示在父帐户上的api密钥列表中关于ruby-on-rails - Sendgrid Ruby API,尝试在发布请求中发送 header 内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48468126/