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.
Related
I have created a collection in my Fauna database. I also made an index that returns all the data in that collection. I want to know if there is a way to get JSON REST API to work with this data. What I mean by this is I want a URL that I can 'GET' using XMLHttpRequest and then parse into JSON.
You might say that Fauna is "HTTP native". All of the language drivers (JS, C#, Python, etc.) are built on top of http requests. That is, there are no special connections or connection-pools to maintain. You can get pretty far with using the JS driver in the browser, for example, without using a server.
But to answer your question more directly, there are many ways to serve a REST API, and it is going to depend on how you want to serve the API.
Examples
AWS
Here is a blog post on how to set up a REST API with AWS App Runner, for example.
https://fauna.com/blog/deploying-a-rest-api-with-aws-app-runner-and-fauna
Vercel and Netlify
There are also some example apps on github that demonstrate using a REST API with
Vercel:
https://github.com/vercel/next.js/tree/canary/examples/with-cookie-auth-fauna
Or Netlify:
https://github.com/netlify/netlify-faunadb-example
Both of which host serverless functions for you to create a REST API.
I am trying to obtain oauth tokens in order to use the CAD API, however, I keep getting this error:
Signature on SAML token is INVALID commentary: [Verification against Static Certificate not supported in this scenario.
Since they dont have an NPM or NodeJS I have started to create one, however, I am getting hung up on the SAML assertion request. So far I have ported over this ruby gem for making the saml request to get the new oauth token which is found here:
https://github.com/cloocher/aggcat/blob/master/lib/aggcat/base.rb (line 70) def saml_message...
My first stab at this is not neat, but I'm just trying to verify I can make a SAML request with NodeJS and get a response back containing oauth token.
My source code for this NodeJS script can be found at:
https://github.com/britztopher/intuit-cad/blob/master/server/saml2.js
And the Intuit documentation for making this request can be found here:
Intuits CAD Doc which is lacking any kind of sample of what is needed to produce the assertion message.
If any clarification is needed please comment, as I am just trying to jump this first hurdle.
OK, so after cloning the aggcat repo for the ruby script and modifying the template for the assertion message to use constants instead of variables then comparing the that assertion message to the one my script was generating I finally found the reason why this was failing. To make sure the sha1 digest was the same and the signature was the same I had to make the assertion messages were the same.
The reason for failure was that sample Assertion Sample in the documentation is wrong all over the place:
https://developer.intuit.com/docs/0020_customeraccountdata/009_using_customeraccountdata/0010_gettingstarted/0025_making_your_first_connection/saml_assertion_sample
There are spaces missing and the one thing that got me was this element:
This is not a terminating element and needs a closing tag. For example:
In addition, the xml doc type () is not needed either.
After all this aggravation with intuit's developer documentation, I decided to save everyone the trouble of trying to follow it to confusion, and made an NPM module called intuit-cad, which builds the SAML assertion and performs all the authentication in order for you to make clean API calls easily. Also, this module is built on top of promises, which make error handling and callback hell prevention a breeze. In addition to the library I have a simple blog post about it here: intuit-cad blog post
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.
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.
I'm writing an API wrapper as a gem, and I want to test API responses using RSpec.
The problem with this is that all API requests are made using GET, and contain an API key in the url:
e.g. game/metadata/{api_key}
This presents problems for testing, as I don't want to keep the API key in the git repository history. Is there any way I can do these spec tests, preferably with RSpec/VCR, and not store the API key in version control?
I've tried using environment variables, but VCR still stores the entire request, not just the response body.
VCR has a configuration option specifically for cases like these:
VCR.configure do |c|
c.filter_sensitive_data("<API_KEY>") { MyAPIClient.api_key }
end
See https://www.relishapp.com/myronmarston/vcr/docs/configuration/filter-sensitive-data for a larger example.