Google Calendar API 'start-min' parameter not doing anything - ruby-on-rails

This is my code:
oauth_yaml = YAML.load_file('google-api.yaml')
client = Google::APIClient.new
client.authorization.client_id = oauth_yaml["client_id"]
client.authorization.client_secret = oauth_yaml["client_secret"]
client.authorization.scope = oauth_yaml["scope"]
client.authorization.refresh_token = #rtoken
client.authorization.access_token = #token
if client.authorization.refresh_token && client.authorization.expired?
client.authorization.fetch_access_token!
end
service = client.discovered_api('calendar', 'v3')
result = client.execute(
:api_method => service.events.list,
:parameters => {'calendarId' => #calendar, 'start-min' => Time.now.xmlschema},
:headers => {'Content-Type' => 'application/json'}
)
#parsed = JSON.parse(result.data.to_json)
I'm getting all events regardless of if the 'start-min' parameter is included or not. Am I doing something wrong here?
Thanks.

The correct parameter is actually timeMin, not start-min.

Related

quickbooks integration in rails application (parameter rejected OAuth)

I am creating a rails application to post invoices to quickbooks I am using this gem quickbooks-ruby every time when I try to authenticate with quickbooks It gives this error OAuth::Problem parameter_rejected below is my code.
quickbook.rb (initialiser)
::QB_OAUTH_CONSUMER = OAuth::Consumer.new(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, {
:site => "https://oauth.intuit.com",
:request_token_path => "/oauth/v1/get_request_token",
:authorize_url => "https://appcenter.intuit.com/Connect/Begin",
:access_token_path => "/oauth/v1/get_access_token"
})
Authenticate method in controller
def authenticate_quickbooks
callback = api_webhooks_quickbook_oauth_callback_path
token = QB_OAUTH_CONSUMER.get_request_token(:oauth_callback => callback)
session[:qb_request_token] = token
redirect_to("https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token.token}") and return
end
callback
def quickbooks_oauth_callback
at = session[:qb_request_token].get_access_token(:oauth_verifier => params[:oauth_verifier])
token = at.token
secret = at.secret
realm_id = params['realmId']
end
Problem solved. My quickbooks app was using oAuth2 and the gem has different method of authentication for oAuth1 and oAuth2.
below is the code (for OAuth2):
quickbook.rb (initialiser)
oauth_params = {
:site => "https://appcenter.intuit.com/connect/oauth2",
:authorize_url => "https://appcenter.intuit.com/connect/oauth2",
:token_url => "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"
}
::QB_OAUTH2_CONSUMER = OAuth2::Client.new(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, oauth_params)
Quickbooks.sandbox_mode = true
Authenticate method in controller
redirect_uri = "http://localhost:3000#{quickbooks_authenticate_callback_path}"
grant_url = ::QB_OAUTH2_CONSUMER.auth_code.authorize_url(:redirect_uri => redirect_uri, :response_type => "code", :state => SecureRandom.hex(12), :scope => "com.intuit.quickbooks.accounting")
redirect_to grant_url
callback
redirect_uri = oauth_callback_quickbooks_url
if resp = ::QB_OAUTH2_CONSUMER.auth_code.get_token(params[:code], :redirect_uri => redirect_uri)
where oauth_callback_quickbooks_url = application callback which is also defined in quickbooks app
please note that you also have to include oauth2 gem

calling ruby method in rails controller raises error

I have a rails app and I'm trying to call the methods below from the controller, but I get this error:
undefined local variable or method "service" for #<EventsController:0x007fb6d27da1c8>` for this line: `api_method: service.freebusy.query
What's the problem here? Why can't the get_busy_events see the service var if it's defined above it?
controller
include GoogleCalendarApi
.....
#user = current_user
#google = #user.socials.where(provider: "google_oauth2").first
unless #google.blank?
#client = init_google_api_calendar_client(#google)
#result = open_gcal_connection(get_busy_events, #client, #google)
lib/google_api_calendar.rb
def init_google_api_calendar_client(google_account)
#method only called if google_oauth2 social exists
client = Google::APIClient.new
client.authorization.access_token = google_account.token
client.authorization.client_id = ENV['GOOGLE_API_KEY']
client.authorization.client_secret = ENV['GOOGLE_API_SECRET']
client.authorization.refresh_token = google_account.refresh_token
return client
end
def open_gcal_connection(options, initialized_client, social_object)
client = initialized_client
old_token = client.authorization.access_token
service = client.discovered_api('calendar', 'v3')
result = client.execute(options) #after execution you may get new token
# update token if the token that was sent back is expired
new_token = client.authorization.access_token
if old_token != new_token
social_object.update_attribute(token: new_token)
end
return result
end
def get_busy_events
result = open_gcal_connection(
api_method: service.freebusy.query,
body: JSON.dump({ timeMin: '2015-12-24T17:06:02.000Z',
timeMax: '2013-12-31T17:06:02.000Z',
items: social_object.email }),
headers: {'Content-Type' => 'application/json'})
#handling results
end
To answer your question(as I did in the comments):
To fix your method, you have to define the service variable in the action where you are calling it.
As for your posted link: if you look at the get_busy_events method there is a line where service = client.discovered_api('calendar', 'v3')
and it is fine, because it is in the method. The same goes for client that the service declaration depends on- you have to declare them inside the method where you use them.
You should follow the article and make the code as it is there so you would have:
def init_client
client = Google::APIClient.new
# Fill client with all needed data
client.authorization.access_token = #token #token is taken from auth table
client.authorization.client_id = #oauth2_key
client.authorization.client_secret = #oauth2_secret
client.authorization.refresh_token = #refresh_token
return client
end
which you can use to define client variable in all your other actions and then use the service method:
def get_busy_times
client = init_client
service = client.discovered_api('calendar', 'v3')
#result = client.execute(
:api_method => service.freebusy.query,
:body_object => { :timeMin => start_time, #example: DateTime.now - 1.month
:timeMax => end_time, #example: DateTime.now + 1.month
:items => items
},
:headers => {'Content-Type' => 'application/json'}})
end
EDIT No2:
Since you have a controller, where client is initialized I suggest passing it down as an argument:
include GoogleCalendarApi
.....
#user = current_user
#google = #user.socials.where(provider: "google_oauth2").first
unless #google.blank?
#client = init_google_api_calendar_client(#google)
#result = open_gcal_connection(get_busy_events(#client), #client, #google)
and changing your get_busy_events method:
def get_busy_events(client)
service = client.discovered_api('calendar', 'v3')
result = open_gcal_connection(
api_method: service.freebusy.query,
body: JSON.dump({ timeMin: '2015-12-24T17:06:02.000Z',
timeMax: '2013-12-31T17:06:02.000Z',
items: social_object.email }),
headers: {'Content-Type' => 'application/json'})
#handling results
end
Although this is a bit weird for me(nesting arguments like this) so you should look at refactoring this.

Yahoo OAuth 1.0 Callback Issue?

I want to use Yahoo Fantasy sport API in my web application, For that I am using OAuth for Yahoo login. I have consumer key and secret key and i passed the keys successfully, When I run the following code. It redirects to Yahoo login, It asks permission for accessing the user's credentials. If i give AGREE the page Redirects to https://api.login.yahoo.com/oauth/v2/request_auth and It shows the Verifying code. If i press the close button in verification code page, it's not callback to my URL.
#ts=Time.now.to_i
#callback_url = "http://localhost:3000/callback"
#nonce = SecureRandom.hex()
consumer = OAuth::Consumer.new("my consumerkey","secret key",
{ :site => 'https://api.login.yahoo.com',
:http_method => :post,
:scheme => :header,
:oauth_nonce => #nonce,
:request_token_path => '/oauth/v2/get_request_token',
:authorize_path => '/oauth/v2/request_auth',
:access_token_path => '/oauth/v2/get_token',
:oauth_callback => "http://localhost:3000/callback",
:oauth_timestamp => Time.now.to_i,
:oauth_signature_method => "HMAC-SHA-1",
:oauth_version => "1.0",
:oauth_callback_confirmed => true,
})
request_token = consumer.get_request_token
session[:request_token]=request_token
redirect_to request_token.authorize_url
access_token=request_token.get_access_token
access = ActiveSupport::JSON.decode(access_token.to_json)
if !(access.present?)
#response = "Response failed"
else
#response = access
end
Can you please tell me What changes to be made to get the callback for to get access_token.
I think you got confused while getting callback. change your code as follows, You will surely get access token to make Yahoo API call.
##access_token = nil
##request_token = nil
def get_request_token
##consumer = OAuth::Consumer.new('consumer key',
'secret key',
{
:site => 'https://api.login.yahoo.com',
:scheme => :query_string,
:http_method => :get,
:request_token_path => '/oauth/v2/get_request_token',
:access_token_path => '/oauth/v2/get_token',
:authorize_path => '/oauth/v2/request_auth'
})
##request_token = ##consumer.get_request_token( { :oauth_callback => 'http://localhost:3000/callback' } )
session[:request_token]=##request_token
redirect_to ##request_token.authorize_url
#redirect_to ##request_token.authorize_url( { :oauth_callback => 'http://localhost:3000/success' } )
end
def callback
request_token = ActiveSupport::JSON.decode(##request_token.to_json)
if !(request_token.present?)
$request_token_value = "Response failed"
else
$request_token_value = request_token
end
# access_token = ##request_token.get_access_token({:oauth_verifier=>params[:oauth_verifier],:oauth_token=>params[:oauth_token]})
##access_token = ##request_token.get_access_token(:oauth_verifier=>params[:oauth_verifier])
access_json = ActiveSupport::JSON.decode(##access_token.to_json)
puts "****************************"
puts $access_json
puts "****************************"
end

Rails - OAuth::Problem Exception: parameter_absent in LinkedIn API

I am Develoing a Linked in APi using Oauth gem. The same gem is working perfectly for my Twitter API, But in the case of Linkedin API, I am getting the request token, and when it tries to get the access token it return the error
OAuth::Problem Exception: parameter_absent
Code is as follows..
consumerKey = "*************************"
consumerSecret = "*************************"
callbackUrl = "http://localhost/"
apiURL = "https://api.linkedin.com"
request_token_path = "/uas/oauth/requestToken"
access_token_path = "/uas/oauth/accessToken"
authorize_path = "/uas/oauth/authorize"
#consumer = OAuth::Consumer.new(consumerKey,consumerSecret, {
:site => apiURL,
:scheme => :header,
:http_method => :post,
:request_token_path => request_token_path,
:access_token_path => access_token_path,
:authorize_path => authorize_path
})
unless(params["oauth_token"].nil?)
#request_token = session[:request_token]
#access_token = #request_token.get_access_token
else
#request_token = #consumer.get_request_token(:oauth_callback => callbackUrl)
firstUrl = #request_token.authorize_url(:oauth_callback => callbackUrl)
session[:request_token] = #request_token
redirect_to firstUrl
end
Thank you..
Finally i did it, ":oauth_verifier => params[:oauth_verifier]" this was the problem :)
unless(params["oauth_token"].nil?)
#request_token = session[:request_token]
#access_token = #request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
else
#request_token = #consumer.get_request_token(:oauth_callback => callbackUrl)
firstUrl = #request_token.authorize_url(:oauth_callback => callbackUrl)
session[:request_token] = #request_token
redirect_to firstUrl
end
I got the same problem, you can fix it by using https://github.com/decioferreira/omniauth-linkedin-oauth2

401 Unauthorized for foursquare OAuth

OK, I am pulling my hair after trying this out too many times to debug.
So please help me out here. I keep getting 401 Unauthorized error after I am redirected back.
Here is my code. What am I doing wrong here?
require 'rubygems'
require 'OAuth'
require 'json'
class SessionController < ApplicationController
before_filter :load_oauth
def index
if session[:request_token] && params[:oauth_token]
#request_token = OAuth::RequestToken.new(#consumer,
session[:request_token], session[:request_secret])
#access_token =
#request_token.get_access_token(:oauth_verifier =>
params[:oauth_verifier])
puts #access_token
#info = #access_token.get("http://api.foursquare.com/v1/
test")
flash[:notice] = "Foursquare! Yay!"
else
redirect_to(#foursqrurl)
end
end
private
def load_oauth
#oauth_key = 'key'
#oauth_secret = 'secret'
#consumer = OAuth::Consumer.new(#oauth_key,#oauth_secret,{
:site => "http://foursquare.com",
:scheme => :header,
:http_method => :post,
:request_token_path => "/oauth/request_token",
:access_token_path => "/oauth/access_token",
:authorize_path => "/oauth/authorize"
})
#request_token = #consumer.get_request_token(:oauth_callback =>
"http://localhost:3001/session")
session[:request_token] = #request_token.token
session[:request_secret] = #request_token.secret
puts #request_token.token
puts #request_token.secret
# redirecting user to foursquare to authorize
#foursqrurl = #request_token.authorize_url
puts #foursqrurl
end
end
I know absolutely nothing about Oauth and this may be completely wrong, but, if http://foursquare.com is not your local machine and oauth_callback is a URL that http://foursquare.com will call when it has completed then it will be calling back to itself as its definition of localhost will be its own 127.0.0.1 i.e itself.
If I have guessed correctly here then change the :oauth_callback to your public IP Address/name.
I think #request_token = OAuth::RequestToken.new(#consumer,
session[:request_token], session[:request_secret]) is wrong.
If you already have the token and the secret, you don't really need to do the verifier thing.
You should construct it like this:
OAuth::RequestToken.from_hash(consumer, { :oauth_token => params[:oauth_token] })
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
Or if you already have the token and the secret, you should do:
access_token = OAuth::AccessToken.from_hash(consumer, {
:oauth_token => "YOUR_TOKEN",
:oauth_token_secret => "YOUR_SECRET"
})

Resources