2016-03-04 96 views
0

我正嘗試使用RestClient將小圖片文件上傳到Google Drive。我已經有一個訪問令牌(在先前的代碼中被請求),但我不知道如何形成有效負載。使用Ruby RestClient上傳到Google Drive API

的API文檔說明的要求應該是這樣的:

POST /upload/drive/v3/files?uploadType=media HTTP/1.1 
Host: www.googleapis.com 
Content-Type: image/jpeg 
Content-Length: number_of_bytes_in_file 
Authorization: Bearer your_auth_token 

JPEG data 

我曾嘗試以下,但它導致一個錯誤:

require 'rest-client' 

file = File.open("./uploaded-by-api.jpg") 
response = RestClient::Request.execute(method: :post, url: 'https://www.googleapis.com/upload/drive/v3/files', payload: { uploadType: "media", file: file, }, headers: { Authorization: "Bearer #{access_token}", "Content-Type" => "image/jpeg", "Content-Length" => "1000" }) 

我敢肯定有錯在我包括實際的文件數據的方式,但我找不到任何與RestClient的例子。

回答

0

好與解決它下面:

response = RestClient.post(
    'https://www.googleapis.com/upload/drive/v3/files', 
    { 'uploadType' => "media", 'upload' => file }, 
    "Authorization" => "Bearer #{access_token}", 
    "Content-Type" => "image/jpeg", 
    "Content-Length" => "1000" 
)