dchun / QuoteManager

Saas App for Managing Quotes and getting paid
https://quotemanager.boopis.com
0 stars 0 forks source link

Refresh token must be present #209

Closed dchun closed 9 years ago

dchun commented 9 years ago

I ran into the following error when trying to send an email:

ArgumentError (Missing access token.):
  app/services/gmail_api.rb:11:in `send_message'
  app/controllers/contacts_controller.rb:78:in `block in send_email_to_contact'
  app/controllers/contacts_controller.rb:67:in `send_email_to_contact'

I ran into a similar issue when I was building the appointment application where the user was being created without the refresh token, as a consequence, google does not send the refresh token on an already approved account but only on the first callback of the approval. So it is necessary to make sure that the refresh token exist when an identity association is created for the user or the fresh_token method will not work since it relies on the refresh_token

phuong3030 commented 9 years ago

I checked it on production server with account: p@email.com password: 123123123. Google always send the refresh token at the first time we connect to application. And in the next time, we use the API, Google only send the access_token.

send-email send-email1

I think if you tested with local server and had already confirmed the application and reset database, you would deactive this app on security.google.com and test again.

This code below contain a function to check expired token.

# Identity.rb
def refresh!
    response = request_token_from_google
    data = JSON.parse(response.body)
    update_attributes(
      access_token: data['access_token'],
      expires_at: Time.now + (data['expires_in'].to_i).seconds
    )
  end

  def expired?
    expires_at < Time.now
  end

  def fresh_token
    refresh! if expired?
    access_token
  end
dchun commented 9 years ago

It was in production. I'll reset my security settings. Perhaps we can add a validation to identities requiring the refresh token to be present or not allow the connection to google.

phuong3030 commented 9 years ago

It will be done after I finish currency for quote issude

phuong3030 commented 9 years ago

Fixed missing Google refresh token when sending email in:

send email contact

https://github.com/boopis/QuoteManager/pull/212