Basic authentication in Github api with http request in Ruby? - ruby

I'm trying to use basic authentication in Github api. I wrote something like this:
require 'httpclient'
request = HTTPClient.new
request.set_basic_auth("http://api.github.com/authorizations", "my_username", "my_password")
request.get "http://api.github.com/user/repos"
and expect it returns the repos of a user. However, it keeps throwing an error:
Connection refused - connect(2) for "api.github.com" port 80 (Errno::ECONNREFUSED)
I know there are a whole bunch of gems like github, octokit that do the stuff for users. I want to try it myself. Does anyone know how do I authenticate with a simple http request in Ruby?

You need to make an HTTPS request instead of HTTP. Authentication always needs to use SSL.
You are also using the wrong URL's. As per their docs (https://developer.github.com/v3/auth/#basic-authentication) you need to set your basic authentication to the following path:
https://api.github.com/user
And make your repo request at:
"https://api.github.com/users/<USERNAME>/repos"
So your request would look something like
request = HTTPClient.new
request.set_basic_auth("https://api.github.com/user", "my_username", "my_password")
request.get "https://api.github.com/users/<USERNAME>/repos"
I suggest taking a look at the documentation that I linked to above, and make your first attempt by using curl requests, as they offer more information to help you debug.

Related

How can I GET an API with ruby if it's behind Cloudflare's Anti DDos protection

I was trying to scrape this api, but when i used Net::HTTP.get, it returned the cloudflare page asking me to wait for 5 seconds while it checks my browser.
I looked it up, and there's a module for python and for node.js, but none for ruby. Is this possible with maybe an argument to Net::HTTP or using curl?
You could try specifying a user agent perhaps:
http = Net::HTTP.new(url)
http.request_get(URL, {'User-Agent' =>'whatever'})

Trying to call parse.com cloud function using maigun route action

I'm trying to use a parse.com cloud function in a mailgun route action (forward).
My action is like this (with my app id and JS key included of course):
forward("https://myAppId:javascript-key:myJSkey#api.parse.com/1/functions/hello")
In the mailgun logs, I see it call, but I get the following error:
HTTP Error 401: Unauthorized Server response: 401 HTTP Error 401: Unauthorized
My function is just a simple response.send("OK");
Obviously I'm missing something.
Greg
The issue I think is that the Cloud Code calling convention requires you use special Parse headers, not just keys: it may be different if its being called from a browser with sets the referer headers. I'm not sure you'll be able to call it this way directly from Mailgun: you may need a proxy of some sort.
EDIT: I think you'll need to use the Express Webhook implementation instead, and then you can use standard basic authentication. Cloud Code is really for cases where you have control over the HTTP client you're using.

How do I debug HTTP of Ruby google-api-client

I'm trying to integrate with Google Drive, using the google-api-client. Since there's a lot of stuff going on, I would like to be able to see what's going over the wire (http level). It seems that the client uses Faraday for http connectivity. How would I get a wiredump out of Faraday for debugging?
Google api client uses Faraday.default_connection by default. It is thus possible to manipulate this object, which can be done simply with the following line of code:
Faraday.default_connection.response :logger
Took me a while to figure out.

Using the Google Custom Search API with Ruby google-api-client

As part of a people search project I'm currently participating in, I need to write a ruby script that can send search queries to the Google Custom Search API and store the search results for processing. I found the Ruby google-api-client gem (http://code.google.com/p/google-api-ruby-client/) and installed it, but, despite having thoroughly read the documentation, I am at a loss as to how to execute a Custom Search API call. This is my first attempt at using Google APIs and I'm finding the process a bit overwhelming, is there anyone out there with any experience that could provide some sample code for me to study? Thanks
While I haven't tested this, something like this should work:
require 'google/api_client'
# Creates an instance of the client.
client = Google::APIClient.new
# Authorization setup goes here.
# Fetch the discovery document and obtain a reference to the API we care about.
search = client.discovered_api('customsearch')
# Make an API call using a reference to a discovered method.
response = client.execute(
search.cse.list, 'q' => 'your query'
)
status, headers, body = response
Note that I've omitted all the setup code for authentication, which you can find in the docs for the Ruby client.
There's a few ins and outs with authentication when using an api key as opposed to OAuth thats outlined at the code abode.
You have to explicitly set the authorzation param to nil when constructing the client, otherwise the gem tries to use OAuth to authenticate, so if calling from a server using an api key only, you will always get a 401 Unauthorized. Full Code using the customsearch api is given (copy paste into irb). the code abode - google-api-client for ruby
https://developers.google.com/google-apps/calendar/firstapp
This walks you through getting setup to access the api and setting up keys in google's api console.
It has a tab for ruby - so this is what you need to get started.

is there a way using Ruby's net/http to post form data to an http proxy?

I have a basic Squid server setup and I am trying to use Ruby's Net::HTTP::Proxy class to send a POST of form data to a specified HTTP endpoint.
I assumed I could do the following:
Net::HTTP::Proxy(my_host, my_port).start(url.host) do |h|
req = Net::HTTP::Post.new(url.path)
req.form_data = { "xml" => xml }
h.request(req)
end
But, alas, proxy vs. non-proxied Net::HTTP classes don't seem to use the proxy IP Address. my remote service responds telling me that it received a request from the wrong IP address, ie: not the proxy. I am looking for a specific way to write the procedure, so that I can successfully send a form post via a proxy. Help? :)
Hah, turns out that is the right way to do it, my issue was actually with Squid and the API I was pushing to. Interesting tip related to this problem, if you are proxying with Squid proxy server, you probably want to add this server config option:
header_access X-Forwarded-For deny all
This will make sure that the proxy completely ignores any relation to the caller's IP address as far as the HTTP endpoint is concerned.
You may also want to look at the mechanize gem, based on Perl's WWW::Mechanize. If it's anything like the Perl one (and I'm led to believe it is), then it encapsulates much of the common mess that you're dealing with.
Ruby: http://mechanize.rubyforge.org/
Perl: http://search.cpan.org/dist/WWW-Mechanize/

Resources