我尝试使用RestClient将小图片文件上传到Google云端硬盘。我已经有一个访问令牌(在早期的代码中被请求),但我不知道如何形成有效载荷。
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 :(得分: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"
)