Bad Authentication Error Rails connecting to google drive - ruby-on-rails

I have a contacts controller which has a method to connect and save the data submitted from the form to a spreadsheet in my account on google drive (app/models/contact.rb):
def update_spreadsheet
connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])
ss = connection.spreadsheet_by_title('Learn-Rails02-Example')
if ss.nil?
ss = connection.create_spreadsheet('Learn-Rails02-Example')
end
The method calls on environment variables I set up with figaro which are saved config/environments/development.rb as follows:
GMAIL_USERNAME: tgolsby#gmail.com
GMAIL_PASSWORD: Paxxword (obviously this is a place holder for my actual password in the .yml file)
When I submit my form I get the following error:
*GoogleDrive::AuthenticationError at /contacts
Authentication failed for tgolsby: Response code 403 for post https://www.google.com/accounts/ClientLogin: Error=BadAuthentication*
It points to the following line in the contacts model:
*def update_spreadsheet*
**connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])**
*ss = connection.spreadsheet_by_title('Learn-Rails02-Example')
if ss.nil?
ss = connection.create_spreadsheet('Learn-Rails02-Example')
end*
I have checked and rechecked my username and password and am 100% sure they are correct in the application.yml file. I have gone through the tutorial and repeated all the steps but I just cant' figure out why the app is having trouble connecting to google. I have also checked that I don't have 2 step verification set up on my google account.

You might have the same issue I had. I was accessing my google doc using the google_drive gem on a personal project I had not touched in over a year. I used to be able to access my google doc by enabling a session using session = GoogleDrive.login('rosalyntest1#gmail.com', 'password') but was now getting the same error as you.
I logged into that account using my password and saw an email from google with a subject of "sign in blocked." I followed the link https://www.google.com/settings/security/lesssecureapps to enable less secure app access for that account only and was able to enable a session locally using the same code. I would check your google account and see if you received an email or enable less secure app access and try again.

The code is good (you can check it against the learn-rails example application). It looks exactly like the code from the Learn Ruby on Rails book.
That means the problem is a result of an environment or configuration issue.
First, are you running locally, or do you have the problems after deploying to Heroku?
If running locally, here are things you can try to troubleshoot:
1) Try restarting the web server. When you change configuration files, the server has to be restarted.
2) Replace ENV["..."] in the code and use the credentials directly ("hardcode the credentials"). Be careful not to check the code into the git repo with the hardcoded credentials (and don't push to GitHub because you would expose your credentials).
3) Can you send email from the application? Comment out the code that updates the spreadsheet and you should be able to send email if your credentials are correct.

I had this problem as well, and I was able to fix it by making the ENV["GMAIL_PASSWORD"] code in my contacts model all caps just as it is in my environment. It was not working when I only had the first letter caps in the code but everything in caps in the env variable, so be sure to consider the case sensitivity of your environments.

Related

Rails credentials only remembering old credentials?

Issue: I created a new Stripe account and connected it to my application, did all the new test publishing and secret api keys... Added to my credentials and config.
All of my keys are successfully called with :
Rails.application.credentials.dig(:stripe, :publishable_key),
...in both config and stripe.js file
I update my keys in credentials and make a test payment and get
No such token: tok_1EKxxxxxxxxxxxhQKy
I then manually put in the new publishable key in the stripe.js file, and it works...
To test, I tried entering the old test key, and i get the same errors. So i am assuming the credentials dig is getting my old keys because its the same error?
Is it possible the credentials is somehow not updating?
Now, one issue i can think of is, in the past i used atom to update my credentials, and it never worked. I recently came across a post hat atom doesn't work with credentials but nano does. So i implemented this and it worked (up until i entered new keys in). So i am wondering if my old inputs into the old atom credentials could be interfering? I doubt it but might be worth mentioning
What could be done to fix this as i want to implement using credentials?
All of the keys are test keys and in their right places.
I'm assuming, in the stripe.js file, the credentials or the dig is only fetching the old key?
Because when i manually put in the old test key, it doesn't work with same error as the credentials dig.
This was all working fine until i updated the credentials for the new keys...
Spring cached credentials file.
Run this command:
spring stop

Facebook OmniAuth throwing parameter app_id required when deployed on Heroku

So, I've been developing my app locally for sometime now and I decided today that I would like to deploy it on Heroku to start getting initial feedback for the app. I went through the guide on Heroku to help deploy my Rails 5 app to heroku.
Deployment was smooth and I was shown my login page. I clicked on Login Via FB and I was returned a white page with the following error response
The parameter app_id is required
Now, my obvious plan of action was to follow the trail, refer SO and hopefully come up with a solution for this. I tried every plausible resource on the web but I still get this error.
I've even updated my APP_ID and APP_SECRET variables on heroku. I've even updated Valid OAuth Redirect URIs section in my fb developer settings with the appropriate Heroku URL.
The funny bit is that everything works smoothly on my localhost but fails on heroku which is strange.
I strongly believe that Facebook would through appropriate errors and that app_id is in fact not being sent to fb. If this is the case, how do I solve this?
If you need code snippets to look at, do ask what might be needed to be looked at and I shall update my post accordingly.
Thank you!
the solution turned out to be a pretty simple one. In my application.yml file I had saved my Facebook vars as facebook_app_id and facebook_secret_key. But in my Heroku config vars I had included them as FACEBOOK_APP_ID and FACEBOOK_SECRET_KEY. I was under the impression keys aren't case sensitive. When I changed them to lowercase for Heroku, it worked.

How to safely generate account activation links in Rails / cloud 9 ide

I have been following the rails tutorial by Michael Hartl for some time. When I came to the point of creating account activation links and automatically send them in an Email, I noticed some bad stuff:
I'm using the cloud 9 ide and the activation link from the Email only works when the cloud 9 project is private. Since I need to store the password for my gmail account in the configs, everyone can view my code and log in to my gmail account.
How could this problem be solved? Making the project private doesn't work because you then aren't able to activate your account.
If you'd like to keep your workspace private, but your application url public, just click on the 'Share' button near the top right of the IDE. Once there, check 'Public' next to 'Application'. That will disable Cloud9's authentication when accessing the application url while keeping your code/workspace private.

ruby google api client, service account and Google app to use google drive

You can skip to the problem below but in short I can auth, upload and get a file list but it doesn't correspond to the gmail account I setup with the service account and google dev console
longer version:
I created a new email account say blah#gmail.com and used the google development console to enable drive, contacts and a few other APIs for my project.
Then I created the service account API keys/secrets and downloaded the client_secrets.json as well.
It took a while but got authentication working with the ruby google-api-client and I can login via rails console.
Problem:
I'm getting file.list but it only has the Getting Started.pdf even though I've created a few docs and spreadsheets on the blah#gmail.com service account.
I can also upload a file but don't see it.
I "think" that it's uploading to Google Apps related account? but I have neither set one up nor do I own domain for the organization I'm setting this up with.
I have no idea where the files are going or uploading or how I can access the drive account I actually did setup with blah#gmail.com
what's going on .
EDIT:
Can i get some code example on how to modify this to impersonate user?
require 'google/apis/drive_v2'
Drive = Google::Apis::DriveV2
#drive = Drive::DriveService.new
scope = 'https://www.googleapis.com/auth/drive'
#drive.authorization = Google::Auth.get_application_default([scope])
#give me list of files
#files = #drive.list_files
puts "Search results:"
#files.items.each do |file|
puts "- File: #{file.title} (#{file.id})"
end
If I have to setup google apps this might be tough since they only have subdomains and I don't have direct access. The type of thing I wish I had been told up front.

Unauthorized Access with Paypal REST API

Everything works fine in a development/localhost environment.
When deploying to heroku, using sandbox/test credentials on a new app, and creating a new payment I am getting
A PayPal::SDK::Core::Exceptions::UnauthorizedAccess occurred in orders#create:
Failed. Response code = 401. Response message = Unauthorized.
Again, everything thing works in a development environment
Should I permit any specific domains (like in Facebook integration)?
If you are running a Rails application on a public server, there is quite a chance that the Rails environment is set to "production".
If you further use the config/initializers/paypal.rb and config/paypal.yml generated by the Paypal API SDK's standard generator, then:
the initializer loads configuration for the current Rails environment, which is "production"
and in the production configuration one important line presumes that you want to run on the live API
So most probably you are attempting to communicate with the live API without knowing it. Changing the line to mode: sandbox should make you happy.
I had this problem and I called Paypal. It was a config problem in my account.
To resolve I went to https://developer.paypal.com, logged in, Then clicked on Dashboard Then on my account (left) The Direct credit cards has a grey x next to it so you have to click the link next to it that reads Enable live credentials Once you do this you have to answer some verification questions then they approve your account then your live and the unauthorized error goes away.

Resources