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
Related
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
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
I've written some Ruby code (connected with Cucumber) that will go to a website and click a file that I'd like to download. The browser I'm using for this is Google Chrome.
Typically, when you go to download a file in Chrome, it doesn't ask for permission. However, when I run the code I made, it says:
"This type of file can harm your computer. Do you want to keep file_name.exe anyway?" It gives 2 options, "keep" or "discard". I have to click keep.
Obviously, you don't want all executables to just start downloading; however, this particular website/file should always be trustworthy.
Is there a command in Ruby or Cucumber that allows you to click the "keep" button automatically? This could just be a general "click at this pixel" or something. Or is there a way to mark a particular website in Chrome as safe. You can't inspect the element because it's not part of the website, but, instead, part of the browser. Preferably without having to download other software.
With this being said, this suggests that if it is possible, it should also be possible to automate an installation (as in clicking next -> next -> etc) for you. Hopefully this is correct?
Thanks in advance.
You can implement it in any browser. But, for Google Chrome, here is the solution -
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = "Absolute or relative path to your download directory"
browser = Selenium::WebDriver.for :chrome, :profile => profile
You haven't specified which gem you use for browser. But, even if you use watir-webdriver, you can use the same profile you created above with watir-webdriver.
browser = Watir::Browser.new :chrome, :profile => profile
I actually switched to using Sikuli, which worked pretty well. Thanks for the help, though.
Do you really need or want the browser to download the file? Are you really testing the browser's download feature, or do you want to verify that the server can serve the file and that it is what you expect?
I found the idea of setting up a default directory and having to check for the file clumsy, fragile and prone to errors, especially when setting up on a new host, especially for tests that run in multiple browsers.
My solution is to just use Ruby (or whatever language) features to download the file directly, and then validate that it is the file it's supposed to be. I'm not testing the browser, I'm testing the software. The only exception to that idea I can think of is if you use some javascript logic or something browser-dependent to redirect you to a link, but please don't ever do that.
However, you run into a problem if you have to log in to access your file; you either have to implement auth in your Ruby code, which isn't technically part of your Cucumber specification, or you need the cookies. I use this code to copy the cookies to avoid logging in again, and grab the file:
def assert_file_link(uri, filename, content_type)
f = open_uri_with_cookies uri
attachment_filename = f.meta["content-disposition"].sub("Attachment;filename=", "") # "Attachment;filename=Simple Flow - Simple Form.rtf"
content_length = Integer(f.meta["content-length"])
assert(f.status == ["200", "OK"], "Response was not 200 OK")
assert(f.content_type == content_type, "Expected content-type of '#{content_type}' but was '#{f.content_type}'")
assert(attachment_filename == filename, "Expected filename of '#{filename}' but was '#{attachment_filename}'")
assert(content_length > 0, "Expected content-length > 0 but was '#{content_length}'")
end
def open_uri_with_cookies(uri)
# hack the cookies from the existing session so we don't need to log in!
cookies = ""
#driver.manage.all_cookies.each { |cookie| cookies.concat("#{cookie[:name]}=#{cookie[:value]}; ") }
if block_given?
open(uri, "Cookie" => cookies, :proxy => nil) do |f|
yield f
end
else
open(uri, "Cookie" => cookies, :proxy => nil)
end
end
Hope this helps.
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.
I'm trying to pull data from Google trends and got a "You have reached your daily limit" error after only 2 tries.
Is there any way to go around this? I know Google API projects have special quota limits but Google Trends doesn't have an API. I also read that we may need to pass it a cookie file so that it seems like I'm logged in. Has anyone faced this issue before?
I'm struggling with the same issue!
From your question I can't figure out what stage have you achieved...
But here is the solution that I've found:
You should emulate browser with cookies.
I think the best way to do it is to use Mechanize library.
At first your program should "login" using GET request to "https://accounts.google.com/Login?hl=en"
Immediately after that you can access some other personal resources, but not google trends!
After some significant time you can successfully get google trends data as CSV.
I still have not discovered the exact time period, but it is more than 10 minutes and less than several hours :). That is why saving your cookies for latter use is a good idea!
Few more tips:
If you are developing using python / ruby under Windows do not forget to set up CA ROOT certificates package for OpenSSL library. Otherwise HTTPS connection will fail and you won't login! See Getting the `certificate verify failed (OpenSSL::SSL::SSLError)` erro with Mechanize object
I recommend you to save cookies to external file at program shutdown. And restoring them at startup.
Do not forget to allow redirects, because Google is using redirects all the time.
Ruby code example:
require 'mechanize'
require 'logger'
begin
agent = Mechanize.new { |a|
a.user_agent = 'Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.16'
cert_store = OpenSSL::X509::Store.new
cert_store.add_file 'cacert.pem'
a.cert_store = cert_store
a.log = Logger.new('mech.log')
if File.file?('mech.cookies')
cookies = Mechanize::CookieJar.new
cookies.load('mech.cookies')
a.cookie_jar = cookies
end
a.open_timeout = 5
a.read_timeout = 6
a.keep_alive = true
a.redirect_ok = true
}
LOGIN_URL = "https://accounts.google.com/Login?hl=en&continue=http://www.google.com/trends/"
login_page = agent.get(LOGIN_URL)
login_form = login_page.forms.first
login_form.Email = *
login_form.Passwd = *
login_response_page = agent.submit(login_form)
page = agent.get(url)
# DO SOME TRENDS REQUESTS AFTER SIGNIFICANT PERIOD OF TIME
ensure
if agent
agent.cookie_jar.save('mech.cookies')
end
end
You probably disabled your cookies, which makes Google Trends think you're a robot
I think I have found a way to solve the problem. Just make sure that you call the Google Trends API with the cookie PREF. That is you don't need to login the Google account. Of course, you don't need to emulate browser. The cookie PREF is just enough.
OK. Where the cookie PREF comes from? It is very easy. Just open the browser, and login in your Google account. Finally, look up the cookie PREF under the Google website, it is just under the domain www.google.com.Then copy the value of the cookie PREF to your program or script. That's all.
I have called the Google Trends API hundreds of times in several seconds by this way. Good Luck to you!
I found this paper about prevention or just a Zeta-Jones effect in google Trends, it was so useable:
G Fond, A Gamanb, E Haffenb, P Llorca. "Google Trends: ready for real-time suicide prevention or just a Zeta-Jones effect ?." International Journal of Computer Networks and Communications Security 3, no. 1 (2015): 1-5.