Sendgrid Ruby API,正在尝试发送 post 请求中的 Header 内容

Sendgrid Ruby API, Trying to send over Header content in post request

我正在尝试向 sendgrid 发送 Post 请求,以便为子用户生成 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 的正确语法。

如果你能帮忙,我将不胜感激。谢谢!

如果我理解正确,你想给 "From" 另一个用户发送电子邮件。代表是非标准的做事方式。 例如。 https://sendgrid.com/docs/Classroom/Troubleshooting/Authentication/my_emails_are_displaying_as_on_behalf_of_or_via_in_some_mail_clients.html

您可能想尝试设置 from 而不是 on-behalf-of

"from": { "email": "from_address@example.com" },

参考:https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html

我最近不得不这样做。您需要在实例化客户端时而不是在发出请求时设置 On-Behalf-Of headers:

```

@send_grid = API.new(api_key: @api_key, request_headers: {
               'On-Behalf-Of' => @username
             })

```

然后当您使用 @send_grid 发出请求时,它将代表子用户发送——并且 API 密钥将不会出现在 api 密钥列表中parent 帐户