簡體   English   中英

通過ruby在Google雲端硬盤中創建文件夾時出錯

[英]Error creating folder in google drive through ruby

我在使用ruby中的google-drive-api庫在Google雲端硬盤中創建文件夾時遇到問題。 我可以成功登錄,列出文件夾,甚至可以將文件添加到根文件夾。 我在根文件夾下有一個名為'Applications'的文件夾。 列出所有文件顯示該文件夾的ID,讓我們稱之為“0BwiVxEBt”。在我的控制器中,我成功啟動了谷歌驅動器連接(因為我能夠列出所有文件),然后調用create_parent('Test')方法在我的google_drive_connection.rb中:

def create_folder (title)
file = @drive.files.insert.request_schema.new({
    'title' => title,
    'description' => title,
    'mimeType' => 'application/vnd.google-apps.folder'
})
file.parents = [{"id" => '0BwiVxEBt'}] # 0BwiVxEBt is id for Applications folder
media = Google::APIClient::UploadIO.new(title, 'application/vnd.google-apps.folder')
result = @client.execute(
        :api_method => @drive.files.insert,
        :body_object => file,
        :media => media,
        :parameters => {
            'uploadType' => 'multipart',
            #'convert' => false,
            'alt' => 'json'})
    if result.status == 200
        return result.data
    else
        puts "An error occurred: #{result.data['error']['message']}"
        return nil
    end
end

對於我在其他帖子中看到的插入文件夾就像文件插入(在此rb文件中的ahother方法中工作正常),沒有擴展名的標題,以及mimeType為'application / vnd.google-apps.folder “

錯誤:沒有這樣的文件或目錄 - 測試

可能是一個小問題,但似乎無法找到它!

感謝您提供的任何指針/幫助。

編輯發現問題,所以對於對解決方案感興趣的任何人,對於文件夾,您只需要在請求中發送元數據,不需要媒體部分,只需要正文(元數據)信息,因此我的'create_folder'方法導致:

def create_folder (title)
  file = @drive.files.insert.request_schema.new({
    'title' => title,
    'description' => title,
    'mimeType' => 'application/vnd.google-apps.folder'
  })
  file.parents = [{"id" => '0BwiVxEBt'}] # 0BwiVxEBt is id for Applications folder
  result = @client.execute(
        :api_method => @drive.files.insert,
        :body_object => file)
  if result.status == 200
    return result.data 
      else
    puts "An error occurred: #{result.data['error']['message']}"
    return nil
  end
end

找到了我的問題的答案,在解決方案的原始問題中添加了編輯。 簡而言之,對於文件夾,只添加正文部分,而不是請求中的媒體部分。 對於文件夾,只需要元數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM