On Monday, 15 June 2015 at 14:41:32 UTC, Taylor Gronka wrote:
Hello,

I've picked up a web design project, and I would love to use vibe.d/dlang. I need to use oauth to search on a number of web api's, such as searching for Twitter tweets.

I tried using Adam's oauth (Thanks!). However, it doesn't appear support the validation needed for searching Twitter.
https://github.com/adamdruppe/arsd/blob/master/oauth.d


Now I'm looking at std.net.curl, and this is my thought: is HTTP.addRequestHeader(key, val) able to set the oauth authentication headers? It would look a bit odd, but if something like this works, then it wouldn't be difficult to code for each set of headers specifically:
        
auto client = HTTP("https://api.twitter.com";); // not sure if https should be set here or somewhere else
    string bearerUrl = "https://api.twitter.com/oauth2/token";;
client.addRequestHeader("Authorization", "Basic eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw");
    client.postData("grant_type=client_credentials");
    client.perform();

(The above example is based on Step 2 of this page)
https://dev.twitter.com/oauth/application-only


Of course, the oauth headers look a bit different, but I imagine it being something like this: client.addRequestHeader("Authorization", "OAuth2 token: \"string\", tokensecret: \"string\", appkey, \"string\" ...");
\\ not sure if that's how to escape quotes in dlang


Can I get some guidance on this? I would really like to use dlang, but I can't afford to be stuck on this for too long. I'd be happy to try and write a modification of Adam's code, but I lack experience, so it's a slow process for me.

I'd also be happy to write up little tutorials/blogposts about how I accomplish various tasks.

Thanks,

You can use vibe.d to send requests rather than going the lowlevel curl library.

Check out the requestHTTP method [1]
From code I've written in the past, your setup looks correct.

This is some vibe.d code I've used to get a refresh token from Youtube (I know it's different from twitter, but the idea is the same)

YoutubeToken is a local struct that contains the access_token we need to perform searches/post videos etc in any subsequent requests.

/**
 * Returns a new YoutubeToken
 */
public YoutubeToken getRefreshToken(){
    string url = "https://accounts.google.com/o/oauth2/token";;

string postBody = "client_id=$CLIENT_ID$&client_secret=$CLIENT_SECRET$&refresh_token=$REFRESH_TOKEN$&grant_type=$GRANT_TYPE$"
            .replaceMap(
                [
                    "$CLIENT_ID$" : credentials.clientID,
                    "$CLIENT_SECRET$" : credentials.clientSecret,
                    "$REFRESH_TOKEN$" : credentials.refreshToken,
                    "$GRANT_TYPE$" : "refresh_token"
                ]
            );

logInfo("Getting new refresh token with URL: %s and postBody: %s", url, postBody);

    auto response = requestHTTP(url,
        (scope req){
            req.method = HTTPMethod.POST;
            req.contentType = "application/x-www-form-urlencoded";
            req.writeBody(cast(ubyte[])postBody);
        }).bodyReader.readAllUTF8().parseJsonString;

return YoutubeToken(response["access_token"].get!string, response["expires_in"].get!long, response["token_type"].get!string );
}


[1] http://vibed.org/api/vibe.http.client/requestHTTP

Reply via email to