I'm trying to post a tweet to Twitter with the following code, but keep getting the error "Could not authenticate you","code":32":
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
char *signedurl = malloc(sizeof(char) * 1024); /* Not how it will be, but works in this example. */
char *url = "https://api.twitter.com/1.1/statuses/update.json";
signedurl = oauth_sign_url2(url, NULL, OA_HMAC, "POST", consumer_key, consumer_secret, user_token, user_secret);
char *status = "status=test";
curl_easy_setopt(curl, CURLOPT_URL, signedurl); /* URL we're connecting to, after being signed by oauthlib */
curl_easy_setopt(curl, CURLOPT_USERAGENT, "dummy-string"); /* Not the actual string, just a dummy for this example */
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, status);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
int curlstatus = curl_easy_perform(curl); /* Execute the request! */
curl_easy_cleanup(curl);
free(signedurl);
Now, I looked at Twitter's documentation and the discussion on their website, and they recommend three things:
- Regenerate your access tokens. I've done this, and tried to post to Twitter via them using the developers console in the Twitter desktop program I have, and that works. This makes me think that the problem is elsewhere.
- Include
Content-Type: application/x-www-form-urlencoded
in the header of the request. My understanding is that this is default for how I'm using cURL, and indeed, when I use theVERBOSE
option, I can see that it is in the header. - Properly encode the url. I know that
oauth_sign_url2
does this correctly when fetching things from Twitter, and I've tried usingoauth_url_escape("status=test")
on the status (even though I don't think I'm supposed to do that). That doesn't work either.
I have a feeling it's something completely obvious, but I am stumped.