Best RoR approach for CURLing - ruby-on-rails

I have the following piece of code in PHP. I'm looking for the best way to convert it to Ruby. I've looked at a few approaches, including open-uri and the curb and wrapper curb-fu libraries. open-uri doesn't look very good, but I really like the curb-fu approach. But, I have a feeling using two libraries for this is overkill, there has to be a simpler way to accomplish what this piece of code is doing.
#Setup connection
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $resource_uri);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_USERPWD, $site_public_key . ":" . $site_private_key);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_FAILONERROR, 0);
#Send request
$result_json = curl_exec($curl)

Your best bet is to use rest-client. Its api is really cool and lightweight :
result = RestClient::Request.new({:user => "username", :password => "password",
:method => :get, :url => "www.whatever.com"}).execute
or if you don't need auth you can simply do :
result = RestClient.get("http://www.whatever.com")

Related

Which youtuve v3 api not supporting etag

I am using youtuve v3 https://www.googleapis.com/youtube/v3/search api to get the list of videos in a channel.
Also, trying to use etag to use caching to avoid quota limit in youtube api. I am always get different etag for each request.
So i would like to know is this search api support etag caching?. I am using php curl to get the results.
eg:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_HEADER, 'If-None-Match: "'.$eTag.'"' );
$curl_exec = curl_exec($curl);

Asana API Adding an Attachment via cURL/PHP

Experiencing difficulty trying to successfully add an attachment to a task using cURL/PHP.
$data = array("file" => "#$filename");
$headers = array("Authorization: Basic " . base64_encode($asana_api_key) . ":"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.asana.com/api/1.0/tasks/$task_id/attachments");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
Assume $filename, $asana_api_key, and $task_id are set properly. Should be fairly straightforward... but for some unknown reason I get the following response:
{"errors":[{"message":"Server Error","phrase":"19 snobby warthogs cheer busily"}]}
Oddly enough, running this request using curl command line works perfectly. I've tinkered with this for hours now to no avail.
Any ideas as to what the issue may be?
This was a bug on our end, and should now be fixed. It had to do with the way curl was sending the filename, namely, as the entire path to the file. Browsers typically send only the basename (without the directory). We now apply the same logic ourselves if we get a full path for our local name, and it seems like it works, but your case may be different.
I'd certainly appreciate knowing if it works for you now.

Unable to refresh Reddit OAuth 2.0 access token

I cannot refresh the Reddit access token.
When I send following request to https://ssl.reddit.com/api/v1/access_token
Content-Type: application/x-www-form-urlencoded
Authorization: #####
client_secret=#####&grant_type=refresh_token&client_id=#####&refresh_token=#####
I get status 200 but content is {"error": "invalid_request"}.
According to OAuth 2.0 spec and Reddit spec I do everything right.
I've also tried it without client_id and client_secret with the same result.
Am I missing something?
Reddit's OAuth implementation is really unique (and not in a good way).
The necessary parameters for refreshing tokens on reddit are:
client_id
client_secret
grant_type (=refresh_token)
refresh_token
scope
state
duration
redirect_uri
You'll also need the basic HTTP authentication header with client_id as login and client_secret as password.
I had to look up reddit's source code to figure out what was missing from my requests... So much development time lost on trivial matters.
In case anyone is looking for more explicit answer:
Here is how I did this in PHP.
$authorizeUrl = 'https://ssl.reddit.com/api/v1/access_token';
$clientId = "YOUR_CLIENT_ID";
$clientSecret = "YOUR_CLIENT_SECRET";
$post = array(
"client_id" => $clientId,
"client_secret" => $clientSecret,
"grant_type" => "refresh_token",
"refresh_token" => "STORED_REFRESH_TOKEN_VALUE",
"scope" => "identity",
"state" => "WHATEVER_VALUE",
"duration" => "temporary",
"redirect_uri" => "https://example.com/reddit",
);
$payload = http_build_query($post);
$ch = curl_init($authorizeUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

Retweet using CURL

I am using twitter api for retweet. in my PHP file I am using CURL to send request like this
$ch = curl_init() or die('cURL not available');
curl_setopt($ch, CURLOPT_URL, $retweet_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$retweet_result = curl_exec($ch);
curl_close($ch);
Its giving "Unable to authenticate error." . I have already authenticated user and created a session.

Accessing Clickbank API through Delphi's TIdHTTP?

I am trying to access the ClickBank API from my Delphi project, to check if a customer has a valid subscription.
I found the API Documentation here, but there are no Delphi examples. So I am trying to create my own little example, however I just cant figure it out with Indy's TIdHTTP.
Could anyone point me in the right direction, perhaps set up a minimal example?
P.S: I tried looking at the C# sample, however I cant port it to Delphi.
ClickBank sample C# is found here https://sandbox.clickbank.com/api_12_examples/api_example.csharp
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("https://api.clickbank.com/rest/1.2/orders/list");
request.Accept = "application/xml";
request.Headers.Add(HttpRequestHeader.Authorization,
"<< DEVELOPER KEY >>:<< API KEY >>");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
The PHP version is
https://sandbox.clickbank.com/api_12_examples/api_example.php
You'll see that they aren't doing much setup here... just setting two headers and performing a GET.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.2/orders/list");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_GET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/xml",
"Authorization: << DEVELOPER KEY >>:<< API KEY >>"));
$result = curl_exec($ch);
curl_close($ch);
In Delphi - a quick demo is to drop a TIdHTTP1 client on a form, along with a Button and a Memo. Then on an onclick of the button (where xxx= your developer key and yyy= your api key) do the same - set two headers and perform a GET:
IdHTTP1.Request.Accept := 'application/xml';
IdHTTP1.Request.CustomHeaders.Add('Authorization: xxx:yyy');
Memo1.Text := IdHTTP1.Get('https://api.clickbank.com/rest/1.2/orders/list');

Resources