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?
答案 0 :(得分:0)
你问过很久以前......所以这是为了防止我的回答会帮助打开此页面的其他人寻找相同的解决方案。
假设您以这种方式列出文件:
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
在你的some_page.html.erb中这段代码:
<% @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 %>