Rails: export data to google spreadsheet
I am new to Rails and I want to export data to Google Spread Sheet from my web application.
- I created an app to get client id and client secret and also enable disk api for this.
- I have google drive and google api gmail client installed
- And I used the code given here
This code runs successfully, opens a new login tab and displays the embed code. This is where I got stuck. The code that requires google authorization is in my controller code, so my user can paste that code into my controller. I know its a quiet silly thing to ask, but I don't find a way to automatically get the code from the api for further execution like we usually do in our facebook oauth apps. So can you advise me how to do this? The code looks like
def export_spred_sheet
require 'rubygems'
require 'google/api_client'
require 'launchy'
# Get your credentials from the console
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Create a new API client & load the Google Drive API
client = Google::APIClient.new
drive = client.discovered_api('drive', 'v2')
# Request authorization
client.authorization.client_id = CLIENT_ID
client.authorization.client_secret = CLIENT_SECRET
client.authorization.scope = OAUTH_SCOPE
client.authorization.redirect_uri = REDIRECT_URI
uri = client.authorization.authorization_uri
Launchy.open(uri)
# Exchange authorization code for access token
$stdout.write "Enter authorization code: "
client.authorization.code = gets.chomp
client.authorization.fetch_access_token!
# Insert a file
file = drive.files.insert.request_schema.new({
'title' => 'My document',
'description' => 'A test document',
'mimeType' => 'text/plain'
})
media = Google::APIClient::UploadIO.new('document.txt', 'text/plain')
result = client.execute(
:api_method => drive.files.insert,
:body_object => file,
:media => media,
:parameters => {
'uploadType' => 'multipart',
'alt' => 'json'})
# Pretty print the API result
jj result.data.to_hash
end
Or is there any other way to accomplish the task If I'm wrong?
source to share
I also struggled with this in one of my projects and finally I found soul contemplation like this:
Instead of using the client and client ID, you can use the P12 key generated in the google developer console in the service account for authentication. In this case, you do not need to insert any code into the controller.
To generate key p12
- go to google developer console
- then → "APIs and Auth" → "Credentials"
- Create a new client ID of type "Service Account"
- After creating a new customer ID. Under the Service Account section, you will find the Generate New P12 Key button. When clicked, it will generate p12 key. Download it and save it securely in your application and use it for authentication.
And use the following piece of code to retrieve the access token.
key_file = p12_key_file_path
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, 'notasecret')
client = Google::APIClient.new application_name: "abcd", application_version: "1.0.0"
SERVICE_ACCOUNT_ID (used in the following code snippet) will be the Email generated in this Service Account section of the Google Developer Console .
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/analytics',
:issuer => SERVICE_ACCOUNT_ID,
:access_type => 'offline',
:signing_key => key
)
client.authorization.fetch_access_token!
client
Hope this helps you.
source to share