Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
Hi, I was googling for a way to upload a file to any cloud storage using c++ and came across dropbox api. I found the code, so I did a copy and paste to try and see if it works. I entered the Access token and ran the code, the upload failed showing this error:
"Error in call to API function "files/upload": The given OAuth 2 access token is malformed."
I suspect the error could be related to this line of code:
curl_easy_setopt(curl, CURLOPT_URL, "https://content.dropboxapi.com/2/files/upload");
But I am new in this so I have no idea how to solve it.
Hi @hb_,
Definitely the problem is not in cited code line. Did you set properly CURLOPT_XOAUTH2_BEARER parameter?
Hope this gives some direction.
@Здравко Thank you. You were right, I guess this time I set CURLOPT_XOAUTH2_BEARER parameter properly so it gave another error:
"Error in call to API function "files/upload": Must provide HTTP header "Authorization" or URL parameter "authorization"."
I am searching to find out what this error could be about.
If you can help in any way I would much appreciate.
hey @Здравко I think it worked
However I came to find that the code does not do what I actually want, what it does is just creates a new file and write something you specify to it but I want to upload a local file from my computer to my dropbox account?
Any idea how can I do that?
Hey @hb_,
Seems you have some progress. Most upload operations (no restricted to Dropbox) are POST based. It would be difficult (and impractical) to encode big file's content to GET parameters! Further, every content is transferred as... content and you have to direct where this content to be placed on. The target file name could be the same as source file name (if any) or different. The choice is yours! I'm not sure where you read reference info for CURL, but curl.haxx.se is good starting point and together with Dropbox API (also SDK) documents you can do what you want. Between: libcurl is C library, not C++ (as in your thread header).
Of course would be nice if there was Dropbox C/C++ SDK. It's really curious why 'fundamental' languages C and C++ are left without proper support from Dropbox side. There are many libraries providing underlying transport support that could be used as base for Dropbox 'shell'. For example Qt, WxWidgets, Boost and many others. Of course libcurl, discussed here, is also good example. This thread could be a proposal! @Greg-DB, What you would say about?
@Здравко Thanks for helping out! I can't speak to why any particular language was or wasn't supported in an official SDK, but I'll pass this along as a feature request for an official C/C++ Dropbox SDK. I can't promise if or when that might be implemented though.
@hb_ If you're still having trouble, please share the code you have so far so we can take a look.
For reference, when uploading a file to Dropbox via the Dropbox API /2/files/upload endpoint, the data for the file you want to upload needs to be sent in the request body. For example, you might do something like this:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "data for upload here");
To get the data you want to upload, you can use whatever local file I/O access you have to read the data from the local file.
Hi @Greg-DB , thanks for replying so quick
I followed your suggestion and tried tp use std::ifstream like this:
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(); string test = "D:\\Desktop\\test.txt"; std::ifstream file(test); if(curl) { printf ("Running curl test.\n"); struct curl_slist *headers=NULL; /* init to NULL is important */ headers = curl_slist_append(headers, "Authorization: Bearer <my_access_token>"); headers = curl_slist_append(headers, "Content-Type: application/octet-stream"); 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/upload"); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, file); curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, FALSE); /* 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");
but it didn't compile displaying this error:
C:\Min.GW\curl-7.64.1-win64-mingw\include\curl\curl.h|2857|error: use of deleted function 'std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const std::basic_ifstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]'|
But before that I had this code:
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 <my_access_token>"); headers = curl_slist_append(headers, "Content-Type: application/octet-stream"); headers = curl_slist_append(headers, "Dropbox-API-Arg: {\"path\":\"/D:\\Desktop\\test.txt\"}"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_URL, "https://content.dropboxapi.com/2/files/upload"); curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, FALSE); /* 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");
And this would just create new folders and files in my account
Don't know if the codes I posted might be too long, the difference between the two is only at this line:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "test data for upload");
It's error to pass ifstream object as char sequence! You have to read the file content.
@Здравко yeah
you are right I can read a text file content into a char and pass it as a parameter but what aboout a audio file or a video?
The same. In addition you can use CURLOPT_POSTFIELDSIZE (you can't rely on zero termination).
Good example could be foud here.
Hi there!
If you need more help you can view your support options (expected response time for a ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!