You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

pakk4's avatar
pakk4
New member | Level 2
11 months ago

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

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.

  • 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.

  • 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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,888 PostsLatest Activity: 2 days ago
326 Following

If you need more help you can view your support options (expected response time for an email or 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!