sendgrid / sendgrid-ruby

The Official Twilio SendGrid Led, Community Driven Ruby API Library
https://sendgrid.com
MIT License
620 stars 324 forks source link

Bad username / password #464

Closed rohandey closed 3 years ago

rohandey commented 3 years ago

Issue Summary

Using this old sendgrid-ruby version for a legacy app, suddenly it started to throw error SendGrid::Exception: {"errors":["Bad username / password"],"message":"error"}

Is there any change to how api calls are made or we need to make some changes for this work again.

Code Snippet

sendgrid = SendGrid::Client.new do |c|
  c.api_user = 'apikey'
  c.api_key = 'SG.****************migc'
end

email = SendGrid::Mail.new do |m|
  m.to      = "to@example.com"
  m.from    = "you@youremail.com"
  m.subject = "Test Send"
  m.html    = "Go"
end

sendgrid.send(email)

Exception/Log

SendGrid::Exception: {"errors":["Bad username / password"],"message":"error"}

Technical details:

Urgent help required.

Thank You

thinkingserious commented 3 years ago

Hello @rohandey,

Yes, the method of authentication has changed.

My recommendation is to update to the latest version of this library, then your code would look something like this:

require 'sendgrid-ruby'
include SendGrid

from = SendGrid::Email.new(email: 'test@example.com')
to = SendGrid::Email.new(email: 'test@example.com')
subject = 'Sending with Twilio SendGrid is Fun'
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
mail = SendGrid::Mail.new(from, subject, to, content)

sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
response = sg.client.mail._('send').post(request_body: mail.to_json)

With best regards,

Elmer

rohandey commented 3 years ago

Hi @thinkingserious Thank you, this worked. What is the best way to sets headers under this SendGrid::Mail.new(from, subject, to, content)

Thanks Rohan

rohandey commented 3 years ago

Hi @thinkingserious

To be more clear I am not able to do add_tos to message. Previously we were sending news letters by appending array of emails like this

header = Smtpapi::Header.new header.set_tos(email_arr) mail = SendGrid::Mail.new( smtpapi: header, to: 'to@example.com', from: 'from@example.com', subject: 'Daily Mail', html: @template )

sending = @client.send(mail)

What would be the new way with the current api?

Thanks Rohan

thinkingserious commented 3 years ago

Ah, you may need a more flexible approach. Please see this full example. Or, this example, if you would like to use dynamic templates.

rohandey commented 3 years ago

Thank You @thinkingserious ... It worked :)