使用Ruby RestClient上传到Google Drive API
问题描述:
我正尝试使用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的例子。
答
好与解决它下面:
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"
)