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

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.

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'})

Basic authentication in Github api with http request in 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.

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.

How do I simulate a web session and store the received cookie using Ruby?

I am testing a messaging application that uses single sign-on(SSO). I need to simulate a user connecting to WAM using SSO, then I need to get the cookie from the server and store it for further communication events. Is anyone familiar with how this might be done using Ruby?
Mechanize does that automatically. There are also other HTTP client gems that may support cookies, e.g. this httparty example.
If your intention or need is to do this more 'organically' as it were, then you could use Watir to drive a browser, and retrieve the cookie (assuming it's stored on disk and not a session cookie) from wherever the browser stores it using normal file I/O functions. If it's a session cookie that's a bit tricker but you can usually see them with developer tools like firebug.
If you want something operating at the HTTP level that is simulating a browser, then See the other answer provided by MichaelW

Accessing SSL enabled Google Apps feed with http protocol

Building an app using a calendar on a Google Apps domain that has SSL enforced domain-wide. I initially found the problem when building a Rails app using the GCal4Ruby library, which used the allcalendars feed URL with a non-SSL protocol (GCal4Ruby debug output snippet [sic]):
…
url = http://www.google.com/calendar/feeds/default/allcalendars/full
Starting post
Header: AuthorizationGoogleLogin auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxGData-Version2.1
Redirect recieved, resending get to https://www.google.com/calendar/feeds/default/allcalendars/full?gsessionid=xxxxxxxxxxxxxxxxxxxxxx
Redirect recieved, resending get to https://www.google.com/calendar/feeds/default/allcalendars/full?gsessionid=xxxxxxxxxxxxxxxxxxxxxx
Redirect recieved, resending get to https://www.google.com/calendar/feeds/default/allcalendars/full?gsessionid=xxxxxxxxxxxxxxxxxxxxxx
…
This was interesting because it seemed to continue forever. I think I've fixed this in GCal4Ruby locally by creating the ability to use the allcalendars feed with the HTTPS protocol (i.e: https://www.google.com/calendar/feeds/default/allcalendars/full).
The thing that worries me is that I see no mention of the allcalendars feed needing to specify the HTTPS protocol in the Google documentation. That, and the fact that when I access the same domain using the Zend GData library in PHP, it works fine accessing the non-SSL private feed (i.e. http://www.google.com/calendar/feeds/r-calendar.com_xxxxxxxxxxxxxxxxxxxxxxxxxxx%40group.calendar.google.com/private/full).
So, the question: What am I misunderstanding? Is it just the allcalendars feed that needs to be accessed with SSL and the rest of the private feeds can safely use the authentication token?
Anyone have any insight, or pointers to some good docs?
So, it looks like perhaps redirect to the normal URL is normal for authentication, but that the library is not handling the redirect correctly because of some differences between the way Google Apps and Accounts work on the backend. This is in contrast to the Zend library, which seems to handle this more robustly. That's my current guess, anyway.

Resources