Invalid OAuth tokens - ruby-on-rails

I am using rforce gem and oauth2 login system for my SFDC app.
I am trying to login through Oauth2 but I get Invalid OAuth tokens error. Here is my code
access_token = oauth_client.web_server.get_access_token(params[:code], :redirect_uri => oauth_redirect_uri, :grant_type => 'authorization_code')
access_token is an OAuth2::AccessToken object
oauth = {
:consumer_key => '3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3vxxxxxxxxxxxxxxxN',
:consumer_secret => '7xxxxxxxxxxx291xxxxxxx1',
:access_token => access_token.token,
:access_secret => '7xxxxxxxxxxx291xxxxxxx1',
:login_url => 'https://login.salesforce.com/services/OAuth/u/20.0'
}
I think consumer_secret and access_secret are same. If not, then what is access_secret and where could I find it?
I am using oauth2 -v 0.4.1 and rforce -v 0.8.1

What you are using here is Oauth1. In Oauth 2 there is only access_token and refresh_token. There is no access_secret any more.
I think this article will help you:
http://wiki.developerforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com

Related

OAuth token not retrieved with Uber API

I am unable to retrieve the token in the OAuth process using the Uber API.
Here is my code:
require 'oauth2'
<% client = OAuth2::Client.new('<client id>', '<client secret>',
:site => 'http://login.uber.com/oauth/authorize?response_type=code') %>
<% token = client.auth_code.get_token('authorization_code_value',
:redirect_uri => 'http://localhost:4567/callback',
:headers => {'Authorization' => "Token <token>"}) %>
Getting:
OAuth2::Error - invalid_client: {"error": "invalid_client"}:
/Library/Ruby/Gems/2.0.0/gems/oauth2-1.0.0/lib/oauth2/client.rb:113:in
`request'
any idea ? I tried http/https , and code/token on the response type.
thanks,
Matt
Apparently you had the wrong client id, check if it matches the one on your developer dashboard.

Ruby and Twitter: Getting Access Token from Request Token?

I am currently in Step 3 of the processing on getting an oauth token/secret from an user trying to login via Twitter. https://dev.twitter.com/docs/auth/implementing-sign-twitter
Step 3 tells me to send this request to the API, but I am stuck as to how to do so. I currently have BOTH the oauth_token and oauth_verifier, but how do I send this POST request to get the oauth_token, oauth_token_secret pair?
Is there a standard Oauth Ruby gem I can use to send this POST request? I see examples online where I pass an #accessToken object, but i do not have such an object available. I just have the oauth_token and oauth_verifier (as strings). Given these 2 things, how do I convert them to an oauth_token and oauth_token_secret?
POST /oauth/access_token HTTP/1.1
User-Agent: themattharris' HTTP Client
Host: api.twitter.com
Accept: */*
Authorization: OAuth oauth_consumer_key="cChZNFj6T5R0TigYB9yd1w",
oauth_nonce="a9900fe68e2573b27a37f10fbad6a755",
oauth_signature="39cipBtIOHEEnybAR4sATQTpl2I%3D",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1318467427",
oauth_token="NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0",
oauth_version="1.0"
Content-Length: 57
Content-Type: application/x-www-form-urlencoded
oauth_verifier=uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY
Try something like the following rails controller actions, using the twitter and oauth gems:
def redirect
consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, {
:site => "https://api.twitter.com",
:scheme => :header
})
request_token = consumer.get_request_token(:oauth_callback => CALLBACK_URL)
session[:twitter_request_token] = request_token
redirect_to request_token.authorize_url #=> "https://api.twitter.com/oauth/authorize?oauth_token=XYZ"
end
def callback
request_token = session[:twitter_request_token]
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
client = Twitter::REST::Client.new(
:consumer_key => CONSUMER_KEY,
:consumer_secret => CONSUMER_SECRET,
:access_token => access_token.token,
:access_token_secret => access_token.secret
)
twitter_user = client.user
redirect_to root_url # or do something with the twitter_user
end
See also: http://barkingiguana.com/2009/10/13/twitter-oauth-authentication-using-ruby/
yes there is the Omniauth gem for authentication with Twitter. The documentation is straight forward.
I personally use Omniauth integrated with Devise and the Twitter gem to access Twitter - works very well.
Hope this helps,
Eugen
The common procedure is the following:
You shell to register your app on twitter development page.
Then set the proper Name, Description, and Website values up for your application.
App Name
App Description
http://your_app_domain.zone:3000/
Change Application Type is your app, by default it has read only access type.
Setup the callback URL for yuor application:
http://your_app_domain.zone:3000/auth/twitter/callback
Store the all keys, and secrets that are shewn on the OAuth tool twitter page:
Consumer key:
Consumer secret:
Access token:
Access token secret:
Setup route on your site with devise, or devise-like gem with the specified twitter keys, and secrets to enable authentication engine. The route list now shall include /auth/twitter path.
By going to http://your_app_domain.zone:3000/auth/twitter you will be redirected to twitter site, and dropped back to your site with passed oauth_token
But
You simple receive those keys, and secrets, and apply then in your app, avoiding the 6, and 7 points:
client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end

how to get oauth access token for facebook using ruby

Am trying to get oauth access token for facebook programmatically in ruby.
My code is as follows:
client = OAuth2::Client.new(
APP_ID,
SECRET_ID,
:authorize_url => "/dialog/oauth",
:token_url => "/oauth/access_token",
:site => "https://www.facebook.com/"
)
code = client.auth_code.authorize_url(:redirect_uri => "http://www.facebook.com/")
token = client.auth_code.get_token(code, :redirect_uri => "https://graph.facebook.com/")
OAuth2::AccessToken.new(client, token.token, {:mode => :query, :param_name =>"oauth_token"})
When i try to run the above ruby code, i'm getting the following exception
https://www.facebook.com/dialog/oauth?response_type=code&client_id=APP_ID
51&redirect_uri=http%3A%2F%2Fwww.facebook.com%2F
/home/ec2-user/.rvm/gems/ruby-1.9.3-p0#samples/gems/oauth2-0.5.2/lib/oauth2/clie
nt.rb:129:in `get_token': OAuth2::Error (OAuth2::Error)
from /home/ec2-user/.rvm/gems/ruby-1.9.3-p0#samples/gems/oauth2-0.5.2/li
b/oauth2/strategy/auth_code.rb:29:in `get_token'
from oauth.rb:16:in `<main>'
Any help is greatly appreciated as I have spent more than a day while trying to sort this out.
Have you tried to put as redirect_uri instead of localhost:3000 your real IP address ex. 231.61.233.57:3000? Additionally you could try to use ssh tunneling for testing purposes so your localhost application will be available worldwide. Check this out http://progrium.com/localtunnel/ .
When you will get ip address from this tool try to set redirect_uri param to it.

How to the same thing with omniauth

Reading on this oauth tutorial.
I would like to do exactly the same thing using the Omniauth gem, to get the access tokens from Twitter's API.
#oauth = OAuth::Consumer.new(APP_CONFIG['twitter_consumer_key'], APP_CONFIG['twitter_consumer_secret'], :site => 'http://api.twitter.com', :request_endpoint => 'http://api.twitter.com', :sign_in => true)
# Get the request tokens from the API
rt = oauth.get_request_token
rtoken = rt.token # request token
rsecret = rt.secret # request token secret
# Go to url and click ""Allow access” when prompted
# http://api.twitter.com/oauth/authorize?oauth_token=your_request_token_from_above
# Get the access tokens from the API
at = rt.get_access_token
oauth_token = at.token
oauth_token_secret = at.secret
Anyone know how this can be achieved?
Here it is a good howto, take a look:
http://philsturgeon.co.uk/blog/2010/11/using-omniauth-to-make-twitteroauth-api-requests

How to achieve the same thing for the Facebook API?

Reading on this oauth tutorial.
The code below, shows how this can be achieved to get the access tokens on Twitter's API. I would like to do exactly the same thing for the Facebook API using OAuth.
#oauth = OAuth::Consumer.new(APP_CONFIG['twitter_consumer_key'], APP_CONFIG['twitter_consumer_secret'], :site => 'http://api.twitter.com', :request_endpoint => 'http://api.twitter.com', :sign_in => true)
# Get the request tokens from the API
rt = oauth.get_request_token
rtoken = rt.token # request token
rsecret = rt.secret # request token secret
# Go to url and click ""Allow access” when prompted
# http://api.twitter.com/oauth/authorize?oauth_token=your_request_token_from_above
# Get the access tokens from the API
at = rt.get_access_token
oauth_token = at.token
oauth_token_secret = at.secret
Anyone know how this can be achieved?
Look at "OAuth URLs and tokens" on the Koala docs: https://github.com/arsduo/koala/wiki/OAuth It is a great gem for FB.

Resources