1
votes

I am developing a web-app where-in users are be able to create content in their own Google Drive account and share it with others. To allow this I would like the app to access the folder shared (say, publically) by that user through the drive rest api and present the content to other users of the app. (To clarify, I do not want to list or show the files through the google drive website but read its contents programmatically and process it within the app).

Would such a scenario be possible with google drive and if so how should I proceed setting it up?

Thanks in advance

PS: I looked at service account but it seems that every user would have register the same app if they want to share contents of their drive with others through that app. Have I got this correct?

1

1 Answers

0
votes

You asked a long time ago... So this is just in case my answer will help somebody else who opened this page looking for the same solution.

let's say you you list files in this way:

def google_files

  client = Google::APIClient.new
  client.authorization.access_token = Token.last.fresh_token
  drive_api = client.discovered_api('drive', 'v2')

  @result = Array.new
  page_token = nil
  begin
    parameters = {:orderBy => 'folder'}
    if page_token.to_s != ''
      parameters['pageToken'] = page_token
    end
    api_result = client.execute(
        :api_method => drive_api.files.list,
        :parameters => parameters)
    if api_result.status == 200
      files = api_result.data
      @result.concat(files.items)
      page_token = files.next_page_token
    else
      puts "An error occurred: #{result.data['error']['message']}"
      page_token = nil
    end
  end while page_token.to_s != ''
  @result
end

In your some_page.html.erb this code:

<% @result.each do |f| %>
    <% if f.mimeType == 'application/vnd.google-apps.folder' %>
        <% if f.parents.any? %>
            <% f.parents.each do |parent_root| %>
                <% if parent_root.is_root %>

                    <!-- these are your folder in the root of your disk - 'My Disk' -->
                    <%= image_tag f.icon_link %>  <%= link_to f.title, f.alternate_link, class: 'btn btn-default btn-sm', :target => "_blank" %>
                <% end %>
            <% end %>
        <% else %>

            <!-- these are your folder in the root of the 'Shared with me' -->
            <%= image_tag f.icon_link %>  <%= link_to f.title, f.alternate_link, class: 'btn btn-default btn-sm', :target => "_blank" %>
        <% end %>
    <% else %>
        <% if f.parents.any? %>
            <% f.parents.each do |parent| %>
                <% if parent.isRoot %>

                    <!-- these are your Files in the root of your disk - 'My Disk' -->
                    <%= image_tag f.icon_link %>  <%= link_to f.title, f.alternate_link, class: 'btn btn-default btn-sm', :target => "_blank" %>
                <% end %>
            <% end %>
        <% else %>

            <!-- these are your Files in the root of the 'Shared with me' -->
            <%= image_tag f.icon_link %>  <%= link_to f.title, f.alternate_link, class: 'btn btn-default btn-sm', :target => "_blank" %>
        <% end %>
    <% end %>
<% end %>