【发布时间】:2021-03-15 11:04:28
【问题描述】:
我在我的 Rails 应用程序中使用 gmail api 发送电子邮件。用户可以向 google 进行身份验证并发送电子邮件(它会征求用户同意。在用户批准后,他可以从他的帐户发送电子邮件)
我的要求是我想显示登录用户,在我的 rails 应用程序中从他的电子邮件 ID 发送了多少电子邮件。对于那个我使用下面的端点。但我得到一个错误
require 'net/http'
require 'uri'
in controler
def sent_email_count
_
api_key = "api_key_contains_smal_case_capital_case_letters_and_special_symbols"
uri = URI.parse("https://www.googleapis.com/gmail/v1/users/#{current_user.email}/messages?labelIds=SENT&q=newer_than%3A1d&key={api_key}")
@gmail_response = Net::HTTP.get_response(uri)
end
in views :-
response <%= @gmail_response >
但出现未经授权的错误。
发送电子邮件计数:- #Net::HTTPUnauthorized:0x00007f6f5e3e2158
我也试过如下。但它不起作用。(api键的字符串插值更改)
uri = URI.parse("https://www.googleapis.com/gmail/v1/users/#{current_user.email}/messages?labelIds=SENT&q=newer_than%3A1d&key=#{api_key}")
@gmail_response = Net::HTTP.get_response(uri)
uri = URI.parse("https://www.googleapis.com/gmail/v1/users/#{current_user.email}/messages?labelIds=SENT&q=newer_than%3A1d&key=api_key")
@gmail_response = Net::HTTP.get_response(uri)
有人可以帮我解决这个问题
【问题讨论】:
-
同意发送电子邮件并不一定授予查询用户帐户数据的权限。我会检查 gmail api 并查看需要哪些权限。或者,您可以将通过您的应用程序发送的电子邮件记录在数据库表中。
标签: ruby-on-rails gmail-api endpoint rest