通过 Google Gmail API 创建草稿 [英] Creating draft via Google Gmail API

查看:28
本文介绍了通过 Google Gmail API 创建草稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为登录用户创建草稿消息,但在运行以下命令时不断收到错误Missing draft message

I am trying to create a draft message for a logged in user but keep getting the error Missing draft message when I run the below

require 'google/api_client'
client = Google::APIClient.new
client.authorization.client_id = ENV['GOOGLE_CLIENT_ID']
client.authorization.client_secret = ENV['GOOGLE_CLIENT_SECRET']
client.authorization.grant_type = 'refresh_token'
client.authorization.refresh_token = User.last.refresh_token
token = client.authorization.fetch_access_token!
gmail = client.discovered_api('gmail', 'v1')
params = { 'userId' => 'me', 'draft' => { 'message' => {'raw' => 'test email' } } }
# { 'userId' => 'me', 'message' => {'raw' => 'test email' } }
result = client.execute(api_method: gmail.users.drafts.create, parameters: params)

此外,我尝试了 params 的注释组合,但仍然没有运气.有什么想法吗?

In addition I tried the commented out combination for params and still no luck. Any ideas?

推荐答案

我第一次尝试这样做时也遇到了同样的问题.我找到的解决方案是不将消息信息作为参数的一部分包含在内,而是将其传递到 :body_object 中,如下所示.

I had the same issue when I was trying to do this for the first time as well. The solution that I found was to not include the message information as part of the parameters, but rather pass that on in the :body_object as shown below.

@result = client.execute(
  :api_method => gmail.users.drafts.create,
  :parameters => {
    'userId' => "me"      
  },
  :body_object => {
    'message' => {
      'raw' =>  Base64.urlsafe_encode64('Test Email Message')
    }
  }
)

这篇关于通过 Google Gmail API 创建草稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆