Ruby on Rails Google Api Authentication - ruby-on-rails

So, I'm very new to this. I got a generated json file from my google developer console that holds information like private keys, client id, token stuff, etc.
Now, I'm trying to use the Google Analytics Report V4 api. I put all my code into a concern, and when I run the code I get this error:
Google::Apis::AuthorizationError: Unauthorized
So I know that I have to authorize my app, but I'm not sure how. I have this json file which appears to have all the information I need to authenticate my app.
After some research, I know that (on the following code) I need to assign analytics.authorization to something, I just don't know to what.
analytics = Google::Apis::AnalyticsreportingV4::AnalyticsReportingService.new
analytics.authorization = ???
Do you know of any method I'm supposed to call that takes in the location of my json file as a parameter or something that can in turn, authorize my rails app?
Thank you so so much if you can help.
I know there are other questions like this. But they use omniauth with devise I think, and I can't do that. I already have a specific context in which users need to be logged in to my app, so logging in with google wouldn't work in my case. Also, other question/answers that don't involve omniauth and devise are outdated or don't have an accepted answer.

Related

You must set ShopifyAPI::Base.api_version before making a request

I've received the above error when following the guides from Shopify to create a Rails app. None of the other solutions worked and I followed the Shopify guide to the tee.
Downloading the Shopify Rails app from github, I saw that they had this in their shop model:
def api_version
ShopifyApp.configuration.api_version
end
Simply put, you made an API call without opening up a valid session. A session opens when you provide a shop domain, its API token, and the version of the API you expect to use.
If you fail to open a session, and make a call, you get that message. So inspect your code, and ensure you have a session. The convenience method is used all over the place, first get a shop instance, then use the method with_shopify_session. Your problem will then go away. You can also hot-rod this for NON typical use cases, by making a similar method with_api_session that mimics the Shopify call, allowing you to function with your own codes, where perhaps you are not directly installed an App but still have API keys. Think private Apps.

How do I authenticate using Devise in a Rails REST API App?

I'm creating a Rails app which have both a GUI part, and a REST/JSON-API.
The REST/JSON API is fairly simple, and the controller returns data like this:
def get_players
#players = Player.all
render json: #players
end
The GUI part of the app is using Devise for authentication, and it works fine.
Now I want to add authentication for the REST/JSON Api too, how do I do that?
Also, how do I test the REST API using curl when the Authentication is added?
---- edit ----
as it turns out, Devise wasnt necessary in this case. A home-cooked token-authentication method works for now. (token created when Player is created, and returned on correct e-mail/password combo).
After getting a few tips, I found a number of great sites. There are several ways to do this, however I don't know which one is best, but these sites help a long way:
https://github.com/lynndylanhurley/devise_token_auth (An extension to
Devise)
https://labs.kollegorna.se/blog/2015/04/build-an-api-now/
(Manual way)
Token based authentication for Rails JSON APIs (SO Question)
Rails API : Best way to implement authentication? (SO Question)
Rails API : Best way to implement authentication?

Google Oauth2 api - Omniauth Rails

Currently I'm recovering the omniauth information perfectly fine using the typical setup for omniauth gems.
I'm retrieving the access_token as a string and storing it into an Authorization model that is associated to a main Users model.
So, getting to my problem.
I need to access the calenders api and in the docs I see many references like this...
https://www.googleapis.com/calendar/v3//users/me/calendarList/calendarId
This seems fairly intuitive. I want to access this in conjunction with the access token that I have retrieved. Something along the lines of this...
https://www.googleapis.com/calendar/v3//users/me/calendarList/calendarId?access_token=blah
so that I can load it directly and more streamlined into Backbone models. However, I don't see anything in the docs and when I try the implementation, it consistently throws me 404s of "Not found" (which is rather annoying and undescriptive. I'd prefer if it threw 422s but I suppose that's a discussion for somewhere else.)
Is there anything in the docs that provide an example implementation of how to access the info with a token?
thanks
I did a bit more research and found out about the google ruby client gem that was released. Basically, we have to feed the token into the Google::Client instance and pump out an object that has a defined set of methods that can be used on it.
I created a special case controller to handle this logic to render out a suitable api.
TO-DO: Will get into specifics in a bit. Currently I'm very tired.

Building an api as a service

I am building an api for others to use. This is a simple enough Json request the user passes as some data and we pass some back.
What I would love is to secure our api and have some sort of user system where we can turn users on and off and we can log how many requests each user makes.
What would be the best way to do this in Rails? I don't want it to slow down the request. I can see ways of doing it using devise maybe but would be great to hear other people's opinions.
Thanks
Another way is to use 3scale (http://www.3scale.net) - it's free up to a traffic cap but handles all the key management, users, documentation etc. and there's a ruby library which you can drop into your code if you're using rails. (other libs are here: https://support.3scale.net/libraries).
I've done this before using the Token Authentication capabilities of devise (see https://github.com/plataformatec/devise ).
I found the following setup works:
Create a user account for each api user.
Configure devise for token authentication
Set the Token Authentication configuration to require the token to be submitted with each request.
This will allow you to enable and disable individual users as well as to track every request back to the api user that made the call.
If you're really interested in tracking usage you may want to consider also creating a database table where you track all api requests. This can be setup to belong_to the users table so that you easily find all requests from different users (e.g., #user.api_requests).
The count of all requests made by a user would be:
#user.api_requests.count
# or use a where clause to find how many of each type
#user.api_requests.where("api_request_type = ?", 'SomeAPICallType').count
One final note -- I recently used the Grape library for building out an API. I thought it was pretty well done and it worked great for our needs. I especially like the ability it provided to version APIs. Details are here: https://github.com/intridea/grape/wiki

How do you authenticate user generated "apps" for your app?

I'm think something like Facebook apps here. User generated pieces of code that people can write to interact with my app.
I understand how an authenticated API works, but this seems a little more complicated because not only does the APP have to authenticate itself (with a regular api-key) but the USER using the app has to be authenticated somehow too, without giving the app free reign.
I've been reading a bit here to see how FB does it: http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application
And it looks like you have to pass a signature in addition to the api-key along with every call, but I'm having trouble wrapping my head around how this gets generated and used on the other end (my server).
Figure there must be a simple explanation of this out there? Thanks!
P.S. I'm building a Rails app if there are any applicable gems/plugins.
This may be what I need:
http://github.com/phurni/authlogic_api
Did you have any success with authlogic_api? I'm working on the server-side for a Steam game, where users are logged in through Steam, so I'm only responding to REST calls from the client (no user login required). The rdocs for authlogic_api give some brief set-up info, but I'm struggling with what to do in the application_controller to restrict access; essentially the equivalent of this code from the authlogic example:
http://github.com/binarylogic/authlogic_example/blob/5819a13477797d758cb6871f475ed1c54bf8a3a7/app/controllers/application_controller.rb

Resources