通过 HTTP 请求将文件上传到 Google 驱动器 (C)
Upload files to Google Drive via HTTP request (C)
我想使用 C 编程将文件上传到 Google 驱动器。
我遵循了 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);
谢谢!
[JPEG_DATA]
将是文件的确切内容,逐字节。 [NUMBER_OF_BYTES_IN_FILE]
是文件的大小,巧合的是 [JPEG_DATA]
.
中的字节数
您可以查看post-callback.c to see how to add a payload to a POST request, and httpput.c了解如何从文件中增量读取数据。
将图像视为常规字节。 Content-Type 将告诉特定的解析器 post body 中附加的字节代表什么。毕竟它全是零。只要满足 http 请求响应结构,您就不必负责分块数据并发送。该协议是 header 中每个字段的合同(在跳过的行之前),提供有关您的实际数据的元数据。一旦必要的信息(长度、类型、...)符合要求,只需将图像作为常规字节读入并附加到消息中。剩下的就是 http(tcp/ip) 问题了。
我想使用 C 编程将文件上传到 Google 驱动器。
我遵循了 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);
谢谢!
[JPEG_DATA]
将是文件的确切内容,逐字节。 [NUMBER_OF_BYTES_IN_FILE]
是文件的大小,巧合的是 [JPEG_DATA]
.
您可以查看post-callback.c to see how to add a payload to a POST request, and httpput.c了解如何从文件中增量读取数据。
将图像视为常规字节。 Content-Type 将告诉特定的解析器 post body 中附加的字节代表什么。毕竟它全是零。只要满足 http 请求响应结构,您就不必负责分块数据并发送。该协议是 header 中每个字段的合同(在跳过的行之前),提供有关您的实际数据的元数据。一旦必要的信息(长度、类型、...)符合要求,只需将图像作为常规字节读入并附加到消息中。剩下的就是 http(tcp/ip) 问题了。