Скачивание файла через curl в c ++ из dropbox - PullRequest
0 голосов
/ 20 апреля 2019

Я хочу скачать файл из общей ссылки dropbox, используя curl в программе на c ++

Я нашел pipbox api pdf, который показал мне, как это сделать

#include <stdio.h>
#include <curl/curl.h>

int main (int argc, char *argv[])
{
     CURL *curl;
     CURLcode res;
     /* In windows, this will init the winsock stuff */
     curl_global_init(CURL_GLOBAL_ALL);
     /* get a curl handle */
     curl = curl_easy_init();
     if(curl) {
     printf ("Running curl test.\n");
     struct curl_slist *headers=NULL; /* init to NULL is important */
     headers = curl_slist_append(headers, "Authorization: Bearer 
     <ACCESS_TOKEN>");
     headers = curl_slist_append(headers, "Content-Type:");
     headers = curl_slist_append(headers, "Dropbox-API-Arg: 
     {\"path\":\"/test.txt\"}");
     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_URL,
    "https://content.dropboxapi.com/2/files/download");
     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
     /* Perform the request, res will get the return code */
     res = curl_easy_perform(curl);
     /* Check for errors */
     if(res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",

     curl_easy_strerror(res));
    /* always cleanup */
    curl_easy_cleanup(curl);
    printf ("\nFinished curl test.\n");
    }
          curl_global_cleanup();
         printf ("Done!\n");
           return 0;
         }

Однако предоставленные комментарии не дают мне большого объяснения, и я не могу заставить его работать.

Я не понимаю эти три строки кода:

headers = curl_slist_append(headers, "Authorization: Bearer <ACCESS_TOKEN>");

headers = curl_slist_append(headers, "Content-Type:");

headers = curl_slist_append(headers, "Dropbox-API-Arg:{\"path\":\"/test.txt\"}");

Я думаю, что должен заменить некоторые вещи, но я не знаю, что

Ответы [ 2 ]

2 голосов
/ 20 апреля 2019

«Я думаю, что мне нужно заменить некоторые вещи, но я не знаю, что»: замените <ACCESS_TOKEN> на фактический токен доступа.

Вы также должны установить «Контент»-Type: "заголовок к соответствующему значению для данных, которые вы выбираете.

Вы также должны изменить значение заголовка" Dropbox-API-Arg ", чтобы он соответствовал файлу, который вы пытаетесь получить.

0 голосов
/ 23 апреля 2019

Я наконец нашел решение своей проблемы.

Оказывается, мне не нужно было использовать Dropbox API

Вот код

#include <iostream>
#include <curl/curl.h>

using namespace std;

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
  size_t written;
  written = fwrite(ptr, size, nmemb, stream);
  return written;
}

int main(int argc, char** argv) {

CURL *curl;
FILE *fp;

const char* destination = "D:\\Desktop\\test.exe";

fp = fopen(destination, "wb");

curl = curl_easy_init();

/* A long parameter set to 1 tells the library to follow any Location: header 
 * that the server sends as part of an HTTP header in a 3xx response. The 
 *Location: header can specify a relative or an absolute URL to follow.
*/
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); 

curl_easy_setopt(curl, CURLOPT_URL, "https://www.dropbox.com/s/09nd26tdyto23yz/BankAccount.exe?dl=1"); // "dl=0"changed to "dl=1" to force download

// disabe the SSL peer certificate verification allowing the program to download the file from dropbox shared link
// in case it is not used it displays an error message stating "SSL peer certificate or SSH remote key was not OK"
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

CURLcode res;

res = curl_easy_perform(curl);

curl_easy_cleanup(curl);

fclose(fp);


if (res ==CURLE_OK)
    cout << "OK";
else
    cout << curl_easy_strerror(res);

return 0;
}

СпасибоВы, ребята, за попытку помочь мне.Я ценю

...