Я тестирую приложение ruby on rails на локальном хосте, которому требуется аутентификация из Dropbox.
Когда оно говорит
Приложение хочет получить доступ к файлам и папкам в вашем Dropbox.
Я нажимаю разрешить.Тогда я получаю
DropboxError в SessionsController # create
v1_retired
У меня также,
session['access_token'] = access_token
@db = get_client access_token
name = @db.account_info['display_name']
email = @db.account_info['email']
uid = @db.account_info['uid']
user = User.find_by_provider_and_uid('dropbox', user_id) || User.create_with_omniauth(email, uid, name)
Где name = @db.account_info['display_name']
- красныйотметка в строке 24.
файл app/controllers/sessions_controller.rb:24:in `create'
Parameters:
{"state"=>"vJUckYTI8YJVzdw1L9OA8g==",
"code"=>"m6ncKLSfo7AAAAAAAAAADOJrqwYitp3Oi2HW8pXc",
"provider"=>"dropbox"}
sessions_controller.rb
require 'dropbox_sdk'
class SessionsController < ApplicationController
def get_auth
redirect_uri = ENV['db_callback']
flow = DropboxOAuth2Flow.new( ENV['db_key'], ENV['db_secret'], redirect_uri, session, :dropbox_auth_csrf_token)
end
def new
auth_url = get_auth.start
redirect_to auth_url
end
def index
if session['access_token'] != ''
@user = get_dropbox_client.account_info['display_name']
end
end
def create
code = params[:code]
begin
access_token, user_id, url_state = get_auth.finish(params)
rescue Exception
end
session['access_token'] = access_token
@db = get_client access_token
name = @db.account_info['display_name']
email = @db.account_info['email']
uid = @db.account_info['uid']
user = User.find_by_provider_and_uid('dropbox', user_id) || User.create_with_omniauth(email, uid, name)
session[:user_id] = uid
session[:access_token] = access_token
session[:user_name] = name
user.access_token = access_token
user.save
redirect_to '/'
end
def destroy
session.clear
redirect_to root_url
end
end
routs.rb
Rails.application.routes.draw do
match 'auth/:provider/callback', to: 'sessions#create', via: [:get, :post]
match '/', to: 'sites#load', constraints: { subdomain: /.+/ }, via: [:get, :post, :put, :patch, :delete]
root 'sites#index'
get '/logout', to: 'sessions#destroy'
get '/auth/dropbox', to: 'sessions#new'
match '/*req', to: 'sites#load', constraints: { subdomain: /.+/ }, via: [:get, :post, :put, :patch, :delete]
get '/about', to: 'pages#about'
get '/source', to: 'pages#source'
get '/pricing', to: 'pages#pricing'
get '/admin', to: 'pages#admin'
get '/webhook', to: 'webhook#challenge'
post '/webhook', to: 'webhook#post'
post "/versions/:id/revert", to: "versions#revert", as: "revert_version"
resources :sites, path: ''
end
У меня естьgem 'dropbox-sdk'
в моем gemfile и application_controller.rb:
require 'dropbox_sdk'
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def get_client at
return DropboxClient.new(at)
end
def current_user
User.find_by( access_token: session['access_token'] )
end
end
Gemfile
source 'https://rubygems.org'
gem 'figaro'
gem 'dropbox-sdk'
gem 'paper_trail'
gem 'httparty'
gem 'pry'
gem 'rails_12factor'
gem 'dalli'
group :development, :test do
gem 'rspec-rails', '~> 3.0'
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.8'
gem 'pg', '~> 0.20.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
Как изменить файл application_controller.rb в соответствии с dropbox_api