Libcurl Application-only authentication in c


#1

Code

curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, “https://api.twitter.com/oauth2/token”);
curl_easy_setopt(curl, CURLPROTO_HTTPS, 1L);
curl_easy_setopt(curl, CURLOPT_PORT, 443);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 29L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, “grant_type = client_credentials”);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
curl_easy_setopt(curl, CURLOPT_CAINFO , “./CA.crt”);

query (wireshark log)

POST /oauth2/token HTTP/1.1
Host:api.twitter.com
User-Agent: beetbot v0.1
Authorization: Basic (my base64 encoded key)
Content-Type:application/x-www-form-urlencoded;charset=UTF-8
Content-Length:29
Accept-Encoding:gzip

grant_type=client_credentials

respons

HTTP/1.1 403 Forbidden
content-encoding: gzip
content-length: 42
content-type: text/plain;charset=utf-8
date: Wed, 28 Jan 2015 06:08:07 UTC
server: tsa_b

what am I doing wrong?


#2

We have a working Twitter Oauth implementation using libcurl in C, and the main differences I can see between our code and this is we explicitly set SSL_VERIFYPEER to 0, and otherwise don’t explicitly set any of the various SSL/CAINFO/HTTPS/PORT options you have here. Many of these probably make no difference as they will be set the same due to the URL provided (ie it being ‘https’) or system broader system configs. I know there was discussion and issues around this a year or so ago, on the older mailing list IIRC, but am not 100%. Hopefully there is a clue there though that will help you get it working. The other thing is to make sure your system time is correct.

Cheers,

JB.


#4

clear code

headerlist = curl_slist_append(headerlist, "User-Agent: curl/7.39.0");
char autorize [200] = "Authorization:Basic ";
strcat(autorize, keyv);
headerlist = curl_slist_append(headerlist, autorize);
headerlist = curl_slist_append(headerlist, "Content-Type:application/x-www-form-urlencoded;charset=UTF-8");
headerlist = curl_slist_append(headerlist, "Content-Length:29");
headerlist = curl_slist_append(headerlist, "Accept-Encoding:gzip");

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "https://api.twitter.com/oauth2/token");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

Work )