LinkedIn Gem Ruby: OAuth (permission_unknown) - ruby

I am trying to use the LinkedIn gem to access LinkedIn. I can't seem to get past getting access.
My code is this:
#client = LinkedIn::Client.new(API_KEY, SECRET)
#rtoken = client.request_token.token
#rsecret = client.request_token.secret
puts "token: #{#rtoken} secret #{#rsecret}"
#authorize_url = client.request_token.authorize_url
puts "authorize url: #{#authorize_url}"
#pin = #authorize_url.split("oauth_token=").last.strip
puts "pin #{#pin}"
#keys = #client.authorize_from_request(#rtoken, #rsecret, #pin)
#client.authorize_from_access(#keys)
And that produces the error:
token: sdklghsdgksdghskdhg secret shdlgkshdgshsdk
authorize url: https://www.linkedin.com/uas/oauth/authorize?oauth_token=sdkghskldghsdkg
pin fslkdghskdghdsgkhsdkhg
OAuth::Problem: permission_unknown
Not really sure where to start on this. I've tried to find what permission is missing, but I'm at a loss.
Thoughts?

I've just started playing with this myself. I hit this same issue unless I manually paste the authorize URL into my browser, and then click Accept to allowing the requested permissions to my account. Then a pin is printed on the screen which I enter in for the authorize_from_request call.
So this line in your code is incorrect - the last part of the URL is a generated_token as the README indicates, not a pin. I'm not sure of a programmatic way to do this.
#pin = #authorize_url.split("oauth_token=").last.strip

I discovered the issue. It turns out request_token.authorize_url does not return the pin. It just gives you a URL to visit where you can authenticate and generate a pin. Then you'll have to copy the pin into your application. A more elegant solution is this:
puts request_token.authorize_url
puts "Access the URL above. Authenticate. Enter the PIN here:"
pin = gets.strip
access_keys = client.authorize_from_request(rtoken, rsecret, pin)
This prints the URL and directs the user to authenticate, then enter the pin into the application.

Related

validating only itunes app store and play store url in rails

I am having a simple problem that is to validating url using ruby on rails technology.There are alot of solution for validating url but none works for me.As i want to allow only play store and app store url to save in database.
Say i have a play store app link.
here is my model:
class AppStore < ActiveRecord::Base
validate :custom
private
def custom
if self.url_name.match /paly.google.com|itunes.apple.com/
else
self.errors.add(:url_name, 'must be app store url or paly store url')
end
end
end
Now the is when i enter url like "http://play.google.com" also the full url of the app then the validation is failling.
A sample url would be like https://play.google.com/store/apps/details?id=com.viber.voip
Please help me to solve this problem as.I am new to rails technology.
Thank you guys.
Please try the following:
class AppStore < ActiveRecord::Base
VALID_STORES = ['play.google.com', 'itunes.apple.com']
validate :store_url_format
private
def store_url_format
unless VALID_STORES.any? { |host| url_name.includes?(host) }
errors.add(:url_name, 'must be an AppStore or a PlayStore url')
end
end
end

is there a pure command-line oauth flow for installed apps?

I'm trying to get a pure command line oauth flow for an installed app and it's not easy to piece this together... Docs are sorely lacking... I started with the drive example (https://github.com/google/google-api-ruby-client-samples/tree/master/drive) but when it gets to client.authorization = flow.authorize(file_storage) it tries to start webrick to put up a web page. I need something that works similarly to the CLI tools provided by google: it needs to print out the URL I need to visit and then read in the response that I can copy&paste. Is this possible with the google ruby client?
Looks like the following monkey-patch works:
module Google
class APIClient
class InstalledAppFlow
def authorize_cli(storage)
puts "Please visit: #{#authorization.authorization_uri.to_s}"
printf "Enter the code: code="
code = gets
#authorization.code = code
#authorization.fetch_access_token!
if #authorization.access_token
if storage.respond_to?(:write_credentials)
storage.write_credentials(#authorization)
end
#authorization
else
nil
end
end
end
end
end

Force google to show account chooser and consent screen

I am able to use both prompt=consent and prompt=select_account individually, but Google doesn't seem to allow me to combine them. I tried the prompt=consent+select_account as suggested in an answer of Force google account chooser, but that fails with the error: "Invalid prompt: consent+select_account".
The doc (https://developers.google.com/accounts/docs/OAuth2Login) says "A space-delimited list", so I tried consent select_account but that fails with: "The requested URL was not found on this server."
I also tried combining prompt=select_account and approval_prompt=force, but Google doesn't like that either.
Anyone else have luck with combining consent screen and account chooser?
Update:
This is my JavaScript method creating URL for getting contacts from gmail
$scope.importGmailContacts = function() {
provider = 'gmail';
$scope.importing_from_gmail = true;
window.open(protocol + "://" + host + ":" + port + "/contacts/gmail", "_blank",
"toolbar=yes, scrollbars=yes, resizable=yes, top=0, left=0, width=600, height=600, prompt='select_account+consent', approval_prompt=force");
}
I have tried setting prompt and approval_prompt both collectively and individually but it does not seems to work. Refer to this question.
You need add: access_type=online&prompt=select_account+consent:
private static final String AUTHORIZE_URL
= "https://accounts.google.com/o/oauth2/auth?"
+ "response_type=code&access_type=online&prompt=select_account+consent"
+ "&client_id=xxx&redirect_uri=xxx";
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=xxx";
..
I just tried this and it DID work when space-delimited:
options[:prompt] = 'select_account consent'
https://accounts.google.com/o/oauth2/auth?client_id=XXXX&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code&prompt=select_account+consent&approval_prompt=force
Results in the error
That’s an error.
Error: invalid_request
Conflict params: approval_prompt and prompt
Prompt and approval_prompt parameters can not be used together.
prompt Optional. A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your app requests access. Possible values are:
none Do not display any authentication or consent screens. Must not be specified with other values.
consent Prompt the user for consent.
select_account Prompt the user to select an account.
If memory serves approval_prompt is the older way of doing it and google added Prompt sometime in 2012. I cant actually find any documentation on approval_prompt anymore but if memory serves it was the same as doing prompt=consent it just requested access again.

How do I control my dropbox account from Ruby?

I read the documentation of the Dropbox SDK for Ruby, and couldn't find an example about using my own dropbox account. The examples I saw require that I get an ACCESS_TOKEN from a random user that validates the session for me.
I am the user! I just want to start uploading and deleting files; nothing more.
How do I achieve that?
EDIT:
snippet
require 'dropbox_sdk'
APP_KEY = '******'
APP_SECRET = '*****'
ACCESS_TYPE = :dropbox
session = DropboxSession.new(APP_KEY, APP_SECRET)
#I don't want to do this, I just want to know the way of authenticate
#myself with some method that don't need authorization
#-------------------------------------------
session.get_request_token
authorize_url = session.get_authorize_url
puts "AUTHORIZING", authorize_url
puts "Please visit that website and hit 'Allow', then hit Enter here."
gets
session.get_access_token
client = DropboxClient.new(session, ACCESS_TYPE)
puts "linked account:", client.account_info().inspect

Unknown command on Gmail gem

I'm trying to get my emails using the Gmail gem (source) with the next call:
class MyClass
def initialize account, credentials
#gmail = Gmail.connect(account, credentials)
end
def get_attachments received_at, options
mails = #gmail.inbox.find(
:to => options[:to],
:has=>:attachments,
:before => options[:end_date],
:after => received_at
)
#more code
end
end
#gmail is a valid instance of Gmail class, and this call raises sometimes the next error:
Unknown command v2if4084974eef.9
The command (v2if4084974eef.9) changes in every call I make.
What am I doing wrong?
[Edit]
The error raised in specs is this
1)
Net::IMAP::BadResponseError in 'MailFacade get_data should return an array'
Unknown command v17if3069084anm.44
I had this issue and I found the answer to this in the last comment on this link:
https://github.com/nu7hatch/gmail/issues/36
You just have to follow these steps:
1. Go to your Google account settings: myaccount.google.com
2. Click on "connected apps & sites" option.
3. Turn "Allow less secure apps" to "ON" (near the bottom of the page).
That's it, at least works for me, hope it works for you aswell.
Greetings from Bolivia!!
You need to allow "less secure" apps to use your Gmail account (as #xXAngelJinXx mentioned). Since Google changed the UI, now one needs to do the following:
Go to your Google account settings: https://myaccount.google.com
Click on "Security" on the left
Scroll down to "Less secure app access"
Click on "Turn on access (not recommended)"
Turn it to "ON".

Resources