cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Musicians, convert your MuseScore files to PDF to play music on the go! Learn more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error 404 when trying to get refresh token using CURL C++

Error 404 when trying to get refresh token using CURL C++

pakk4
New member | Level 2

Hello, I am currently trying to get the refresh token for my scoped app using CURL C++ but I am just getting error 404 from the buffer.

 

Here is my code:

 

void get_refresh_token()
{
	CURL* curl = curl_easy_init();

	if (curl)
	{
		struct curl_slist* header_list = 0;

		header_list = curl_slist_append(header_list, "Authorization: Basic <APP_KEY>:<APP_SECRET>");
		header_list = curl_slist_append(header_list, "Content-Type: application/x-www-form-urlencoded");
		header_list = curl_slist_append(header_list, "Dropbox-API-Arg: {\"code\":\"/<ACCESS_CODE>\",\"grant_type\": \"authorization_code\"}");

        std::string buffer;

		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
	    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
		curl_easy_setopt(curl, CURLOPT_URL, "https://api.dropboxapi.com/oauth2/token");

		CURLcode result = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
	}

	curl_global_cleanup();
}

 

 

Any help would be appreciated, thank you.

2 Replies 2

Greg-DB
Dropbox Staff

Based on this code, it looks like you're not formatting this HTTP request correctly:

  • Make sure you're using the POST method (i.e., not GET)
  • Send the parameters as application/x-www-form-urlencoded data in the request body, not as JSON in a Dropbox-API-Arg header
  • When sending the app key/secret in the Authorization header, be sure to encode them with base64 encoding

Please refer to the /oauth2/token documentation here for information on how to configure this request.

Здравко
Legendary | Level 20

Hi @pakk4,

Before trying to do something read carefully all relevant documents (i.e at least curllib and Dropbox API documentation), something you skipped as seems. 🙋

In addition to what Greg said, be careful not to pass unsupported types (don't rely on C++ type checking since libcurl is NOT C++, but a clear C library, so wont receive error or warning). By default (i.e. not explicitly changed) data type is supposed to be a pointer to FILE, not something else!

Some pseudocode follows:

 

 

const char* constexpr oauth2_token = "https://api.dropboxapi.com/oauth2/token";

void somefunc() {
  auto callback = [](char *ptr, size_t size, size_t nmemb, void *userdata) -> size_t {
    std::string& dataBuf = *(std::string*)userdata;
    dataBuf.append(ptr, size*nmemb);
    return size*nmemb;
  };
  CURL *curl = curl_easy_init();
  if (curl) {
    std::string dataBuf;
    std::string body("grant_type=authorization_code&code=");
    body += "<your access code>";
    curl_easy_setopt(curl, CURLOPT_URL, oauth2_token);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
    curl_easy_setopt(curl, CURLOPT_USERPWD, "<APP_KEY>:<APP_SECRET>");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&dataBuf);
    CURLcode res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    
    // ... do whatever you want with your result here.
  }
}

 

 

Adapt it to your needs.

Good luck.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?