How to handle user revoking LinkedIn access in rails - ruby-on-rails

How should I handle a user revoking my access to their LinkedIn account? I am using Ruby on Rails with Omniauth, Omniauth-LinkedIn and linkedin gem (latter incorporates oauth gem).
My controller code
if !#user[:lnk_token].nil?
client = LinkedIn::Client.new("xxxx","xxxx")
client.authorize_from_access(#user[:lnk_token].strip,#user[:lnk_access_token_secret].strip)
#lnk = client.profile(:fields => ["picture-url", "positions", "educations"])
end

Use a simple error handler:
if !#user[:lnk_token].nil?
client = LinkedIn::Client.new("xxxx","xxxx")
client.authorize_from_access(#user[:lnk_token].strip,#user[:lnk_access_token_secret].strip)
begin
#lnk = client.profile(:fields => ["picture-url", "positions", "educations"])
rescue
#lnk = nil
end
end
Further reading: http://rubylearning.com/satishtalim/ruby_exceptions.html

Related

Undefined method `escape' for URI:Module

I'm a noob studying Ruby on Rails. I am trying to integrate my ruby on rails app to Quickbooks. I'm following the steps of this video. And I am stuck when I try to authenticate by pressing the "Connect to QuickBooks" button.
I get this error;
I did a little research and found out that this error was about the URI.escape() command. I think they removed it in Ruby 3. How can I resolve this issue? I can't even figure out where this URI.escape() command is located.
This is from vendors_controller.rb
def authenticate
callback = oauth_callback_vendors_url
token = $qb_oauth_consumer.get_request_token(:oauth_callback => callback)
session[:qb_request_token] = token
# If Rails >= 4.1 you need to do this => session[:qb_request_token] = Marshal.dump(token)
redirect_to("https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token.token}") and return
end
def oauth_callback
at = session[:qb_request_token].get_access_token(:oauth_verifier => params[:oauth_verifier])
# If Rails >= 4.1 you need to do this => at = Marshal.load(session[:qb_request_token]).get_access_token(:oauth_verifier => params[:oauth_verifier])
session[:token] = at.token
session[:secret] = at.secret
session[:realm_id] = params['realmId']
redirect_to root_url, notice: "Your QuickBooks account has been successfully linked."
end
My initializer quickbooks.rb
QB_KEY = "I PASTED MY CLIENT ID KEY HERE"
QB_SECRET = "I PASTED MY CLIENT SECRET HERE"
$qb_oauth_consumer = OAuth::Consumer.new(QB_KEY, QB_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"
})
My rails version Rails 6.1.4
My ruby version ruby 3.0.1p64
Thank you for your time.
I know many people say that URI.escape wasn't good, but it was good enough for me when all I wanted from it is to escape urls with non-English characters.
So I just monkey patched it
Add the following under /config/initializers/functions_overrides.rb
(the file name can be anything you want)
require 'uri'
module URI
class << self
def escape(str)
alpha = "a-zA-Z"
alnum = "#{alpha}\\d"
unreserved = "\\-_.!~*'()#{alnum}"
reserved = ";/?:#&=+$,\\[\\]"
unsafe = Regexp.new("[^#{unreserved}#{reserved}]")
str.gsub(unsafe) do
us = $&
tmp = ''
us.each_byte do |uc|
tmp << sprintf('%%%02X', uc)
end
tmp
end.force_encoding(Encoding::US_ASCII)
end
end
end
Use the https://developer.intuit.com/app/developer/qbo/docs/develop/sdks-and-samples-collections/ruby
OAuth Ruby Client
Intuit offers an OAuth 2.0 Client which provides a set of methods that make it easier to work with Intuit’s OAuth and OpenID implementation.
Community Supported Ruby SDK
The Community Supported Ruby SDK makes it easy to integrate your web app with the QuickBooks Online API. This guide assumes that you have an existing web app that you want to integrate with QuickBooks Online.
URI#escape was deprecated and later removed - https://github.com/ruby/uri/commit/61c6a47ebf1f2726b60a2bbd70964d64e14b1f98
From the commit :
# This method is obsolete and should not be used. Instead, use
# CGI.escape, URI.encode_www_form or URI.encode_www_form_component
# depending on your specific use case.
Ruby 3.0.0 onwards URI#escape does not work.
Use CGI.escape or URI.encode_www_form as mentioned in the deprecation warning

stripe callback redirecting http instead https uri

I'm using stripe connect on my website, and when a user in production tries to connect his stripe account to my web site, I have the following error in the stripe callback :
{
"error": "invalid_redirect_uri",
"error_description": "Invalid redirect URI 'http://www.mywebsite.com/stripe_connections/callback'. Ensure this uri exactly matches one of the uris specified in your application settings",
"state": "4 »
}
whereas my redirecti URIS in my stripe application setting is https://www.mywebsite.com/stripe_connections/callback
here is my controller :
require 'oauth2'
class StripeConnectionsController < ApplicationController
skip_after_action :verify_authorized
def new
stripe_auth_url = "https://connect.stripe.com/oauth"
client = OAuth2::Client.new(ENV['STRIPE_CONNECT_CLIENT_ID'], ENV['STRIPE_SECRET_KEY'], :site => stripe_auth_url)
#stripe_url = client.auth_code.authorize_url(:redirect_uri => "#{request.protocol}#{request.host_with_port}/stripe_connections/callback", :scope => 'read_write', state: params[:brief_id])
end
def callback
#brief = Brief.find(params[:state])
stripe_auth_url = "https://connect.stripe.com/oauth"
#user = current_user
client = OAuth2::Client.new(ENV['STRIPE_CONNECT_CLIENT_ID'], ENV['STRIPE_SECRET_KEY'], :site => stripe_auth_url)
access_token = client.auth_code.get_token(params[:code], :redirect_uri => '#{request.protocol}#{request.host_with_port}/oauth2/callback')
stripe_connection = StripeConnection.find_or_create_by(user_id: #user.id)
stripe_connection.update_attributes(access_token: access_token.token,
refresh_token: access_token.refresh_token,
livemode: access_token.params['livemode'],
stripe_user_id: access_token.params['stripe_user_id'],
publishable_key: access_token.params['stripe_publishable_key']
)
#user.profile.projects.where(state: 'pending').update_all(state: "on_sale")
end
end
I'm using heroku and paying the SSL add-ons already.
I don't know why stripe is returning http instead of https. Does anyone have an idea? thx.
ps: this has already worked before in production and works in the beta version of the website
Do you have a button that the user clicks to connect to stripe? I just removed the redirect_uri parameter.
You have to remove the redirect_uri of client.auth_code.authorize_url() in the new method and also the redirect_uri in the callback method and put the right protocol in the dashboard stripe.

salesforce sandbox integration in rails, error expired access

I am using the Ruby Gem "databascdotcom" to integrate Salesforce in a Rails app and all works fine until i try it with sandbox account type "Configuration Only".
The following code work fine when i used with salesforce product account.
Here is my code
def SalesForceFeed
#oppID = params[:oppid]
client = Databasedotcom::Client.new client.client_id #=> foo client.client_secret #=> bar
client.authenticate :username => "foo#bar.com", :password => "ThePasswordTheSecurityToken" #=> "the-oauth-token"
client.materialize("Opportunity")
begin
#client=SalesForce::Connection.new.client
#opp = Opportunity.find_by_Id(#oppID)
rescue Exception=>e
end
But when i try to use it with salesforce sandbox account with username like "foo#bar.com.sandbox"
I m getting following error "expired access/refresh token"
Any ideas?
Got the issue.
Just need to add
host= "test.salesforce.com"
Thanks All

"Missing authorization code" error when trying to use Google Calendar API

The following is the code I am using to allow users to allow users to authorise my app to access their Google Calendar via OAuth. I based it off this sample code.
It works most of the time, but sometimes, there is an ArgumentError: Missing authorization code error on the client.authorization.fetch_access_token! line in the create_google_calendar action in the services controller. If I comment out that line, all of the client.authorization attributes are null.
I am using Rails 3.2.0 and Ruby 1.9.2.
What is causing this?
Gemfile
gem 'google-api-client', :require => 'google/api_client'
service.rb
def self.google_calendar_client google_calendar_service=nil
client = Google::APIClient.new
client.authorization.client_id = xxx
client.authorization.client_secret = xxx
client.authorization.scope = 'https://www.googleapis.com/auth/calendar'
url_prefix = Rails.env.production? ? xxx : 'http://localhost:3000'
client.authorization.redirect_uri = "#{url_prefix}/create_google_calendar"
if google_calendar_service.present?
client.authorization.update_token! :access_token => google_calendar_service.token, :refresh_token => google_calendar_service.google_calendar_refresh_token, :expires_in => google_calendar_service.google_calendar_expires_in, :issued_at => Time.at(google_calendar_service.google_calendar_issued_at)
client.authorization.fetch_access_token! if client.authorization.expired?
end
client
end
services_controller.rb
def connect_google_calendar
#google_calendar_url = Service.google_calendar_client.authorization.authorization_uri.to_s
end
def create_google_calendar
client = Service.google_calendar_client
client.authorization.code = params[:code]
client.authorization.fetch_access_token!
current_user.services.create :provider => 'google_calendar', :token => client.authorization.access_token, :google_calendar_refresh_token => client.authorization.refresh_token, :google_calendar_expires_in => client.authorization.expires_in, :google_calendar_issued_at => client.authorization.issued_at
end
The truth is, I don't know. Your code looks right to me. But I can at least tell you what the error means. Missing authorization code means that it thinks you're trying to do an "authorization code" grant type when you fetch the access token. If you're actually trying to obtain an access token off a refresh token as opposed to doing it on the first pass after obtaining authorization from the user, then you may not have correctly set up the authorization object.
You can check this by inspecting the client.authorization.grant_type value. In very recent versions of the client you can manually set the grant_type value to force a particular mode, which may give you more informative error messages, depending on what the actual issue is.

How do I authenticate users in a Rails app with the oauth gem and twitter 1.0.0 gem?

Jnunemaker just updated his twitter gem (https://github.com/jnunemaker/twitter) and removed the Twitter::Oauth class. My code doesn't look much like his example, so I'm having issues updating it. Here's what my code used to look with the twitter 0.9 gem:
UsersController
def oauth
consumer = Twitter::OAuth.new('mykey','mysecret')
request_token = consumer.request_token
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
redirect_to 'http://api.twitter.com/oauth/authorize?oauth_token='+request_token.token
end
def callback
consumer = Twitter::OAuth.new('mykey','mysecret')
atoken, asecret = oauth.authorize_from_request(session[:request_token], session[:request_token_secret], params[:oauth_verifier])
consumer.authorize_from_access(atoken,asecret)
user = Twitter::Base.new(consumer).verify_credentials
#and then I create a new user in my application, with attributes such as the user's follower count, etc
end
Here's an example of what I've tried to do to change this code:
UsersController
def oauth
consumer = OAuth::Consumer.new("mykey", "mysecret", :site => "siteurl")
request_token = consumer.get_request_token
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
redirect_to 'http://api.twitter.com/oauth/authorize?oauth_token='+request_token.token
end
def callback
consumer = OAuth::Consumer.new("mykey", "mysecret", :site => "siteurl")
request_token = session[:request_token]
atoken = OAuth::RequestToken.new(consumer, request_token.token, request_token.secret).get_access_token(:oauth_verifier => params[:oauth_verifier])
consumer.authorize_from_access(atoken)
user = Twitter::Client.new(consumer).verify_credentials
Gemfile
...
gem 'oauth'
I'm sure there are a number of things wrong in my callback method, but one thing that's weird is that my oauth method works fine when I'm running locally, but gives me a '502 Bad Gateway' error when I try from my live (deployed with heroku) version.
If you can't get it to work with what you have now, I have been able to use the Omniauth gem together with the Twitter gem. Omniauth is very easy to setup.
To use the Twitter gem, just get the access token info after the Omniauth callback is done:
token = omniauth['credentials']['token'],
secret = omniauth['credentials']['secret']
Then just set the Twitter gem config settings before using the Twitter gem methods
Twitter.oauth_token = token
Twitter.oauth_token_secret = secret
Twitter.home_timeline.first.text
(You'll have to configure the Twitter gem consumer_key and consumer_key_secret if you don't haven't that already set up in an initializer file...)
I've had good luck with
Authlogic + AuthLogic Connect.
I'm not sure if you need to implement the oauth by hand, but the gem might be worth looking into.
https://github.com/viatropos/authlogic-connect
The only gotcha I've found with oauth providers is sometimes they provide poor error messages if the callback url isn't recognized, which is configured where you get the api keys.
-Ken
You were close in your example. The right code for your controller action would be something like this:
def new
consumer = OAuth::Consumer.new(YOUR_CONSUMER_TOKEN, YOUR_CONSUMER_SECRET, site: 'https://api.twitter.com', request_endpoint: 'https://api.twitter.com', authorize_path: '/oauth/authenticate')
unless params[:oauth_token]
request_token = consumer.get_request_token({ oauth_callback: request.original_url })
session[:request_token] = { token: request_token.token, secret: request_token.secret}
redirect_to request_token.authorize_url(force_login: 'true')
else
request_token = OAuth::RequestToken.from_hash(consumer, oauth_token: session[:request_token]["token"], oauth_token_secret: session[:request_token]["secret"])
access_token = request_token.get_access_token(oauth_verifier: params[:oauth_verifier])
session[:request_token] = nil
#client = Twitter::REST::Client.new do |config|
config.consumer_key = YOUR_CONSUMER_TOKEN
config.consumer_secret = YOUR_CONSUMER_SECRET
config.access_token = access_token.token
config.access_token_secret = access_token.secret
end
end
end

Resources