I'm trying to work through the application-only authentication for Twitter (
https://dev.twitter.com/docs/auth/application-only-auth), but I can't seem
to get the POST request right at step 2.
I'm fairly certain I have the credentials portion right, but for whatever
reason I can't get Twitter to recognize the body of my request. Here's what
I've tried:
using Codecs, HttpCommon, Requests
consumer_key = "6nOtpXmf4bYuu3...";
consumer_secret = "sES5Zlj096St0O65VX...";
#Function to create authentication header
function encodecredentials(consumer_key::String, consumer_secret::String)
#URI encode keys for future-proofing, per Twitter docs
bearer_token_credentials =
"$(encodeURI(consumer_key)):$(encodeURI(consumer_secret))"
return(base64(bearer_token_credentials))
end
#1. Try POST request by itself - Expect an error
response = post("https://api.twitter.com/oauth2/token")
println(response.data)
{"errors":[{"label":"authenticity_token_error","code":99,"message":"Unable
to verify your credentials"}]}
#2. Add in authentication
#Change in error message suggests Twitter okay with my Authorization
credentials
response = post("https://api.twitter.com/oauth2/token";
headers = {"Authorization" => "Basic
$(encodecredentials(consumer_key, consumer_secret))",
"Content-Type" =>
"application/x-www-form-urlencoded;charset=UTF-8"},
data = "grant_type=client_credentials")
println(response.data)
{"errors":[{"label":"forbidden_missing_parameter","code":170,"message":"Missing
required parameter: grant_type"}]}
#3. Try authentication with data as Dict instead of string
#Get same error message
response = post("https://api.twitter.com/oauth2/token";
headers = {"Authorization" => "Basic
$(encodecredentials(consumer_key, consumer_secret))",
"Content-Type" =>
"application/x-www-form-urlencoded;charset=UTF-8"},
data = {"grant_type" => "client_credentials"})
println(response.data)
{"errors":[{"label":"forbidden_missing_parameter","code":170,"message":"Missing
required parameter: grant_type"}]}
So regardless of how I specify the body parameter, Twitter doesn't want to
recognize what I'm doing. Is there a different way I'm supposed to add data
to a POST request?