【问题标题】:Google Drive: Rest API access to another users shared folder in a google drive accountGoogle Drive:Rest API 访问 Google Drive 帐户中的其他用户共享文件夹
【发布时间】:2015-06-26 10:13:06
【问题描述】:

我正在开发一个网络应用程序,用户可以在其中创建自己的 Google 云端硬盘帐户中的内容并与他人共享。为此,我希望该应用程序通过 drive rest api 访问该用户共享(例如公开)的文件夹,并将内容呈现给该应用程序的其他用户。 (澄清一下,我不想通过 google drive 网站列出或显示文件,而是以编程方式阅读其内容并在应用程序中处理它)。

使用 google drive 会不会出现这种情况?如果可以,我应该如何进行设置?

提前致谢

PS:我查看了服务帐户,但似乎每个用户都需要注册同一个应用,如果他们想通过该应用与其他人共享其驱动器的内容。我猜对了吗?

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    您很久以前就问过...所以这是以防万一我的回答会帮助打开此页面的其他人寻找相同的解决方案。

    假设您以这种方式列出文件:

    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 %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      相关资源
      最近更新 更多