【发布时间】:2019-04-15 06:12:52
【问题描述】:
我想使用 C 编程将文件上传到 Google Drive。
我遵循了Google's tutorial 和libcurl example 的以下说明。
POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media HTTP/1.1
Content-Type: image/jpeg
Content-Length: [NUMBER_OF_BYTES_IN_FILE]
Authorization: Bearer [YOUR_AUTH_TOKEN]
[JPEG_DATA]
但我不知道应该用[JPEG_DATA] 替换什么。是指文件名吗?
对于Content-Length: [NUMBER_OF_BYTES_IN_FILE],这是否意味着我应该用我的计算机显示的确切文件大小替换它?
到目前为止我写的代码:
curl_handle = curl_easy_init();
struct curl_slist *header = NULL;
/* Content-Type: image/jpeg */
header = curl_slist_append(header, "Content-Type: image/jpeg");
不知道在下面的[NUMBER_OF_BYTES_IN_FILE] 中放什么
/* Content-Length: [NUMBER_OF_BYTES_IN_FILE] */
header = curl_slist_append(header, "Content-Length:[NUMBER_OF_BYTES_IN_FILE]");
/* Authorization: Bearer [YOUR_AUTH_TOKEN] */
char auth[200];
strcat(auth, "Authorization: Bearer ");
strcat(auth, access_token);
header = curl_slist_append(header, auth);
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, header);
/* POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media HTTP/1.1 */
curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.googleapis.com/upload/drive/v3/files");
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, "uploadType=media");
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl_handle);
/* Print response */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res4));
}
else {
printf("%s\n", chunk.memory);
}
curl_easy_cleanup(curl_handle);
free(chunk.memory);
谢谢!
【问题讨论】:
-
@ybungalobill 嗨,但是如果我要上传图片,如何输入图片的内容?
标签: c rest http google-drive-api libcurl