'A problem of using CURL (with C++) with GET method from Twitter API

Recently I'm into using CURL with the Twitter API, unfortunately my code isn't working (for now).

It was engineered to send GET requests to Twitter APIs and write received content to a file. Some code came from the Internet, I don't remember its source.

The code is working with HTTP, but all Twitter APIs use HTTPS, that's why I can't use it anymore. It's compiling and linking properly.

Here's a screenshot of the app describing the problem:

image

EDIT: I cut off the Bearer token due to safety.

EDIT: Sorry for not properly describing the problem, currently I'm new here.

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

void getFromTwitterToFile(std::string statusNumber)
{
    std::string bearerToken = ""; //secret!
    std::string data;
    std::string fullBearer = "Authorization: Bearer " + bearerToken;
    std::string tempFile = userSettings.tempFilePath + "temp.dat";
    std::string api_url = 
        "http://api.twitter.com/2/tweets/" 
        + statusNumber
        + "?tweet.fields=attachments,author_id,created_at,entities,id,text&media.fields=preview_image_url,url&expansions=attachments.media_keys";

    std::cout << api_url << std::endl;

    CURL* curl = curl_easy_init();
    CURLcode res;
    FILE* fp;
    
    //checking if curl initialised properly
    if (!curl)
    {
        std::cout << "Curl hasn't initialised properly" << std::endl;
    }

    //opening a file
    fp = fopen(tempFile.c_str(), "wb");

    //setting up api url
    curl_easy_setopt(curl, CURLOPT_URL, api_url.c_str());

    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

    //file 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_tech_writeRequestFile);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

    struct curl_slist* headers = NULL;
    headers = curl_slist_append(headers, "Content-Type: application/json");
    headers = curl_slist_append(headers, fullBearer.c_str());

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
    curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, bearerToken);

    //performing 
    res = curl_easy_perform(curl);

    if (res != CURLE_OK)
    {
        std::cout << "Error in performing CURL action" << std::endl;
        std::cout << curl_easy_strerror(res) << std::endl;
    }

    //cleanup
    curl_easy_cleanup(curl);
}


Solution 1:[1]

The code is working with HTTP, but all Twitter APIs use HTTPS, that's why I can't use it anymore

libcurl supports both HTTP and HTTPS. The real problem is that when you are sending your HTTP request, the server is redirecting you to an HTTPS url. libcurl can follow that redirect automatically for you, but only if you enable the CURLOPT_FOLLOWLOCATION option, which is disabled by default.

Though, you really should update your code to send requests to HTTPS urls to begin with, especially knowing that the Twitter API uses HTTPS.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Remy Lebeau