保存文件从googledrive c++

saving file from googledrive c++

本文关键字:c++ googledrive 保存文件      更新时间:2023-10-16

我需要一些帮助。我正在尝试使用控制台从google drive保存一个标准文件。

代码如下:

 m_pHeadersList = NULL; 
 int NotF; // number of the file
 cout << endl << "choose file to download:"; cin >> NotF;
 access_token = FileLoad ("access_token");
 m_tmpStr = "Authorization: Bearer " + access_token;
 m_pHeadersList = curl_slist_append(m_pHeadersList, m_tmpStr.c_str());
 GETreq(curl, res, ws2s(files[NotF].downloadUrl) , m_pHeadersList); 
下面是GETreq函数的代码:
void GETreq(CURL *curl, CURLcode res, string URL, curl_slist* m_pHeadersList)
{
    curl = curl_easy_init();
    if(curl) {
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
    curl_easy_setopt(curl, CURLOPT_CAINFO , "cacert.pem");
    curl_easy_setopt(curl, CURLOPT_URL, URL);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL); 
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_pHeadersList); 
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_HEADER, 1);
    static const char *headerfilename1 = "head1.out";
    FILE *headerfile1;
    static const char *bodyfilename1 = "body1.out";
    FILE *bodyfile1;
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    headerfile1 = fopen(headerfilename1,"wb");
    bodyfile1 = fopen(bodyfilename1,"wb");
    curl_easy_setopt(curl,   CURLOPT_WRITEHEADER, headerfile1);
    curl_easy_setopt(curl,   CURLOPT_WRITEDATA, bodyfile1);
    res = curl_easy_perform(curl);
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %sn",
              curl_easy_strerror(res));
    fclose(headerfile1);
    fclose(bodyfile1);
    curl_easy_cleanup(curl);
    }
}

它可以工作,但它将文件(txt, xml甚至jpg)保存到body1。作为文本输出。我需要一些关于如何获得文件下载与自己的扩展名的建议。

处理了。刚刚更改了函数

中的参数
curl_easy_setopt(curl, CURLOPT_HEADER, 1);

curl_easy_setopt(curl, CURLOPT_HEADER, 0);